Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.85 KB | None | 0 0
  1. import numpy as np
  2. import scipy as sp
  3. import matplotlib as plt
  4. import os as os
  5. import sys
  6. import glob
  7. from sklearn import svm
  8. from random import randint
  9. from sklearn import multiclass
  10.  
  11.  
  12. class MouseMovement:
  13. # Class for mouse movement data
  14. def __init__(self, startPixel, endPixel, listPixel=[], cl = None):
  15. # startPixel = [x_start, y_start, t_start]
  16. # endPixel = [x_end, y_end, t_end]
  17. # moveList = [x1, y1, t1;
  18. # x2, y2, t2;
  19. # x3, y3, t3;
  20. # ...
  21. # xn, yn, tn]
  22. self.start = startPixel
  23. self.end = endPixel
  24. self.moveList = listPixel
  25. self.cl = cl
  26. self.length = self.getMoveLength()
  27. self.moveTime = self.getMoveTime()
  28. # self.v = self.getSpeedVector()
  29.  
  30. def MMPrint(self):
  31. print (self.start)
  32. print (self.moveList)
  33. print (self.end)
  34.  
  35. def getMoveLength(self):
  36. tmp1 = self.end[0]-self.start[0]
  37. tmp2 = self.end[1]-self.start[1]
  38. length = np.sqrt(tmp1*tmp1+tmp2*tmp2)
  39.  
  40. return length
  41.  
  42.  
  43. def getMoveTime(self):
  44. tmpTime=self.end[2]
  45. for x in self.moveList:
  46. tmpTime += x[2]
  47.  
  48. return tmpTime
  49.  
  50. def getLongPause(self):
  51. list = []
  52. list.append(self.start)
  53. for i in range(0, len(self.moveList)):
  54. list.append(self.moveList[i])
  55. list.append(self.end)
  56. tmpTime = 0
  57. for i in range(1, len(list)):
  58. if np.abs(list[i][2] - list[i - 1][2]) > 1000:
  59. tmpTime += list[i][2]
  60. return tmpTime
  61. def getSpeedVector(self):
  62. l1 = np.append(np.transpose(self.start[0:2]), self.moveList[:,0:2])
  63. l2 = np.append(self.moveList[:,0:2], np.transpose(self.end[0:2]))
  64. # print l1
  65. l1=l1.reshape(len(l1)/2,2)
  66. l2=l2.reshape(len(l2)/2,2)
  67. t = np.maximum(np.append(np.transpose(self.start[2]), self.moveList[:,2]),np.ones(len(self.moveList[:,2])+1))
  68. diff = l2 - l1
  69. # print diff
  70. diff_sq = np.multiply(diff, diff)
  71. # print diff_sq
  72. dist = np.sqrt(diff_sq[:,0]+diff_sq[:,1])
  73. # print dist
  74. v = np.divide(dist,t)
  75. return v
  76.  
  77. def getAverageSpeed(self):
  78. n = len(self.moveList)
  79. avg = 0
  80. br = 0
  81. for i in range(1, n):
  82. tmp1 = self.moveList[i][0] - self.moveList[i - 1][0]
  83. tmp2 = self.moveList[i][1] - self.moveList[i - 1][1]
  84. tmp3 = self.moveList[i][2]
  85. br += np.sqrt((tmp1 * tmp1) + (tmp2 * tmp2)) / tmp3
  86. br += np.sqrt((self.moveList[n - 1][0] - self.end[0]) ** 2 + (self.moveList[n - 1][1] - self.end[1]) ** 2) / \
  87. self.end[2]
  88. avg = br / (n + 1)
  89. return avg
  90. def getElement(self, id, verify):
  91. strOut = '-'+str(id)+'-\n'
  92. if verify == 1:
  93. strOut += '-'+str(self.cl)+'-\n'
  94. else:
  95. strOut += '---\n'
  96. strOut += str(self.start) + '\n'
  97. for data in self.moveList:
  98. strOut += str(data) + '\n'
  99. strOut += str(self.end) + '\n'
  100.  
  101. return strOut
  102.  
  103.  
  104. moveClassList = []
  105.  
  106. # Path to data file
  107. data = str('hw2_mouse_data.txt')
  108. f = open(data,'r')
  109. cnt = 0
  110. tmp = '0'
  111. move_list = []
  112. y=[]
  113. # def feature_normalize(x):
  114. # x_norm=x
  115. # num_col=x.shape[1]
  116. # for i in range (1,num_col):
  117. # x_norm[:,i]=(x_norm[:,i]-np.mean(x[:,i]))/np.std(x[:,i])
  118. # return x_norm
  119. while tmp != '':
  120. # read new line
  121. tmp = f.readline()
  122.  
  123. if not tmp:
  124. # moveClassList.append(MouseMovement(move_list[0], move_list[-1], move_list[1:-1], cl))
  125. break
  126. if tmp[0] == '-':
  127. # save the data
  128. if cnt >0:
  129. moveClassList.append(MouseMovement(move_list[0], move_list[-1], move_list[1:-1], cl))
  130. cnt += 1
  131.  
  132. tmp = f.readline()
  133.  
  134. if tmp.find('Goran', 0, len(tmp)) >=0:
  135. cl = 0 #'Goran'
  136. y.append(cl)
  137.  
  138. if tmp.find('Gorjan', 0, len(tmp))>=0:
  139. cl = 1 #'Gorjan'
  140. y.append(cl)
  141.  
  142. if tmp.find('Ivan', 0, len(tmp))>=0:
  143. cl = 2 #'Ivan'
  144. y.append(cl)
  145.  
  146. if tmp.find('Georgi', 0, len(tmp))>=0:
  147. cl = 3 #'Georgi'
  148. y.append(cl)
  149.  
  150.  
  151. move_list = []
  152.  
  153. else:
  154. if tmp[0] == '[':
  155. data = tmp.replace('[','')
  156. data = data.replace(']','')
  157. data = data.replace('\n','')
  158.  
  159. value = [int(no) for no in data.split(",")]
  160. move_list.append(value)
  161.  
  162.  
  163. # print test data
  164. #print (moveClassList[3].getElement(4,0))
  165.  
  166. #print (moveClassList[3].getAverageSpeed())
  167.  
  168.  
  169.  
  170. # model = svm.SVC(C=10.0, kernel='rbf', degree=3, gamma=1, coef0=0.0, shrinking=True, probability=False,tol=0.001, cache_size=200, class_weight=None, verbose=False, max_iter=-1, random_state=5)
  171. # model.fit(x_train, y_train)
  172. # model.score(x_train, y_train)
  173. # predicted = model.predict(x_predict)
  174.  
  175.  
  176. # t1_str = str('./data_ready/hw2_mouse_prediction_fin.txt')
  177. # f1 = open(t1_str,'w')
  178. # t2_str = str('./data_ready/hw2_mouse_prediction-verify_fin.txt')
  179. # f2 = open(t2_str,'w')
  180. # cnt = 1
  181. #
  182. # while not (not moveClassList):
  183. # el = np.random.randint(0,len(moveClassList),1)
  184. # print el
  185. # f1.write(moveClassList[el].getElement(cnt, 0)) # test
  186. # f2.write(moveClassList[el].getElement(cnt, 1)) # verify
  187. # del moveClassList[el]
  188. # cnt += 1
  189. # print cnt
  190. #
  191. #
  192. moveClassList2 = []
  193. data_predict = str('hw2_mouse_prediction.txt')
  194. f2 = open(data_predict,'r')
  195. cnt2 = 0
  196. tmp2 = '0'
  197. move_list2 = []
  198.  
  199. while tmp2 != '':
  200. # read new line
  201. tmp2 = f2.readline()
  202. if not tmp2:
  203. moveClassList2.append(MouseMovement(move_list2[0], move_list2[-1], move_list2[1:-1]))
  204. break
  205. if tmp2[0] == '-':
  206. # save the data
  207. if cnt2 >0:
  208. moveClassList2.append(MouseMovement(move_list2[0], move_list2[-1], move_list2[1:-1]))
  209. cnt2 += 1
  210. tmp2 = f2.readline()
  211. move_list2 = []
  212. else:
  213. if tmp2[0] == '[':
  214. data_predict = tmp2.replace('[','')
  215. data_predict = data_predict.replace(']','')
  216. data_predict = data_predict.replace('\n','')
  217. value = [int(no) for no in data_predict.split(",")]
  218. move_list2.append(value)
  219. speeds = []
  220. moveLengths = []
  221. moveTimes = []
  222. print(len(moveClassList))
  223. for i in range(0, len(moveClassList)):
  224. speeds.append(moveClassList[i].getAverageSpeed())
  225. moveLengths.append((moveClassList[i].getMoveLength()))
  226. moveTimes.append((moveClassList[i].getMoveTime()))
  227. data_train = np.zeros((1151, 4))
  228. data_train[:, 0] = speeds
  229. data_train[:, 1] = moveLengths
  230. data_train[:, 2] = moveTimes
  231. data_train[:, 3]=y
  232.  
  233. for i in range(0,1151):
  234. rand = randint(0,1150)
  235. pom = data_train[rand,:]
  236. data_train[rand,:] = data_train[i,:]
  237. data_train[i,:] = pom
  238.  
  239. x_train = np.zeros((1151, 4))
  240. y_train = []
  241. x_train = data_train[:, :3]
  242. y_train = data_train[:, 3]
  243.  
  244. #x_test = np.zeros((231, 2))
  245. #y_test = []
  246. #x_test = data_train[921:, :2]
  247. #y_test = data_train[921:, 2]
  248. speedsP = []
  249. moveLengthsP = []
  250. moveTimesP = []
  251. #print(len(moveClassList2))
  252. for c in range(0, len(moveClassList2)):
  253. speedsP.append(moveClassList2[c].getAverageSpeed())
  254. moveLengthsP.append((moveClassList2[c].getMoveLength()))
  255. moveTimesP.append((moveClassList2[c].getMoveTime()))
  256. data_p = np.zeros((133, 3))
  257. data_p[:, 0] = speedsP
  258. data_p[:, 1] = moveLengthsP
  259. data_p[:, 2]=moveTimesP
  260.  
  261.  
  262. #data_train[:, 2] = y
  263.  
  264.  
  265. x_p = np.zeros((133, 3))
  266. x_p = data_p[:, :]
  267.  
  268.  
  269. classif = multiclass.OneVsRestClassifier(svm.SVC(C=1.0, kernel='rbf', degree=3, gamma=0.00005, coef0=0.0, shrinking=True, probability=False,tol=0.001, cache_size=200, class_weight=None, verbose=False, max_iter=-1, random_state=1))
  270. classif.fit(x_train, y_train)
  271. #score = classif.score(x_test, y_test)
  272. #predicted = classif.predict(x_test)
  273. #predict= classif.predict(x_p)
  274. #for i in range(0, len(predicted)):
  275. # print(predicted[i])
  276. #print("Procent pogodeni: ", score*100)
  277.  
  278. predict1 = classif.predict(x_p)
  279. eden=0
  280. dva=0
  281. tri=0
  282. nula=0
  283. for i in range(0, len(predict1)):
  284. if (predict1[i] == 0):
  285. nula += 1
  286. if (predict1[i] == 1):
  287. eden += 1
  288. if (predict1[i] == 2):
  289. dva += 1
  290. if (predict1[i] == 3):
  291. tri += 1
  292. #print(predict1[i])
  293.  
  294. for i in range(0,133):
  295. if predict1[i] == 0:
  296. moveClassList2[i].cl = 'Goran'
  297. if predict1[i] == 1:
  298. moveClassList2[i].cl = 'Gorjan'
  299. if predict1[i] == 2:
  300. moveClassList2[i].cl = 'Ivan'
  301. if predict1[i] == 3:
  302. moveClassList2[i].cl = 'Georgi'
  303. for i in range(0,133):
  304. with open("hw_mouse_prediction_006_2013.txt", "a") as myfile:
  305. myfile.write(moveClassList2[i].getElement(i+1,1))
  306. print(nula)
  307. print(eden)
  308. print(dva)
  309. print(tri)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement