Skip to content

Instantly share code, notes, and snippets.

@schwehr
Created February 10, 2013 07:38
Show Gist options
  • Save schwehr/4748800 to your computer and use it in GitHub Desktop.
Save schwehr/4748800 to your computer and use it in GitHub Desktop.
sklearn python from going through Jake's video
import numpy as np
X = np.random.random((100, 4))
X.shape
from sklearn import datasets
data = datasets.load_iris()
iris = datasets.load_iris()
type(data)
x = data.data
y = data.target
x.shape
y.shape
x[0]
x[:,0]
scatter(x[:,0], x[:,1], c=y)
set(y)
from sklearn.svm import LinearSVC
clf = LinearSVC() # clf == classifier
clf
LinearSVC?
clf.fit(x,y)
clf.coef_
x_new = np.array([[5.0, 3.6, 1.3, 0.25]])
x_new.shape
clf.predict(x_new)
iris.target_names
# <headingcell level=2>
# PCA
from sklearn.decomposition import PCA
x.shape
pca = PCA(n_components=2, whiten=True)
pca.fit(x)
y = pca.transform(x)
y.shape
scatter(y[:,0], y[:,1], c=data.target)
from sklearn.cluster import KMeans
from numpy.random import RandomState
rng = RandomState(42)
kmeans = KMeans(3, random_state=rng).fit(y)
kmeans.labels_
scatter(y[:,0], y[:,1], c=kmeans.labels_)
# Take a look at the unsupervised classification
!wget https://raw.github.com/scikit-learn/scikit-learn/master/examples/applications/svm_gui.py
!python svm_gui.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment