Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. import itertools
  2. import os
  3. import scipy.io as sio
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. from sklearn import svm
  7. from sklearn.metrics import confusion_matrix
  8. from plot_confusion_matrix import plot_confusion_matrix
  9.  
  10. rootdir ='data'
  11. samples = [] # data
  12. y = [] # labels
  13. X = [] # features
  14. isTrain = [] # test (0) or train (1) sample
  15.  
  16. # --- read data
  17. for subdir, dirs, files in os.walk(rootdir):
  18. for file in files:
  19. data = sio.loadmat(rootdir + '/' + file)['d_skel']
  20. samples.append(data)
  21. action = int(file.split('_')[0][1:])
  22. y.append(action)
  23. subject = int(file.split('_')[1][1:])
  24. isTrain.append(subject % 2)
  25.  
  26.  
  27. def for_dimension(vector):
  28. mean = np.mean(vector)
  29. rms = np.sqrt(np.mean(vector ** 2))
  30. vel = sum(abs(np.diff(vector)))
  31. acc = sum(abs(np.diff(np.diff(vector))))
  32. return [mean, rms, vel, acc]
  33. # a = [element for element in itertools.product([mean, rms], [vel, acc])]
  34. # return list(itertools.chain(*a))
  35.  
  36.  
  37. # --- compute features
  38. for sample in samples:
  39. features = [] # 20 joints in data
  40. for i in range(0, 20): # i - joints
  41. xxx = for_dimension(sample[i, 0, :40])
  42. yyy = for_dimension(sample[i, 1, :40])
  43. zzz = for_dimension(sample[i, 2, :40])
  44.  
  45. xxx2 = for_dimension(sample[i, 0, 30:])
  46. yyy2 = for_dimension(sample[i, 1, 30:])
  47. zzz2 = for_dimension(sample[i, 2, 30:])
  48.  
  49. features.append(xxx + yyy + zzz + xxx2 + yyy2 + zzz2)
  50.  
  51. features = [f1 for f2 in features for f1 in f2]
  52. features /= sum(features)
  53. X.append(features)
  54.  
  55. # --- plot
  56. for idx in range(0,9):
  57. plt.subplot(331 + idx)
  58. plt.bar(np.arange(0,240 * 2), X[idx * 16])
  59. plt.show()
  60.  
  61. # --- convert to arrays and split to train and test data
  62. isTrain = np.asarray(isTrain)
  63. X = np.asarray(X)
  64. y = np.asarray(y)
  65. X_train = X[(isTrain == 1)]
  66. X_test = X[(isTrain == 0)]
  67. y_train = y[(isTrain == 1)]
  68. y_test = y[(isTrain == 0)]
  69.  
  70. # --- classify
  71. clf = svm.SVC(gamma='scale')
  72. clf.fit(X_train, y_train)
  73. results = clf.predict(X_test)
  74. # print(results)
  75.  
  76. incorrect = len(np.nonzero(results-y_test)[0])
  77. total = len(y_test)
  78. correct = total - incorrect
  79. accuracy = correct / total
  80. print(accuracy)
  81.  
  82. # --- confusion matrix
  83. cm = confusion_matrix(y_test, results)
  84. # print(cm)
  85.  
  86. classes = np.arange(0, 27)
  87. plot_confusion_matrix(y_test, results, classes=classes, normalize=True, title='Confusion matrix')
  88. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement