StormsEdge
StormsEdge

Reputation: 885

Issue defining KneighborsClassifier in Jupyter Notebooks

I am attempting to utilize KNN on the Iris data set as a "Hello World" of Machine Learning. I am using a Jupyter Notebook from Anaconda and have been clearly documenting each step. A "NameError: name 'knn' is not defined" exception is currently being thrown when I attempt to use knn.fit(X,Y) What am I missing here? I attempted to test the definition of knn by calling print(knn) and I get the following output:

KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
           metric_params=None, n_jobs=1, n_neighbors=1, p=2,
           weights='uniform')

Code below:

#import the load_iris dataset
from sklearn.datasets import load_iris
#save "bunch" object containing iris dataset and its attributes
iris = load_iris()
X = iris.data
Y = iris.target

#import class you plan to use
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors = 1)

#Fit the model with data (aka "model training")
knn.fit(X,Y)

Upvotes: 4

Views: 22125

Answers (2)

Sangam Mishra
Sangam Mishra

Reputation: 13

update your scikit learn modeule.

if you are using jupyter notebook then you can update by running the below code

conda install -c conda-forge scikit-learn

Upvotes: 0

Adrian
Adrian

Reputation: 75

Had same issue. running the following worked for me:

model = sklearn.neighbors.KNeighborsClassifier(n_neighbors=5) 

ran in:

Python 3.6.9

Upvotes: 1

Related Questions