package com.imsl.test.example.datamining.supportvectormachine; import com.imsl.datamining.supportvectormachine.*; /** *
* Illustrates the use of case weights on the * training data. *
** This example is the same as {@link SupportVectorMachineEx4} except that it * also shows how to apply case weights to the training data. Compare the fitted * values between the two examples (the predicted values do not change). *
* * @see Code * @see Output * */ public class SupportVectorMachineEx5 { public static void main(String[] args) throws Exception { SVRegression.VariableType[] ex4DataType = { SVRegression.VariableType.CATEGORICAL, SVRegression.VariableType.QUANTITATIVE_CONTINUOUS, SVRegression.VariableType.QUANTITATIVE_CONTINUOUS}; String dashes = "--------------------------------------------------------------"; double C = 50., nu = .01; double[][] xyTrain = { {1, 0.19, 0.61}, {1, 0.156, 0.564}, {1, 0.224, 0.528}, {1, 0.178, 0.51}, {1, 0.234, 0.578}, {2, 0.394, 0.296}, {2, 0.478, 0.254}, {2, 0.454, 0.294}, {2, 0.48, 0.358}, {2, 0.398, 0.336} }; double[][] xyTest = { {1, 0.316, 0.556}, {1, 0.278, 0.622}, {2, 0.562, 0.336}, {2, 0.522, 0.412} }; double[] trainingWts = {10, 1, 1, 1, 1, 1, 1, 1, 1, 1}; /* Construct a Support Vector Machine. */ SVRegression svm1 = new SVRegression(xyTrain, 0, ex4DataType); svm1.setNuFormulation(true); svm1.setNuParameter(nu); svm1.setRegularizationParameter(C); svm1.setWeights(trainingWts); svm1.fitModel(); double[] fittedValues = svm1.predict(); System.out.println("\n" + dashes); System.out.println(" NU SVR: Training data predicted (fitted) values"); System.out.println(" Actual Fitted value | Difference"); for (int i = 0; i < fittedValues.length; i++) { System.out.printf(" %2.1f %5.4f %5.4f\n", xyTrain[i][0], fittedValues[i], (fittedValues[i] - xyTrain[i][0])); } System.out.println("\n" + dashes); double[] testPredictedValues = svm1.predict(xyTest); System.out.println("\n NU SVR: Test data predictions"); System.out.println(" Actual Prediction | Difference"); for (int i = 0; i < testPredictedValues.length; i++) { System.out.printf(" %2.1f %5.4f %5.4f\n", xyTest[i][0], testPredictedValues[i], (testPredictedValues[i] - xyTest[i][0])); } /* Now use the categorical version and compare results. */ SVClassification svm2 = new SVClassification(xyTrain, 0, ex4DataType); svm2.setNuFormulation(true); svm2.setNuParameter(nu); svm2.setRegularizationParameter(C); svm2.setWeights(trainingWts); svm2.fitModel(); fittedValues = svm2.predict(); System.out.println("\n" + dashes); System.out.println(" NU SVC: Training data predicted (fitted) values"); System.out.println(" Actual Fitted value | Difference"); for (int i = 0; i < fittedValues.length; i++) { System.out.printf(" %2.1f %5.4f %5.4f\n", xyTrain[i][0], fittedValues[i], (fittedValues[i] - xyTrain[i][0])); } System.out.println("\n" + dashes); testPredictedValues = svm2.predict(xyTest); System.out.println("\n NU SVC: Test data predictions"); System.out.println(" Actual Prediction | Difference"); for (int i = 0; i < testPredictedValues.length; i++) { System.out.printf(" %2.1f %5.4f %5.4f\n", xyTest[i][0], testPredictedValues[i], (testPredictedValues[i] - xyTest[i][0])); } } }