Guest User

Untitled

a guest
Nov 20th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. # Split the dataset into two pieces: a training set and a testing set. Train the model on the training set. Test the model on the testing set, and evaluate how well we did.
  2. # STEP 1: split X and y into training and testing sets
  3. from sklearn.cross_validation import train_test_split
  4. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=4)
  5.  
  6. print(X_train.shape)
  7. print(X_test.shape)
  8. >> (90, 4) # 90 samples are selected randomly for training the algorithm
  9. >> (60, 4) # 60 samples are selected randomly for testing the algorithm
  10.  
  11. # STEP 2: train the model on the training set
  12. logreg = LogisticRegression()
  13. logreg.fit(X_train, y_train)
  14.  
  15. # STEP 3: make predictions on the testing set
  16. y_pred = logreg.predict(X_test)
  17.  
  18. # compare actual response values (y_test) with predicted response values (y_pred)
  19. print(metrics.accuracy_score(y_test, y_pred))
  20. >> 0.95
  21.  
  22. knn = KNeighborsClassifier(n_neighbors=5)
  23. knn.fit(X_train, y_train)
  24. y_pred = knn.predict(X_test)
  25. print(metrics.accuracy_score(y_test, y_pred))
  26. >> 0.966666666667
  27.  
  28. knn = KNeighborsClassifier(n_neighbors=1)
  29. knn.fit(X_train, y_train)
  30. y_pred = knn.predict(X_test)
  31. print(metrics.accuracy_score(y_test, y_pred))
  32. >> 0.95
Add Comment
Please, Sign In to add comment