Support vector machine (SVM) binary linear classifier using Smile
Code: "classify_smv.py". Programming language: Python DMelt Version 2.2. Last modified: 03/10/2018. License: Pro
https://datamelt.org/code/cache/classify_smv_6821.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.



#Support Vector Machines
# Support vector machine (SVM) is a binary linear classifier which chooses the hyperplane that represents the largest separation, or margin, between the two classes. If such a hyperplane exists, it is known as the maximum-margin hyperplane and the linear classifier it defines is known as a maximum margin classifier.
# If there exists no hyperplane that can perfectly split the positive and negative instances, the soft margin method will choose a hyperplane that splits the instances as cleanly as possible, while still maximizing the distance to the nearest cleanly split instances.

from smile.data import AttributeDataset,NominalAttribute
from smile.data.parser import DelimitedTextParser,IOUtils
from java.io import File
from jhplot import *
from smile.classification import SVM
from smile.math.kernel import GaussianKernel
from jarray import zeros,array
from java.awt import Color
import java

# this function extract data[][] and label[] array from datasets
def getJavaArrays(dataset):
    rows=dataset.size()
    lst = [0.0]*rows
    twoDimArr = array([lst,[]], java.lang.Class.forName('[D'))
    data = dataset.toArray(twoDimArr)
    label = dataset.toArray(zeros(rows, "i"));
    return data,label

http="http://datamelt.org/examples/data/classification/toy/"
print "Reading data from",http
datasource="toy-train.txt" # also try toy-test.txt (20k events) 
datasetName="Toy"
print Web.get(http+datasource)

parser =DelimitedTextParser();
parser.setDelimiter("[\t ]+");
parser.setResponseIndex(NominalAttribute("class"), 0);
dataset=parser.parse(datasetName,File(datasource))
print dataset
print "SMV classification.."

C=1.0; gamma=1
data,label=getJavaArrays(dataset);
svm = SVM(GaussianKernel(gamma), C)
svm.learn(data, label);
svm.finish()

# now we will use another sample for prediction
print "Training finished. Get a new sample for predictions.."
datasource="toy-test.txt" #  (20k events) 
datasetName="Testing"
print Web.get(http+datasource)
dataset=parser.parse(datasetName,File(datasource))
data,label=getJavaArrays(dataset);

#print label
#print type(data)
prediction=svm.predict(data)
rows=dataset.size()

c1 = SPlot()
c1.setGTitle("Input")
c1.visible()
c1.setAutoRange()
c1.setMarksStyle('various')
c1.setNameX('X')
c1.setNameY('Y')

c2 = SPlot()
c2.setGTitle("Predictions")
c2.visible()
c2.setAutoRange()
c2.setMarksStyle('various')
c2.setNameX('X')
c2.setNameY('Y')

e=0.0
for i in range(rows):
    minput=data[i].tolist()
    expected=label[i]
    predicted=prediction[i] 
    c1.addPoint(expected,minput[0],minput[1],1)
    c2.addPoint(predicted,minput[0],minput[1],1)
    print "Input=",minput, " outcome=",expected," prediction=",predicted
    if (expected != predicted): e=e+1.0
e=e/rows
print "Error rate=",e

c1.update()
c2.update()


You see the box below because you did not login.