Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

knn classifier python example

from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics
#Try running from k=1 through 20 and record testing accuracy
k_range = range(1,21)
scores = {}
scores_list = []
for k in k_range:
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(X_train,y_train)
    y_pred=knn.predict(x_test)
    scores[k] = metrics.accuracy_score(y_test,y_pred)
    scores_list.append(metrics.accuracy_score(y_test,y_pred))

#after running the code above, assign to n_neighbors the best performing value
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X,y)
y_pred= knn.predict(x_test)
Comment

PREVIOUS NEXT
Code Example
Python :: sqlalchemy validation 
Python :: how to get width of an object in pyqt5 
Python :: max of a dict 
Python :: python read music stream 
Python :: pandas where based another column 
Python :: pyautogui pause in python 
Python :: pandas new df from groupby 
Python :: python convert hex to binary 
Python :: rename columns in datarame pandas 
Python :: Get Key From value in dictionary 
Python :: tkinter entry read only 
Python :: print random word py 
Python :: command to check python version in linux 
Python :: python datetime to utc 
Python :: python selenium save cookies 
Python :: Parameter Grid python 
Python :: roll longitude about zero 
Python :: import fashion mnist keras 
Python :: how to log ip addresses in python 
Python :: how to find second maximum element of an array python 
Python :: pandas join two series on index 
Python :: list to tuple 
Python :: remove python2 centos 
Python :: pipenv 
Python :: How to get all links from a google search using python 
Python :: random list python 
Python :: how to slice dataframe based on daterange in pandas 
Python :: athena connector python 
Python :: find python path cmd 
Python :: median absolute deviation scipy 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =