Last active
December 19, 2015 01:39
-
-
Save thearn/5877378 to your computer and use it in GitHub Desktop.
Scikit-learn example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from sklearn import svm | |
from sklearn.grid_search import GridSearchCV | |
import pylab | |
import numpy as np | |
data = [] | |
labels = [] | |
# generate data | |
pylab.figure() | |
for i in xrange(25): | |
x = random.uniform(0, 1) | |
y = random.uniform(0, 1) | |
data.append([x, y]) | |
if x < y: | |
pylab.plot([x], [y], 'ro') | |
labels.append(1) | |
else: | |
pylab.plot([x], [y], 'bo') | |
labels.append(0) | |
# | |
# pylab.show() | |
# make a classifier | |
tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4], | |
'C': [1, 10, 100, 1000]}, | |
{'kernel': ['linear'], 'C': [1, 10, 100, 1000]}] | |
clf = GridSearchCV(svm.SVC(C=1), tuned_parameters) | |
clf.fit(np.array(data), np.array(labels), cv=5) | |
# verify with new data | |
for i in xrange(1000): | |
x = random.uniform(0, 1) | |
y = random.uniform(0, 1) | |
data.append([x, y]) | |
if clf.predict([x, y])[0] == 1: | |
pylab.plot([x], [y], 'rx') | |
labels.append(0) | |
else: | |
pylab.plot([x], [y], 'bx') | |
labels.append(1) | |
# | |
pylab.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment