In [6]:
# import load_iris function from datasets module
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
print X.shape
print y.shape
In [7]:
from sklearn.neighbors import KNeighborsClassifier
In [8]:
knn = KNeighborsClassifier(n_neighbors=1)
+ Name of the object does not matter
+ Can specify tuning parameters during this step
+ All parameters not specified are set to defaults.
In [9]:
print knn
In [10]:
knn.fit(X,y)
Out[10]:
In [11]:
knn.predict([3,5,4,2])
Out[11]:
In [12]:
X_new = [[3,5,4,2], [5,4,3,2]]
knn.predict(X_new)
Out[12]:
In [13]:
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X,y)
knn.predict(X_new)
Out[13]:
In [14]:
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
logreg.fit(X,y)
logreg.predict(X_new)
Out[14]:
In [ ]: