Skip to content

Instantly share code, notes, and snippets.

@ravikiranj
Last active October 4, 2015 13:07
Show Gist options
  • Select an option

  • Save ravikiranj/2641755 to your computer and use it in GitHub Desktop.

Select an option

Save ravikiranj/2641755 to your computer and use it in GitHub Desktop.
Simple SVM Demo
import svm
from svmutil import *
#training data
labels = [0, 1, 1, 2]
samples = [[0, 1, 0], [1, 1, 1], [1, 1, 0], [0, 0, 0]]
#SVM params
param = svm_parameter()
param.C = 10
param.kernel_type = LINEAR
#instantiate the problem
problem = svm_problem(labels, samples)
#train the model
model = svm_train(problem, param)
# saved model can be loaded as below
#model = svm_load_model('model_file')
#save the model
svm_save_model('model_file', model)
#test data
test_data = [[0, 1, 1], [1, 0, 1]]
#predict the labels
p_labels, p_accs, p_vals = svm_predict([0]*len(test_data), test_data, model)
print p_labels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment