forked from bwaldvogel/liblinear-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.java
More file actions
101 lines (80 loc) · 2.85 KB
/
Copy pathModel.java
File metadata and controls
101 lines (80 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package liblinear;
import java.io.Serializable;
import java.util.Arrays;
import static liblinear.Linear.copyOf;
/**
* use {@link Linear#loadModel(String)} and {@link Linear#saveModel(String, Model)} to load/save it
*/
public final class Model implements Serializable {
private static final long serialVersionUID = -6456047576741854834L;
double bias;
/** label of each class (label[n]) */
int[] label;
/** number of classes */
int nr_class;
int nr_feature;
SolverType solverType;
double[] w;
public int getNrClass() {
return nr_class;
}
public int getNrFeature() {
return nr_feature;
}
public int[] getLabels() {
return copyOf(label, nr_class);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("Model");
sb.append(" bias=").append(bias);
sb.append(" nr_class=").append(nr_class);
sb.append(" nr_feature=").append(nr_feature);
sb.append(" solverType=").append(solverType);
return sb.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(bias);
result = prime * result + (int)(temp ^ (temp >>> 32));
result = prime * result + Arrays.hashCode(label);
result = prime * result + nr_class;
result = prime * result + nr_feature;
result = prime * result + ((solverType == null) ? 0 : solverType.hashCode());
result = prime * result + Arrays.hashCode(w);
return result;
}
@Override
public boolean equals( Object obj ) {
if ( this == obj ) return true;
if ( obj == null ) return false;
if ( getClass() != obj.getClass() ) return false;
Model other = (Model)obj;
if ( Double.doubleToLongBits(bias) != Double.doubleToLongBits(other.bias) ) return false;
if ( !Arrays.equals(label, other.label) ) return false;
if ( nr_class != other.nr_class ) return false;
if ( nr_feature != other.nr_feature ) return false;
if ( solverType == null ) {
if ( other.solverType != null ) return false;
} else if ( !solverType.equals(other.solverType) ) return false;
if ( !equals(w, other.w) ) return false;
return true;
}
/**
* don't use {@link Arrays#equals(double[], double[])} here, cause 0.0 and -0.0 should be handled the same
*
* @see Linear#saveModel(java.io.Writer, Model)
*/
protected static boolean equals( double[] a, double[] a2 ) {
if ( a == a2 ) return true;
if ( a == null || a2 == null ) return false;
int length = a.length;
if ( a2.length != length ) return false;
for ( int i = 0; i < length; i++ )
if ( a[i] != a2[i] ) return false;
return true;
}
}