fork download
  1. # load the iris dataset as an example
  2. from sklearn.datasets import load_iris
  3. iris = load_iris()
  4.  
  5. # store the feature matrix (X) and response vector (y)
  6. X = iris.data
  7. y = iris.target
  8.  
  9. # splitting X and y into training and testing sets
  10. from sklearn.model_selection import train_test_split
  11. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=1)
  12.  
  13. # training the model on training set
  14. from sklearn.neighbors import KNeighborsClassifier
  15. knn = KNeighborsClassifier(n_neighbors=3)
  16. knn.fit(X_train, y_train)
  17.  
  18. # making predictions on the testing set
  19. y_pred = knn.predict(X_test)
  20.  
  21. # comparing actual response values (y_test) with predicted response values (y_pred)
  22. from sklearn import metrics
  23. print("kNN model accuracy:", metrics.accuracy_score(y_test, y_pred))
  24.  
  25. # making prediction for out of sample data
  26. sample = [[3, 5, 4, 2], [2, 3, 5, 4]]
  27. preds = knn.predict(sample)
  28. pred_species = [iris.target_names[p] for p in preds]
  29. print("Predictions:", pred_species)
Success #stdin #stdout 3.73s 112968KB
stdin
Standard input is empty
stdout
kNN model accuracy: 0.9833333333333333
Predictions: ['versicolor', 'virginica']