Advertisement
makispaiktis

ML - Lab 5 - SVM

Oct 19th, 2022 (edited)
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. from sklearn import svm
  5. from sklearn.metrics import accuracy_score
  6. from math import floor
  7.  
  8. # Read data
  9. alldata = pd.read_csv("./alldata.txt")
  10. print("alldata = ")
  11. print(alldata)
  12. print()
  13. print("alldata summary: ")
  14. print(alldata.describe())
  15. print()
  16.  
  17. # Training, testing
  18. N = len(alldata)
  19. stop = floor(0.75*N)
  20. xtrain = alldata.loc[0:stop-1, ["X1", "X2"]]
  21. ytrain = alldata.loc[0:stop-1, "y"]
  22. xtest = alldata.loc[stop:N, ["X1", "X2"]]
  23. ytest = alldata.loc[stop:N, "y"]
  24.  
  25. # Display Data
  26. plt.figure()
  27. plt.scatter(alldata[alldata.y == 1].X1, alldata[alldata.y == 1].X2, color="blue", marker="o", label="1")
  28. plt.scatter(alldata[alldata.y == 2].X1, alldata[alldata.y == 2].X2, color="red", marker="+", label="1")
  29. plt.title("Data Points")
  30. plt.xlabel("X1")
  31. plt.ylabel("X2")
  32. plt.legend()
  33. plt.show()
  34.  
  35.  
  36.  
  37. # SVM Classifier
  38. # Grid for hyperplanes
  39. plt.figure()
  40. plt.scatter(alldata[alldata.y == 1].X1, alldata[alldata.y == 1].X2, color="blue", marker="o", label="1")
  41. plt.scatter(alldata[alldata.y == 2].X1, alldata[alldata.y == 2].X2, color="red", marker="+", label="1")
  42. plt.title("SVM Classification")
  43. X1 = np.arange (min(xtrain.X1.tolist()), max(xtrain.X1.tolist()), 0.01)
  44. X2 = np.arange (min(xtrain.X2.tolist()), max(xtrain.X2.tolist()), 0.01)
  45. xx, yy = np.meshgrid(X1, X2)
  46.  
  47. # Create the SVM classifier and apply to the grid's points 'xx' and 'yy'
  48. # Gamma = 1
  49. clf = svm.SVC(kernel="rbf", gamma=1)
  50. clf = clf.fit(xtrain, ytrain)
  51. pred = clf.predict(np.c_[xx.ravel(), yy.ravel()])
  52. pred = pred.reshape(xx.shape)
  53. plt.contour(xx, yy, pred, colors="blue")
  54. ''''
  55. # Gamma = 0.01
  56. clf = svm.SVC(kernel="rbf", gamma=0.01)
  57. clf = clf.fit(xtrain, ytrain)
  58. pred = clf.predict(np.c_[xx.ravel(), yy.ravel()])
  59. pred = pred.reshape(xx.shape)
  60. plt.contour(xx, yy, pred, colors="red")
  61. # Gamma = 100
  62. clf = svm.SVC(kernel="rbf", gamma=100)
  63. clf = clf.fit(xtrain, ytrain)
  64. pred = clf.predict(np.c_[xx.ravel(), yy.ravel()])
  65. pred = pred.reshape(xx.shape)
  66. plt.contour(xx, yy, pred, colors="green")
  67. '''
  68. plt.show()
  69.  
  70.  
  71.  
  72.  
  73.  
  74. # SVM Classifiers for different values of gamma
  75. gammavalues = [10**i for i in range(-2, 6)]
  76. trainingError = []
  77. testingError = []
  78. for gamma in gammavalues:
  79.     clf = svm.SVC(kernel="rbf", gamma=gamma)
  80.     clf = clf.fit(xtrain, ytrain)
  81.     pred = clf.predict(xtrain)
  82.     trainingError.append(1 - accuracy_score(ytrain, pred))
  83.     pred = clf.predict(xtest)
  84.     testingError.append(1 - accuracy_score(ytest, pred))
  85.  
  86. # Plotting the training and testing error for different values of gamma
  87. plt.figure()
  88. plt.plot(trainingError, c="blue")
  89. plt.plot(testingError, c="red")
  90. plt.ylim(0, 0.5)
  91. plt.xticks(range(len(gammavalues)), gammavalues)
  92. plt.legend(["Training Error", "Testing Error"])
  93. plt.xlabel("Gamma")
  94. plt.ylabel("Error")
  95. plt.show()
  96.  
  97.  
  98.  
  99. # Find the best value for gamma - k-fold Cross Validation
  100. from sklearn.model_selection import cross_val_score
  101. accuracies = []
  102. for gamma in gammavalues:
  103.     clf = svm.SVC(kernel="rbf", gamma=gamma)
  104.     scores = cross_val_score(clf, xtrain, ytrain, cv=10)
  105.     accuracies.append(scores.mean())
  106. print(accuracies)
  107. print("Best gamma: ", gammavalues[np.argmax(accuracies)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement