Advertisement
fake_world

ml9

Dec 3rd, 2020
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. from sklearn.model_selection import train_test_split
  2. from sklearn.neighbors import KNeighborsClassifier
  3. #from sklearn.naive_bayes import GaussianNB
  4. from sklearn import datasets
  5.  
  6. iris=datasets.load_iris()
  7. print("Iris Data set loaded...")
  8.  
  9. x_train, x_test, y_train, y_test = train_test_split(iris.data,iris.target,test_size=0.3)
  10. #random_state=0
  11. print("Dataset is split into training and testing samples...")
  12. print("Size of trainng data and its label",x_train.shape,y_train.shape)
  13. print("Size of trainng data and its label",x_test.shape, y_test.shape)
  14. for i in range(len(iris.target_names)):
  15.     print("Label", i , "-",str(iris.target_names[i]))
  16. classifier = KNeighborsClassifier(n_neighbors=1)
  17. #gnb = GaussianNB()
  18. #y_pred = gnb.fit(x_train, y_train).predict(x_test)
  19. #print("Number of mislabeled points out of a total %d points : %d" %(x_test.shape[0], (y_test != y_pred).sum()))
  20. classifier.fit(x_train, y_train)
  21. y_pred=classifier.predict(x_test)
  22.  
  23. print("Results of Classification using K-nn with K=1 ")
  24. for r in range(0,len(x_test)):
  25.     print(" Sample:", str(x_test[r]), " Actual-label:", str(y_test[r]), " Predicted-label:", str(y_pred[r]))
  26.  
  27. print("Classification Accuracy :" , classifier.score(x_test,y_test)*100);
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement