No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tp3.py 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #!/usr/bin/env python3
  4. # -*- coding: utf-8 -*-
  5. import matplotlib.pyplot as plt
  6. from sklearn.neural_network import MLPClassifier
  7. from sklearn.model_selection import train_test_split
  8. from sklearn.model_selection import KFold
  9. from sklearn.metrics import precision_score
  10. from sklearn.metrics import confusion_matrix
  11. from sklearn.svm import SVC
  12. import random
  13. import time
  14. """
  15. from sklearn.datasets import fetch_openml
  16. mnist = fetch_openml('mnist_784')
  17. """
  18. """
  19. images = mnist.data.values.reshape((-1, 28, 28))
  20. plt.imshow(images[0],cmap=plt.cm.gray_r,interpolation="nearest")
  21. plt.show()
  22. print(mnist.target[0])
  23. """
  24. """
  25. indices = [i for i in range(len(mnist.data))]
  26. random.shuffle(indices)
  27. indices = indices[:15000]
  28. data = [mnist.data.values[i] for i in indices]
  29. target = [mnist.target[i] for i in indices]
  30. """
  31. xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.7)
  32. clf = SVC()
  33. clf.fit(xtrain, ytrain)
  34. ypred = clf.predict(xtest)
  35. print(confusion_matrix(ytest, ypred))
  36. """
  37. print(target[4])
  38. print(clf.predict(data[4].reshape(1,-1)))
  39. plt.imshow(data[4].reshape(28, 28), cmap=plt.cm.gray_r, interpolation="nearest")
  40. plt.show()
  41. """
  42. """
  43. kernels = ["linear", "poly", "rbf", "sigmoid"]
  44. for k in kernels:
  45. print(k + ":")
  46. start_time = time.time()
  47. clf = SVC(kernel = k)
  48. clf.fit(xtrain, ytrain)
  49. print(f"training time = {time.time() - start_time}, score = {clf.score(xtest, ytest)}")
  50. """
  51. """
  52. scores = []
  53. times = []
  54. tolerance=[i*0.1 for i in range(1,10,2)]
  55. for C in tolerance:
  56. print(f"C = {C} :")
  57. start_time = time.time()
  58. clf = SVC(C = C)
  59. clf.fit(xtrain, ytrain)
  60. training_time = time.time() - start_time
  61. score = clf.score(xtest, ytest)
  62. times += [training_time]
  63. scores += [score]
  64. plt.plot(tolerance, scores)
  65. plt.show()
  66. plt.plot(tolerance, times)
  67. """