.splitData(n_fold, new Random(0));
public static double crossValidation(double C, int n_thread, int n_fold, LabeledMulticlassData train) throws Exception { double cv_res = 0; List<Pair<StructuredProblem, StructuredProblem>> data_pair_list = train.sp .splitData(n_fold, new Random(0)); for (int i = 0; i < n_fold; i++) { StructuredProblem cv_sp_train = data_pair_list.get(i).getFirst(); StructuredProblem cv_sp_test = data_pair_list.get(i).getSecond(); LabeledMulticlassData cv_train = new LabeledMulticlassData( train.label_mapping, train.n_base_feature_in_train, cv_sp_train); LabeledMulticlassData cv_test = new LabeledMulticlassData( train.label_mapping, train.n_base_feature_in_train, cv_sp_test); MulticlassModel cv_model = trainMultiClassModel(C, n_thread, cv_train); Pair<int[], int[]> gold_pred = getPredictionResults(cv_model, cv_test); double test_acc = getTestingAcc(gold_pred.getFirst(), gold_pred.getSecond()); cv_res += test_acc; System.out.println("Fold " + i + ":" + test_acc); } return cv_res / n_fold; }
public static Pair<Double,Double> crossValidation(double C, int n_thread, int n_fold, LabeledMulticlassData train, double[][] cost_matrix) throws Exception { double cv_acc = 0; double cv_cost = 0; List<Pair<StructuredProblem, StructuredProblem>> data_pair_list = train.sp .splitData(n_fold, new Random(0)); for (int i = 0; i < n_fold; i++) { StructuredProblem cv_sp_train = data_pair_list.get(i).getFirst(); StructuredProblem cv_sp_test = data_pair_list.get(i).getSecond(); LabeledMulticlassData cv_train = new LabeledMulticlassData( train.label_mapping, train.n_base_feature_in_train, cv_sp_train); LabeledMulticlassData cv_test = new LabeledMulticlassData( train.label_mapping, train.n_base_feature_in_train, cv_sp_test); MulticlassModel cv_model = trainCostSensitiveMultiClassModel(C, n_thread, cv_train,cost_matrix); Pair<int[], int[]> gold_pred = getPredictionResults(cv_model, cv_test); double test_acc = getTestingAcc(gold_pred.getFirst(), gold_pred.getSecond()); cv_acc += test_acc; System.out.println("Fold " + i + " acc :" + test_acc); double test_cost = getTestingCost(gold_pred.getFirst(), gold_pred.getSecond(), cost_matrix); cv_cost += test_cost; System.out.println("Fold " + i + " cost :" + test_cost); } return new Pair<Double, Double>(cv_acc/n_fold, cv_cost/n_fold); }