In this example, support vector regression (-SVR) is applied to a categorical response variable. The fitted values and prediction values are in the right tendency. Compare these to the results from support vector classification (-SVC), which is more appropriate for a categorical response variable.
import com.imsl.datamining.supportvectormachine.*; public class SupportVectorMachineEx4 { 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} }; /* Construct an SVMRegression. */ SVRegression svm1 = new SVRegression(xyTrain, 0, ex4DataType); svm1.setNuFormulation(true); svm1.setNuParameter(nu); svm1.setRegularizationParameter(C); 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.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])); } } }
-------------------------------------------------------------- NU SVR: Training data predicted (fitted) values Actual Fitted value | Difference 1.0 1.3662 0.3662 1.0 1.3730 0.3730 1.0 1.4175 0.4175 1.0 1.4066 0.4066 1.0 1.3987 0.3987 2.0 1.5993 -0.4007 2.0 1.6522 -0.3478 2.0 1.6249 -0.3751 2.0 1.6063 -0.3937 2.0 1.5825 -0.4175 -------------------------------------------------------------- NU SVR: Test data predictions Actual Prediction | Difference 1.0 1.4436 0.4436 1.0 1.3972 0.3972 2.0 1.6485 -0.3515 2.0 1.5983 -0.4017 -------------------------------------------------------------- NU SVC: Training data predicted (fitted) values Actual Fitted value | Difference 1.0 1.0000 0.0000 1.0 1.0000 0.0000 1.0 1.0000 0.0000 1.0 1.0000 0.0000 1.0 1.0000 0.0000 2.0 2.0000 0.0000 2.0 2.0000 0.0000 2.0 2.0000 0.0000 2.0 2.0000 0.0000 2.0 2.0000 0.0000 -------------------------------------------------------------- NU SVC: Test data predictions Actual Prediction | Difference 1.0 1.0000 0.0000 1.0 1.0000 0.0000 2.0 2.0000 0.0000 2.0 2.0000 0.0000Link to Java source.