Advertisement
EXTREMEXPLOIT

KNN Algorithm

Mar 27th, 2020
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. from pandas import read_csv
  2. from sklearn.neighbors import KNeighborsClassifier
  3. import matplotlib.pyplot as plt
  4. from mpl_toolkits.mplot3d import Axes3D
  5. from sklearn import preprocessing
  6. from matplotlib import style
  7. import sklearn
  8. import numpy as np
  9.  
  10. style.use('bmh')
  11.  
  12. DATA = read_csv('Iris_Classifier.csv')
  13. X = np.array(DATA[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']])
  14. Y = np.array(DATA['Species'])
  15. X_Train, X_Test, Y_Train, Y_Test = sklearn.model_selection.train_test_split(X, Y, test_size=0.1)
  16.  
  17. KNN_Model = KNeighborsClassifier(n_neighbors=5)
  18. KNN_Model.fit(X_Train, Y_Train)
  19. Accuracity = KNN_Model.score(X_Test, Y_Test)
  20. print('Accuracity:', str(Accuracity*100)[:5] + str('%'))
  21.  
  22. Prediction = KNN_Model.predict([[5.1, 3.5, 1.4, 0.2]])[0]
  23. print(Prediction)
  24.  
  25. labelEncoder = preprocessing.LabelEncoder()
  26. numericSpecies = labelEncoder.fit_transform(DATA['Species'])
  27. ax = plt.axes(projection='3d')
  28. for i in range(len(DATA)):
  29.     ax.scatter3D(DATA['SepalLengthCm'][i], DATA['SepalWidthCm'][i], numericSpecies[i], color=('red', 'green', 'blue')[numericSpecies[i]], alpha=1, edgecolors='black')
  30. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement