Advertisement
sserban21

pb2Bia

Mar 21st, 2023
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. """
  2. Use the digits dataset to perform a classification task using Linear Support Vector Machine(SVM).
  3. Set the parameter C to 5.
  4. Split the data such the test split is 25% of the initial dataset.
  5. Compare the image classification task using the original number of features (64) to the reduced features (20) using the PCA algorithm.
  6. Compare:
  7. - the training time for both models
  8. - the difference in accuracy score
  9. """
  10. import time
  11. from sklearn import datasets
  12. from sklearn.model_selection import train_test_split
  13. from sklearn.svm import LinearSVC
  14. from sklearn.decomposition import PCA
  15.  
  16. digits = datasets.load_digits()
  17. X = digits.data
  18. y = digits.target # 0-9 asta e unidisimensional
  19.  
  20.  
  21. # Split the data such the test split is 25% of the initial dataset
  22. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
  23.  
  24. # Train the model using the original number of features (64)
  25. # daca loss nu e definit, pot sa il las gol
  26. svm_clf1 = LinearSVC(C=5, random_state=42)
  27.  
  28. # start the timer
  29. start_time = time.time()
  30.  
  31. # Train the model using the original number of features (64)
  32. svm_clf1.fit(X_train, y_train)
  33.  
  34. # end the timer
  35. end_time = time.time()
  36.  
  37. # Print the accuracy score and the time elapsed
  38. print("1 - Accuracy score:",svm_clf1.score(X_test, y_test))
  39. print("1 - Time elapsed: ", end_time - start_time)
  40.  
  41. # reduce the number of features to 20 using PCA
  42. pca = PCA(n_components=20)
  43.  
  44. # Train the model using the reduced number of features (20)
  45. X_train_reduced = pca.fit_transform(X_train)
  46. X_test_reduced = pca.fit_transform(X_test)
  47.  
  48. svm_clf2 = LinearSVC(C=5, random_state=42)
  49.  
  50. # start the timer
  51. start_time = time.time()
  52.  
  53. # Train the model using the reduced number of features (20)
  54. svm_clf2.fit(X_train_reduced, y_train)
  55. # end the timer
  56. end_time = time.time()
  57.  
  58. print("Shape: ",y_test.shape)
  59. # Print the accuracy score and the time elapsed
  60. print("2 - Accuracy score:", svm_clf2.score(X_test_reduced, y_test))
  61. print("2 - Time elapsed: ", end_time - start_time)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement