Java implementation of simple version SVM

Source: Internet
Author: User
Tags svm

Java implementation of simple version SVM
The recent image classification work to use the latent SVM, in order to further understand the SVM, the implementation of a simple version of their own.
The reason is simple version, because it is not practical to Lagrange, duality, nuclear functions and so on. Instead, it is solved with the simplest gradient descent method. In the mathematics of the principle I took the http://blog.csdn.net/lifeitengup/article/details/10951655, the text is the use of MATLAB to achieve the SVM.

Source code and data set download: HTTPS://GITHUB.COM/LINGER2012/SIMPLESVM
With the data set coming from LIBSVM, I found one of the data set Http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/binary/breast-cancer_scale. Divide her into two parts, a training set and a test set, corresponding to TRAIN_BC and TEST_BC.
Among the test results are as follows:


Package Com.linger.svm;import Java.io.file;import Java.io.filenotfoundexception;import java.io.IOException;import Java.io.randomaccessfile;import Java.util.stringtokenizer;public class SIMPLESVM {private int examplenum;private int Exampledim;private double[] W;private double lambda;private double lr = 0.001;//0.00001private double threshold = 0.001;PR Ivate double cost;private double[] grad;private double[] yp;public SIMPLESVM (double paramlambda) {lambda = Paramlambda;} private void Costandgrad (double[][] x,double[] y) {cost =0;for (int m=0;m<examplenum;m++) {yp[m]=0;for (int d=0;d< exampledim;d++) {yp[m]+=x[m][d]*w[d];} if (y[m]*yp[m]-1<0) {cost + = (1-y[m]*yp[m]);}} for (int. d=0;d<exampledim;d++) {cost + = 0.5*lambda*w[d]*w[d];} for (int d=0;d<exampledim;d++) {Grad[d] = Math.Abs (Lambda*w[d]), for (int m=0;m<examplenum;m++) {if (y[m]*yp[m]-1 <0) {grad[d]-= y[m]*x[m][d];}}} private void Update () {for (int d=0;d<exampledim;d++) {W[d]-= Lr*grad[d];}} public void Train (double[][] x,double[] y,int maxiters) {examplenum = X.length;if (Examplenum <=0) {System.out.println ("num of Example <=0!"); return;} Exampledim = X[0].length;w = new Double[exampledim];grad = new DOUBLE[EXAMPLEDIM];YP = new Double[examplenum];for (int iter =0;iter<maxiters;iter++) {Costandgrad (x, y); System.out.println ("Cost:" +cost), if (cost< threshold) {break;} Update ();}} private int Predict (double[] x) {double pre=0;for (int j=0;j<x.length;j++) {pre+=x[j]*w[j];} if (pre >=0)//This threshold is typically located at-1 to 1return 1;else return-1;} public void Test (double[][] testx,double[] testy) {int error=0;for (int i=0;i<testx.length;i++) {if (Predict (testx[i) ) = Testy[i]) {error++;}} System.out.println ("total:" +testx.length); System.out.println ("Error:" +error); SYSTEM.OUT.PRINTLN ("Error Rate:" + (double) error/testx.length)); SYSTEM.OUT.PRINTLN ("ACC Rate:" + (Double) (testx.length-error)/testx.length);} public static void LoadData (double[][]x,double[] y,string trainfile) throws Ioexception{file file = new file (trainfile); Randomaccessfile RAF = NEW randomaccessfile (file, "R"); StringTokenizer Tokenizer,tokenizer2; int Index=0;while (TRUE) {String line = Raf.readline (); if (line = = null) Break;tokenizer = new StringTokenizer (line, ""); y[i Ndex] = double.parsedouble (Tokenizer.nexttoken ());//system.out.println (Y[index]); while (Tokenizer.hasmoretokens () {tokenizer2 = new StringTokenizer (Tokenizer.nexttoken (), ":"); int k = Integer.parseint (Tokenizer2.nexttoken ()); Double v = double.parsedouble (Tokenizer2.nexttoken ()); X[index][k] = v;//system.out.println (k);//system.out.println (v);} X[index][0] =1;index++;}} public static void Main (string[] args) throws IOException {//TODO auto-generated method stubdouble[] y = new double[400]; double[][] X = new DOUBLE[400][11]; String trainfile = "E:\\PROJECT\\WORKSPACE\\ALGORITHMS\\BIN\\TRAIN_BC"; LoadData (X,y,trainfile); SIMPLESVM SVM = new SIMPLESVM (0.0001); SVM. Train (x,y,7000);d ouble[] test_y = new double[283];d ouble[][] test_x = new DOUBLE[283][11]; String testfile = "e:\\project\\workspace\\algorithms\\bin\\TEST_BC "; LoadData (Test_x,test_y,testfile); SVM. Test (test_x, test_y);}}



This article linger this article link: http://blog.csdn.net/lingerlanlan/article/details/38688539

Java implementation of simple version SVM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.