public static void svmTrain(ArrayList<ArrayList<Double>> train) { svm_problem prob = new svm_problem(); int dataCount = train.size(); prob.y = new double[dataCount]; prob.l = dataCount; prob.x = new svm_node[dataCount][]; for (int i = 0; i < dataCount; i++) { ArrayList<Double> features = train.get(i); prob.x[i] = new svm_node[features.size() - 1]; for (int j = 1; j < features.size(); j++) { svm_node node = new svm_node(); node.index = j - 1; node.value = features.get(j); prob.x[i][j - 1] = node; } prob.y[i] = features.get(0); } svm_parameter param = new svm_parameter(); param.probability = 1; param.gamma = 0.5; param.nu = 0.5; param.C = 1; param.svm_type = svm_parameter.NU_SVC; param.kernel_type = svm_parameter.LINEAR; param.cache_size = 20000; param.eps = 0.001; _model = svm.svm_train(prob, param); }
public void addPrev(ArrayList<MultiPoint> data, svm_node[] buf, int i) { int L = data.get(0).values.length; for (int j = 0; j < prev; j++) { for (int l = 0; l < L; l++) { svm_node node = new svm_node(); node.index = colCnt + j; node.value = data.get(i - j - 1).values[l]; buf[colCnt + j * L + l] = node; } } // svm_node node = new svm_node(); // node.index = colCnt+prev; // node.value = 1.0; // buf[colCnt+prev*L]=node; colCnt += prev * L; }
public static LabelAndProbability evaluate(ArrayList<Double> features) { svm_node[] nodes = new svm_node[features.size() - 1]; for (int i = 1; i < features.size(); i++) { svm_node node = new svm_node(); node.index = i; node.value = features.get(i); nodes[i - 1] = node; } int[] labels = new int[Globals._numExercises]; int n = Globals._numExercises; Log.d("Number of Exercises", "" + n); double[] values = new double[(n * (n - 1) / 2)]; svm.svm_get_labels(_model, labels); for (int i = 0; i < n; i++) { Log.d("labels", "" + labels[i]); } int Label = (int) svm.svm_predict_values(_model, nodes, values); double accuracy = 0.0; int votes = 0; int p = 0; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) { if (Label == labels[i]) { accuracy += values[p]; if (values[p] > 0) votes++; } else if (Label == labels[j]) { accuracy -= values[p]; if (values[p] < 0) votes++; } p++; } // int label = 0; // for (int i = 0; i < probabilities.length; i++) { // Log.d("Labels and Probabilites", labels[i] + " : " + probabilities[i]); // if (max < probabilities[i]) { // label = labels[i]; // max = probabilities[i]; // } // } // return new LabelAndProbability(Label, (accuracy/n)); return new LabelAndProbability(Label, votes); }
public svm_node[] buildingBean2SvmNode(BuldingBean buldingBean) { // TODO Auto-generated method stub svm_node p0 = new svm_node(); p0.index = 0; p0.value = buldingBean.getBaseElectric(); svm_node p1 = new svm_node(); p1.index = 1; p1.value = buldingBean.getAirElectric(); svm_node[] p = {p0, p1}; return p; }