SHARE
TWEET

Untitled

a guest Aug 22nd, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Create KNN
  2. knn_model = KNeighborsClassifier()
  3.  
  4. # Number of neighbors
  5. n_neighbors = range(3, 5, 10)
  6.  
  7. # Create regularization term space
  8. weights = ["uniform", "distance"]
  9.  
  10. # Create distance metrics space
  11. dist_metrics = [1, 2]
  12.  
  13. # Create hyperparameters
  14. hyperparameters = dict(n_neighbors = n_neighbors, weights = weights, p = dist_metrics)
  15.  
  16. # Create GridSearch
  17. knn_gs = GridSearchCV(knn_model, hyperparameters, cv = 5, scoring = "f1_micro")
  18.  
  19. # Fit GridSearch
  20. model_knn = knn_gs.fit(X_train, y_train)
  21.  
  22. # Model best estimators
  23. print("Number of Neighbors: ", model_knn.best_estimator_.get_params()["n_neighbors"])
  24. print("Weighting Type: ", model_knn.best_estimator_.get_params()["weights"])
  25. print("Distance Metric: ", model_knn.best_estimator_.get_params()["p"])
  26.  
  27. # Predictions
  28. y_train_pred_knn = model_knn.best_estimator_.predict(X_train)
  29. y_test_pred_knn = model_knn.best_estimator_.predict(X_test)
  30.  
  31. # Print F1 scores
  32. print("Training F1 Micro Average: ", f1_score(y_train, y_train_pred_knn, average = "micro"))
  33. print("Test F1 Micro Average: ", f1_score(y_test, y_test_pred_knn, average = "micro"))
  34.  
  35. # Confusion Matrix
  36. knn_cm = confusion_matrix(y_test, y_test_pred_knn)
  37. print(knn_cm)
  38.  
  39. plt.figure(figsize = (9, 9))
  40. sns.heatmap(knn_cm, annot = True, fmt = ".3f", linewidths = .5, square = True, cmap = "BuPu")
  41.  
  42. # Classification Report
  43. print(classification_report(y_test, y_test_pred_knn))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top