Created
October 8, 2018 13:23
-
-
Save neerajvashistha/ac09d8032e071224e05fdc2b3ae08669 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import knn_final | |
| #do copy knn_final before running | |
| if __name__ == '__main__': | |
| from sklearn import datasets | |
| iris = datasets.load_iris() | |
| print(iris["data"]) | |
| predictors = iris.data[:,0:2] | |
| outcomes = iris.target | |
| plt.plot(predictors[outcomes==0][:,0],predictors[outcomes==0][:,1],"ro") | |
| plt.plot(predictors[outcomes==1][:,0],predictors[outcomes==1][:,1],"go") | |
| plt.plot(predictors[outcomes==2][:,0],predictors[outcomes==2][:,1],"bo") | |
| plt.show() | |
| k = 5; limits = (4,8,1.5,4.5); h = 0.1 | |
| (xx,yy,prediction_grid)=make_prediction_grid(predictors,outcomes,limits,h,k) | |
| plot_prediction_grid(xx,yy,prediction_grid) | |
| from sklearn.neighbors import KNeighborsClassifier | |
| knn = KNeighborsClassifier(n_neighbors=5) | |
| knn.fit(predictors,outcomes) | |
| sk_predictions = knn.predict(predictors) | |
| mypredictions = np.array([knn_predict(p,predictors,outcomes,5) for p in predictors]) | |
| print("Mutual Accuracy ",100*np.mean(sk_predictions==mypredictions)) | |
| print("SciKIt Accuracy",100*np.mean(sk_predictions==outcomes)) | |
| print("My Model Accuracy",100*np.mean(mypredictions==outcomes)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment