View Javadoc

1   package liblinear;
2   
3   import static liblinear.Linear.copyOf;
4   
5   
6   public final class Parameter {
7   
8       double     C;
9   
10      /** stopping criteria */
11      double     eps;
12  
13      SolverType solverType;
14  
15      double[]   weight      = null;
16  
17      int[]      weightLabel = null;
18  
19      public Parameter( SolverType solverType, double C, double eps ) {
20          setSolverType(solverType);
21          setC(C);
22          setEps(eps);
23      }
24  
25      /**
26       * <p>nr_weight, weight_label, and weight are used to change the penalty
27       * for some classes (If the weight for a class is not changed, it is
28       * set to 1). This is useful for training classifier using unbalanced
29       * input data or with asymmetric misclassification cost.</p>
30       *
31       * <p>Each weight[i] corresponds to weight_label[i], meaning that
32       * the penalty of class weight_label[i] is scaled by a factor of weight[i].</p>
33       *
34       * <p>If you do not want to change penalty for any of the classes,
35       * just set nr_weight to 0.</p>
36       */
37      public void setWeights(double[] weights, int[] weightLabels) {
38          if (weights == null) throw new IllegalArgumentException("'weight' must not be null");
39          if (weightLabels == null || weightLabels.length != weights.length)
40              throw new IllegalArgumentException("'weightLabels' must have same length as 'weight'");
41          this.weightLabel = copyOf(weightLabels, weightLabels.length);
42          this.weight = copyOf(weights, weights.length);
43      }
44  
45      /**
46       * @see #setWeights(double[], int[])
47       */
48      public double[] getWeights() {
49          return copyOf(weight, weight.length);
50      }
51  
52      /**
53       * @see #setWeights(double[], int[])
54       */
55      public int[] getWeightLabels() {
56          return copyOf(weightLabel, weightLabel.length);
57      }
58  
59      /**
60       * the number of weights
61       * @see #setWeights(double[], int[])
62       */
63      public int getNumWeights() {
64          if (weight == null) return 0;
65          return weight.length;
66      }
67  
68      /**
69       * C is the cost of constraints violation. (we usually use 1 to 1000)
70       */
71      public void setC(double C) {
72          if (C <= 0) throw new IllegalArgumentException("C must not be <= 0");
73          this.C = C;
74      }
75  
76      public double getC() {
77          return C;
78      }
79  
80      /**
81       * eps is the stopping criterion. (we usually use 0.01).
82       */
83      public void setEps(double eps) {
84          if (eps <= 0) throw new IllegalArgumentException("eps must not be <= 0");
85          this.eps = eps;
86      }
87  
88      public double getEps() {
89          return eps;
90      }
91  
92      public void setSolverType(SolverType solverType) {
93          if (solverType == null) throw new IllegalArgumentException("solver type must not be null");
94          this.solverType = solverType;
95      }
96  
97      public SolverType getSolverType() {
98          return solverType;
99      }
100 }