Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import matplotlib as mpl
  5. import time
  6. import itertools
  7. import pickle
  8. import IPython
  9.  
  10. #from sklearn.mixture import gmm
  11. #from sklearn import mixture
  12. from sklearn import svm, datasets
  13. from sklearn.externals.six.moves import xrange
  14. from sklearn.mixture import GMM
  15. from scipy import linalg
  16. from mpl_toolkits.mplot3d import Axes3D
  17. from colorsys import rgb_to_hsv
  18.  
  19.  
  20. if __name__ == "__main__":
  21.  
  22. HS = pickle.load(open("HS.p", "rb"));
  23. y = pickle.load(open("yHS.p", "rb"));
  24.  
  25. #small subset here first
  26. X = HS
  27. X_tmp = X[0:9] + X[118:128] + X[245:255] + X[376:386]
  28. X = X_tmp
  29. y = y
  30. y_tmp = y[0:9] + y[118:128] + y[245:255] + y[376:386]
  31. y = y_tmp
  32. X_test = HS
  33. y_test = y
  34.  
  35. X = np.array(X)
  36. y = np.array(y)
  37.  
  38.  
  39.  
  40. n_classes = len(np.unique(y))
  41. #num_images_array = [118, 127, 131, 81]
  42. num_images_array = [10, 10, 10, 10]
  43.  
  44.  
  45. h = .02 # step size in the mesh
  46.  
  47. # # we create an instance of SVM and fit out data. We do not scale our
  48. # # data since we want to plot the support vectors
  49. C = .1 # SVM regularization parameter
  50.  
  51.  
  52. #svc = svm.SVC(kernel='linear', C=C).fit(X, y)
  53. #rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=C).fit(X, y)
  54. #poly_svc = svm.SVC(kernel='poly', degree=3, C=C).fit(X, y)
  55. lin_svc = svm.LinearSVC(C=C).fit(X, y)
  56.  
  57. # # create a mesh to plot in
  58. x_min = X[:, 0].min() - 1
  59. x_max = X[:, 0].max() + 1
  60. y_min = X[:, 1].min() - 1
  61. y_max = X[:, 1].max() + 1
  62. xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
  63. np.arange(y_min, y_max, h))
  64.  
  65. # title for the plots
  66. titles = ['LinearSVC (linear kernel)']
  67.  
  68.  
  69.  
  70.  
  71.  
  72. # construct list for different SVMs to try
  73. #classifiers = (svc, rbf_svc, poly_svc, lin_svc)
  74. classifiers = (lin_svc)
  75.  
  76. #n_classifiers = len(classifiers)
  77. n_classifiers = 1;
  78.  
  79.  
  80. # for i, clf in enumerate(classifiers):
  81. # # Since we have class labels for the training data, we can
  82. # # initialize the GMM parameters in a supervised manner.
  83. # classifier.means_ =np.array([]);
  84. # counter = 0;
  85. # for i in range(len(colors)):
  86. # if i == 0:
  87. # data = np.array(X_train[0:num_images_array[i]])
  88. # classifier.means_ = data.mean(axis=0)
  89. # else:
  90. # # print sum(num_images_array[:i-1])
  91. # # print sum(num_images_array[:i-1])+num_images_array[i]
  92. # data = np.array(X_train[counter:counter+num_images_array[i]])
  93. # classifier.means_=np.vstack((classifier.means_, data.mean(axis=0)))
  94. # counter = counter + num_images_array[i];
  95. # # Train the other parameters using the EM algorithm.
  96. # classifier.fit(X_train)
  97.  
  98. # h = plt.subplot(2, n_classifiers / 2, index + 1)
  99. # make_ellipses(classifier, h, colors)
  100.  
  101.  
  102.  
  103. # Plot the decision boundary. For that, we will assign a color to each
  104. # point in the mesh [x_min, m_max]x[y_min, y_max].
  105. #plt.subplot(2, 2, i + 1)
  106. #plt.subplots_adjust(wspace=0.4, hspace=0.4)
  107.  
  108. clf = lin_svc
  109.  
  110. Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
  111.  
  112. # Put the result into a color plot
  113. Z = Z.reshape(xx.shape)
  114. plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)
  115.  
  116. # Plot also the training points
  117. plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)
  118. plt.xlabel('H')
  119. plt.ylabel('S')
  120. plt.xlim(xx.min(), xx.max())
  121. plt.ylim(yy.min(), yy.max())
  122. plt.xticks(())
  123. plt.yticks(())
  124. plt.title(titles[i])
  125.  
  126. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement