Wednesday, March 28, 2012

machine learning - An example using LibSVM in python - Stack Overflow

machine learning - An example using LibSVM in python - Stack Overflow: "import numpy as NP
from svm import *

# just random generate 500 points within a given boundary:
# quite a few real data sets are are provided on the  LIBSVM [website][1]
Data = NP.random.randint(-5, 5, 1000).reshape(500, 2)

# choose some non-linear decision boundary:
# let's say that  for these 500 points, those that lie *within* an arbitrary
# circle (which i define below) are in Class I, and those
# outside this circle are Class II
rx = [ (x**2 + y**2) < 9 and 1 or 0 for (x, y) in Data ]

# to 'svm_problem' i passed in the decision boundary and the data
px = svm_problem(rx, Data)

# the kernel function i have selected is RBF (radial basis function)
pm = svm_parameter(kernel_type=RBF)

# train the classifier
v = svm_model(px, pm)

# now test it by calling 'predict' on the trained model object ('v') :
v.predict([3, 1])
# returns the class label (either '1' or '0')"

'via Blog this'