From the video series: Introduction to machine learning with scikit-learn
from IPython.display import IFrame
IFrame('http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', width=300, height=200)
Image Credits: Data3classes, Map1NN, Map5NN by Agor153. Licensed under CC BY-SA 3.0
# import load_iris function from datasets module
from sklearn.datasets import load_iris
# save "bunch" object containing iris dataset and its attributes
iris = load_iris()
# store feature matrix in "X"
X = iris.data
# store response vector in "y"
y = iris.target
# print the shapes of X and y
print(X.shape)
print(y.shape)
(150L, 4L) (150L,)
Step 1: Import the class you plan to use
from sklearn.neighbors import KNeighborsClassifier
Step 2: "Instantiate" the "estimator"
knn = KNeighborsClassifier(n_neighbors=1)
print(knn)
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski', metric_params=None, n_jobs=1, n_neighbors=1, p=2, weights='uniform')
Step 3: Fit the model with data (aka "model training")
knn.fit(X, y)
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski', metric_params=None, n_jobs=1, n_neighbors=1, p=2, weights='uniform')
Step 4: Predict the response for a new observation
knn.predict([[3, 5, 4, 2]])
array([2])
X_new = [[3, 5, 4, 2], [5, 4, 3, 2]]
knn.predict(X_new)
array([2, 1])
# instantiate the model (using the value K=5)
knn = KNeighborsClassifier(n_neighbors=5)
# fit the model with data
knn.fit(X, y)
# predict the response for new observations
knn.predict(X_new)
array([1, 1])
# import the class
from sklearn.linear_model import LogisticRegression
# instantiate the model (using the default parameters)
logreg = LogisticRegression()
# fit the model with data
logreg.fit(X, y)
# predict the response for new observations
logreg.predict(X_new)
array([2, 0])
from IPython.core.display import HTML
def css_styling():
styles = open("styles/custom.css", "r").read()
return HTML(styles)
css_styling()