Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """1. KNN on Iris Datset.ipynb
- from sklearn.datasets import load_iris
- #Create bunch object containing iris dataset and its attributes.
- iris = load_iris()
- X = iris.data
- y = iris.target
- from sklearn.cross_validation import train_test_split
- X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=4)
- from sklearn.neighbors import KNeighborsClassifier
- from sklearn import metrics
- #Try running from k=1 through 25 and record testing accuracy
- k_range = range(1,26)
- 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))
- #Testing accuracy for each value of K
- scores
- # Commented out IPython magic to ensure Python compatibility.
- # %matplotlib inline
- import matplotlib.pyplot as plt
- #plot the relationship between K and the testing accuracy
- plt.plot(k_range,scores_list)
- plt.xlabel('Value of K for KNN')
- plt.ylabel('Testing Accuracy')
- """### K values with 3 to 19 has the same accuracy which is 96.66, so we can use any one value from that, i choose K as 5 and train the model with full training data"""
- knn = KNeighborsClassifier(n_neighbors=5)
- knn.fit(X,y)
- #0 = setosa, 1=versicolor, 2=virginica
- classes = {0:'setosa',1:'versicolor',2:'virginica'}
- #Making prediction on some unseen data
- #predict for the below two random observations
- x_new = [[3,4,5,2],
- [5,4,2,2]]
- y_predict = knn.predict(x_new)
- print(classes[y_predict[0]])
- print(classes[y_predict[1]])
Add Comment
Please, Sign In to add comment