Guest User

Untitled

a guest
Jan 21st, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. import numpy as np
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.linear_model import LogisticRegression
  4. from sklearn.neighbors import KNeighborsClassifier
  5. from sklearn.naive_bayes import GaussianNB
  6. from sklearn.model_selection import cross_val_score
  7. from sklearn.preprocessing import PolynomialFeatures
  8. from sklearn.pipeline import make_pipeline
  9. import matplotlib.pyplot as plt
  10. from utils import plot_classification_dataset, plot_2d_decisionboundary
  11.  
  12.  
  13.  
  14. def predictWithKNN(X, y):
  15. print("KNN:")
  16. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
  17.  
  18. max_train_score = (0, 0)
  19. max_test_score = (0, 0)
  20. for k in range(5, 16):
  21. model = KNeighborsClassifier(n_neighbors=k, algorithm='ball_tree')
  22.  
  23. model.fit(X_train, y_train)
  24.  
  25. train_score = model.score(X_train, y_train)
  26. test_score = model.score(X_test, y_test)
  27.  
  28. if train_score > max_train_score[1]:
  29. max_train_score = (k, train_score)
  30. if test_score > max_test_score[1]:
  31. max_test_score = (k, test_score)
  32.  
  33. print("MAX Train-Score for k={0}: {1}".format(max_train_score[0], max_train_score[1]))
  34. print("MAX Test-Score for k={0}: {1}".format(max_test_score[0], max_test_score[1]))
  35.  
  36. # continue with best test k val
  37. k = max_test_score[0];
  38. model = KNeighborsClassifier(n_neighbors=k, algorithm='ball_tree')
  39. scores = cross_val_score(model, X, y, cv=5)
  40. print("Mean: {0:.4f} (+/- {1:.4f})".format(scores.mean(), scores.std() * 2))
  41.  
  42.  
  43.  
  44.  
  45. def predictWithOneVsRestLogisticRegression(X, y):
  46. print("LogisticRegressor OVR")
  47. model = LogisticRegression(solver='lbfgs',multi_class='ovr',C = 1)
  48. scores = cross_val_score(model, X, y, cv=5)
  49. print("Mean: {0:.4f} (+/- {1:.4f})".format(scores.mean(), scores.std() * 2))
  50.  
  51. def predictWithNaiveBayes(X, y):
  52. print("\nGauss:")
  53. model = GaussianNB();
  54.  
  55. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
  56.  
  57. model.fit(X_train, y_train)
  58.  
  59. scores = cross_val_score(model, X, y, cv=5)
  60. print("Mean: {0:.4f} (+/- {1:.4f})".format(scores.mean(), scores.std() * 2))
  61.  
  62.  
  63.  
  64.  
  65. if __name__ == "__main__":
  66. data = np.load('data11_1.npz')
  67.  
  68.  
  69. # only using features 1 to 4 as the last feature seems to be noisy and
  70. # probably not relevant for the classification
  71. X, y = data['X'][:,:4], data['y']
  72. X2 = X[:,2:4]
  73. X3 = X[:,3].reshape(-1, 1) # reshape as model expects 2 dim
  74.  
  75.  
  76. print("\nFeature 1 to 4:")
  77. predictWithKNN(X, y)
  78. predictWithNaiveBayes(X, y)
  79. predictWithOneVsRestLogisticRegression(X, y)
  80.  
  81. print("\nFeature 3 and 4")
  82. predictWithKNN(X2, y)
  83. predictWithNaiveBayes(X2, y)
  84. predictWithOneVsRestLogisticRegression(X2, y)
  85.  
  86. print("\nFeature 4")
  87. # using only the last feature
  88. predictWithKNN(X3, y)
  89. predictWithNaiveBayes(X3, y)
  90. predictWithOneVsRestLogisticRegression(X3, y)
Add Comment
Please, Sign In to add comment