ivanakarpuzova

ribi

Sep 10th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.16 KB | None | 0 0
  1. 6 Class Fish Species
  2. Класата (видот на рибата) е дадена во последната колона.
  3.  
  4.  
  5. Да се направи модел за класификација за
  6. даденото податочно множество. За тренинг
  7. да се земат само првите 25 примероци од класите
  8. Bream и Perch (Првите 25 од Bream и првите 25 од Perch или вкупно 50).
  9. Значи треба да се направи бинарен класификатор, при што секоја класа освен _Bream_ и _Perch_
  10. се игнорира при тренирањето на множеството. Притоа земањето на првите 25 примероци да се направи
  11. во програмата, а не со рачно копирање!
  12.  
  13. Да се класифицира елементот даден на влез и да се испечати предвидувањето. На влез се добива цела
  14. листа која директно може да се прочита со една input() наредба и потоа може да класифицира без никави
  15. кастирања и рачно парсирање
  16.  
  17.  
  18.  
  19.  
  20.  
  21. data = [[180.0, 23.6, 25.2, 27.9, 25.4, 14.0, 'Roach'],
  22. [12.2, 11.5, 12.2, 13.4, 15.6, 10.4, 'Smelt'],
  23. [135.0, 20.0, 22.0, 23.5, 25.0, 15.0, 'Perch'],
  24. [1600.0, 56.0, 60.0, 64.0, 15.0, 9.6, 'Pike'],
  25. [120.0, 20.0, 22.0, 23.5, 26.0, 14.5, 'Perch'],
  26. [273.0, 23.0, 25.0, 28.0, 39.6, 14.8, 'Silver Bream'],
  27. [320.0, 27.8, 30.0, 31.6, 24.1, 15.1, 'Perch'],
  28. [160.0, 21.1, 22.5, 25.0, 25.6, 15.2, 'Roach'],
  29. [700.0, 30.4, 33.0, 38.3, 38.8, 13.8, 'Bream'],
  30. [500.0, 29.5, 32.0, 37.3, 37.3, 13.6, 'Bream'],
  31. [290.0, 24.0, 26.3, 31.2, 40.0, 13.8, 'Bream'],
  32. [650.0, 31.0, 33.5, 38.7, 37.4, 14.8, 'Bream'],
  33. [500.0, 26.8, 29.7, 34.5, 41.1, 15.3, 'Bream'],
  34. [260.0, 25.4, 27.5, 28.9, 24.8, 15.0, 'Perch'],
  35. [80.0, 17.2, 19.0, 20.2, 27.9, 15.1, 'Perch'],
  36. [850.0, 32.8, 36.0, 41.6, 40.6, 14.9, 'Bream'],
  37. [345.0, 36.0, 38.5, 41.0, 15.6, 9.7, 'Pike'],
  38. [567.0, 43.2, 46.0, 48.7, 16.0, 10.0, 'Pike'],
  39. [55.0, 13.5, 14.7, 16.5, 41.5, 14.1, 'Silver Bream'],
  40. [78.0, 16.8, 18.7, 19.4, 26.8, 16.1, 'Perch'],
  41. [950.0, 38.0, 41.0, 46.5, 37.9, 13.7, 'Bream'],
  42. [306.0, 25.6, 28.0, 30.8, 28.5, 15.2, 'Whitewish'],
  43. [6.7, 9.3, 9.8, 10.8, 16.1, 9.7, 'Smelt'],
  44. [714.0, 32.7, 36.0, 41.5, 39.8, 14.1, 'Bream'],
  45. [197.0, 23.5, 25.6, 27.0, 24.3, 15.7, 'Perch'],
  46. [1000.0, 41.1, 44.0, 46.6, 26.8, 16.3, 'Perch'],
  47. [685.0, 34.0, 36.5, 39.0, 27.9, 17.6, 'Perch'],
  48. [169.0, 22.0, 24.0, 27.2, 27.7, 14.1, 'Roach'],
  49. [125.0, 19.0, 21.0, 22.5, 25.3, 16.3, 'Perch'],
  50. [1000.0, 33.5, 37.0, 42.6, 44.5, 15.5, 'Bream'],
  51. [900.0, 36.5, 39.0, 41.4, 26.9, 18.1, 'Perch'],
  52. [19.7, 13.2, 14.3, 15.2, 18.9, 13.6, 'Smelt'],
  53. [150.0, 20.4, 22.0, 24.7, 23.5, 15.2, 'Roach'],
  54. [120.0, 17.5, 19.0, 21.3, 39.4, 13.7, 'Silver Bream'],
  55. [140.0, 19.0, 20.7, 23.2, 36.8, 14.2, 'Silver Bream'],
  56. [290.0, 24.0, 26.0, 29.2, 30.4, 15.4, 'Roach'],
  57. [725.0, 31.8, 35.0, 40.9, 40.0, 14.8, 'Bream'],
  58. [1000.0, 40.2, 43.5, 46.0, 27.4, 17.7, 'Perch'],
  59. [188.0, 22.6, 24.6, 26.2, 25.7, 15.9, 'Perch'],
  60. [242.0, 23.2, 25.4, 30.0, 38.4, 13.4, 'Bream'],
  61. [475.0, 28.4, 31.0, 36.2, 39.4, 14.1, 'Bream'],
  62. [700.0, 30.4, 33.0, 38.5, 38.8, 13.5, 'Bream'],
  63. [120.0, 18.6, 20.0, 22.2, 28.0, 16.1, 'Roach'],
  64. [820.0, 36.6, 39.0, 41.3, 30.1, 17.8, 'Perch'],
  65. [540.0, 28.5, 31.0, 34.0, 31.6, 19.3, 'Whitewish'],
  66. [150.0, 20.5, 22.5, 24.0, 28.3, 15.1, 'Perch'],
  67. [161.0, 22.0, 23.4, 26.7, 25.9, 13.6, 'Roach'],
  68. [60.0, 14.3, 15.5, 17.4, 37.8, 13.3, 'Silver Bream'],
  69. [840.0, 32.5, 35.0, 37.3, 30.8, 20.9, 'Perch'],
  70. [300.0, 24.0, 26.0, 29.0, 39.2, 14.6, 'Silver Bream'],
  71. [300.0, 25.2, 27.3, 28.7, 29.0, 17.9, 'Perch'],
  72. [180.0, 23.0, 25.0, 26.5, 24.3, 13.9, 'Perch'],
  73. [85.0, 18.2, 20.0, 21.0, 24.2, 13.2, 'Perch'],
  74. [130.0, 20.5, 22.5, 24.0, 24.4, 15.1, 'Perch'],
  75. [900.0, 37.0, 40.0, 42.5, 27.6, 17.0, 'Perch'],
  76. [9.9, 11.3, 11.8, 13.1, 16.9, 8.9, 'Smelt'],
  77. [620.0, 31.5, 34.5, 39.7, 39.1, 13.3, 'Bream'],
  78. [720.0, 32.0, 35.0, 40.6, 40.3, 15.0, 'Bream'],
  79. [270.0, 23.6, 26.0, 28.7, 29.2, 14.8, 'Whitewish'],
  80. [40.0, 13.8, 15.0, 16.0, 23.9, 15.2, 'Perch'],
  81. [5.9, 7.5, 8.4, 8.8, 24.0, 16.0, 'Perch'],
  82. [115.0, 19.0, 21.0, 22.5, 26.3, 14.7, 'Perch'],
  83. [110.0, 20.0, 22.0, 23.5, 23.5, 17.0, 'Perch'],
  84. [300.0, 26.9, 28.7, 30.1, 25.2, 15.4, 'Perch'],
  85. [363.0, 26.3, 29.0, 33.5, 38.0, 13.3, 'Bream'],
  86. [690.0, 34.6, 37.0, 39.3, 26.9, 16.2, 'Perch'],
  87. [820.0, 37.1, 40.0, 42.5, 26.2, 15.6, 'Perch'],
  88. [19.9, 13.8, 15.0, 16.2, 18.1, 11.6, 'Smelt'],
  89. [40.0, 12.9, 14.1, 16.2, 25.6, 14.0, 'Roach'],
  90. [390.0, 27.6, 30.0, 35.0, 36.2, 13.4, 'Bream'],
  91. [1250.0, 52.0, 56.0, 59.7, 17.9, 11.7, 'Pike'],
  92. [87.0, 18.2, 19.8, 22.2, 25.3, 14.3, 'Roach'],
  93. [9.8, 10.7, 11.2, 12.4, 16.8, 10.3, 'Smelt'],
  94. [13.4, 11.7, 12.4, 13.5, 18.0, 9.4, 'Smelt'],
  95. [975.0, 37.4, 41.0, 45.9, 40.6, 14.7, 'Bream'],
  96. [1100.0, 39.0, 42.0, 44.6, 28.7, 15.4, 'Perch'],
  97. [130.0, 20.0, 22.0, 23.5, 26.0, 15.0, 'Perch'],
  98. [450.0, 27.6, 30.0, 35.1, 39.9, 13.8, 'Bream'],
  99. [200.0, 30.0, 32.3, 34.8, 16.0, 9.7, 'Pike'],
  100. [340.0, 23.9, 26.5, 31.1, 39.8, 15.1, 'Bream'],
  101. [700.0, 34.0, 36.0, 38.3, 27.7, 17.6, 'Perch'],
  102. [170.0, 21.5, 23.5, 25.0, 25.1, 14.9, 'Perch'],
  103. [500.0, 29.1, 31.5, 36.4, 37.8, 12.0, 'Bream'],
  104. [150.0, 18.4, 20.0, 22.4, 39.7, 14.7, 'Silver Bream'],
  105. [145.0, 20.7, 22.7, 24.2, 24.6, 15.0, 'Perch'],
  106. [85.0, 17.8, 19.6, 20.8, 24.7, 14.6, 'Perch'],
  107. [600.0, 29.4, 32.0, 37.2, 40.2, 13.9, 'Bream'],
  108. [300.0, 34.8, 37.3, 39.8, 15.8, 10.1, 'Pike'],
  109. [456.0, 40.0, 42.5, 45.5, 16.0, 9.5, 'Pike'],
  110. [540.0, 40.1, 43.0, 45.8, 17.0, 11.2, 'Pike'],
  111. [12.2, 12.1, 13.0, 13.8, 16.5, 9.1, 'Smelt'],
  112. [100.0, 16.2, 18.0, 19.2, 27.2, 17.3, 'Perch'],
  113. [300.0, 32.7, 35.0, 38.8, 15.3, 11.3, 'Pike'],
  114. [700.0, 31.9, 35.0, 40.5, 40.1, 13.8, 'Bream'],
  115. [610.0, 30.9, 33.5, 38.6, 40.5, 13.3, 'Bream'],
  116. [700.0, 34.5, 37.0, 39.4, 27.5, 15.9, 'Perch'],
  117. [70.0, 15.7, 17.4, 18.5, 24.8, 15.9, 'Perch'],
  118. [955.0, 35.0, 38.5, 44.0, 41.1, 14.3, 'Bream'],
  119. [514.0, 30.5, 32.8, 34.0, 29.5, 17.7, 'Perch'],
  120. [51.5, 15.0, 16.2, 17.2, 26.7, 15.3, 'Perch'],
  121. [272.0, 25.0, 27.0, 30.6, 28.0, 15.6, 'Roach'],
  122. [500.0, 28.5, 30.7, 36.2, 39.3, 13.7, 'Bream'],
  123. [9.8, 11.4, 12.0, 13.2, 16.7, 8.7, 'Smelt'],
  124. [510.0, 40.0, 42.5, 45.5, 15.0, 9.8, 'Pike'],
  125. [925.0, 36.2, 39.5, 45.3, 41.4, 14.9, 'Bream'],
  126. [1015.0, 37.0, 40.0, 42.4, 29.2, 17.6, 'Perch'],
  127. [1550.0, 56.0, 60.0, 64.0, 15.0, 9.6, 'Pike'],
  128. [1000.0, 37.3, 40.0, 43.5, 28.4, 15.0, 'Whitewish'],
  129. [920.0, 35.0, 38.5, 44.1, 40.9, 14.3, 'Bream'],
  130. [140.0, 21.0, 22.5, 25.0, 26.2, 13.3, 'Roach'],
  131. [218.0, 25.0, 26.5, 28.0, 25.6, 14.8, 'Perch'],
  132. [9.7, 10.4, 11.0, 12.0, 18.3, 11.5, 'Smelt'],
  133. [69.0, 16.5, 18.2, 20.3, 26.1, 13.9, 'Roach'],
  134. [110.0, 19.0, 21.0, 22.5, 25.3, 15.8, 'Perch'],
  135. [150.0, 21.0, 23.0, 24.5, 21.3, 14.8, 'Perch'],
  136. [160.0, 20.5, 22.5, 25.3, 27.8, 15.1, 'Roach'],
  137. [7.0, 10.1, 10.6, 11.6, 14.9, 9.9, 'Smelt'],
  138. [78.0, 17.5, 18.8, 21.2, 26.3, 13.7, 'Roach'],
  139. [450.0, 26.8, 29.7, 34.7, 39.2, 14.2, 'Bream'],
  140. [556.0, 32.0, 34.5, 36.5, 28.1, 17.5, 'Perch'],
  141. [1650.0, 59.0, 63.4, 68.0, 15.9, 11.0, 'Pike'],
  142. [110.0, 19.1, 20.8, 23.1, 26.7, 14.7, 'Roach'],
  143. [685.0, 31.4, 34.0, 39.2, 40.8, 13.7, 'Bream'],
  144. [200.0, 22.1, 23.5, 26.8, 27.6, 15.4, 'Roach'],
  145. [770.0, 44.8, 48.0, 51.2, 15.0, 10.5, 'Pike'],
  146. [7.5, 10.0, 10.5, 11.6, 17.0, 10.0, 'Smelt'],
  147. [8.7, 10.8, 11.3, 12.6, 15.7, 10.2, 'Smelt'],
  148. [500.0, 42.0, 45.0, 48.0, 14.5, 10.2, 'Pike'],
  149. [170.0, 19.0, 20.7, 23.2, 40.5, 14.7, 'Silver Bream'],
  150. [120.0, 20.0, 22.0, 23.5, 24.0, 15.0, 'Perch'],
  151. [145.0, 19.8, 21.5, 24.1, 40.4, 13.1, 'Silver Bream'],
  152. [130.0, 19.3, 21.3, 22.8, 28.0, 15.5, 'Perch'],
  153. [850.0, 36.9, 40.0, 42.3, 28.2, 16.8, 'Perch'],
  154. [265.0, 25.4, 27.5, 28.9, 24.4, 15.0, 'Perch'],
  155. [0.0, 19.0, 20.5, 22.8, 28.4, 14.7, 'Roach'],
  156. [680.0, 31.8, 35.0, 40.6, 38.1, 15.1, 'Bream'],
  157. [90.0, 16.3, 17.7, 19.8, 37.4, 13.5, 'Silver Bream'],
  158. [575.0, 31.3, 34.0, 39.5, 38.3, 14.1, 'Bream'],
  159. [390.0, 29.5, 31.7, 35.0, 27.1, 15.3, 'Roach'],
  160. [225.0, 22.0, 24.0, 25.5, 28.6, 14.6, 'Perch'],
  161. [10.0, 11.3, 11.8, 13.1, 16.9, 9.8, 'Smelt'],
  162. [1000.0, 39.8, 43.0, 45.2, 26.4, 16.1, 'Perch'],
  163. [500.0, 28.7, 31.0, 36.2, 39.7, 13.3, 'Bream'],
  164. [120.0, 19.4, 21.0, 23.7, 25.8, 13.9, 'Roach'],
  165. [430.0, 35.5, 38.0, 40.5, 18.0, 11.3, 'Pike'],
  166. [200.0, 21.2, 23.0, 25.8, 40.1, 14.2, 'Silver Bream'],
  167. [250.0, 25.9, 28.0, 29.4, 26.6, 14.3, 'Perch'],
  168. [800.0, 33.7, 36.4, 39.6, 29.7, 16.6, 'Whitewish'],
  169. [32.0, 12.5, 13.7, 14.7, 24.0, 13.6, 'Perch'],
  170. [430.0, 26.5, 29.0, 34.0, 36.6, 15.1, 'Bream'],
  171. [145.0, 20.5, 22.0, 24.3, 27.3, 14.6, 'Roach'],
  172. [950.0, 48.3, 51.7, 55.1, 16.2, 11.2, 'Pike'],
  173. [300.0, 31.7, 34.0, 37.8, 15.1, 11.0, 'Pike'],
  174. [250.0, 25.4, 27.5, 28.9, 25.2, 15.8, 'Perch'],
  175. [650.0, 36.5, 39.0, 41.4, 26.9, 14.5, 'Perch'],
  176. [270.0, 24.1, 26.5, 29.3, 27.8, 14.5, 'Whitewish'],
  177. [600.0, 29.4, 32.0, 37.2, 41.5, 15.0, 'Bream'],
  178. [145.0, 22.0, 24.0, 25.5, 25.0, 15.0, 'Perch'],
  179. [1100.0, 40.1, 43.0, 45.5, 27.5, 16.3, 'Perch']]
  180.  
  181.  
  182. class decisionnode(object):
  183. def __init__(self, col=-1, value=None, results=None, tb=None, fb=None):
  184. self.col = col
  185. self.value = value
  186. self.results = results
  187. self.tb = tb
  188. self.fb = fb
  189.  
  190.  
  191. def sporedi_broj(value1, value2):
  192. return value1 >= value2
  193.  
  194.  
  195. def sporedi_string(value1, value2):
  196. return value1 == value2
  197.  
  198.  
  199. # In[43]:
  200.  
  201.  
  202. def get_compare_func(value):
  203. if isinstance(value, int) or isinstance(value, float):
  204. comparer = sporedi_broj
  205. else:
  206. comparer = sporedi_string
  207. return comparer
  208.  
  209.  
  210. def compare_values(v1, v2):
  211. sporedi = get_compare_func(v1)
  212. return sporedi(v1, v2)
  213.  
  214.  
  215. def divideset(rows, column, value):
  216. sporedi = get_compare_func(value)
  217. # print(split_function)
  218. # Divide the rows into two sets and return them
  219. set_false = []
  220. set_true = []
  221. for row in rows:
  222. uslov = sporedi(row[column], value)
  223. # print(column, value, row[column], uslov, row)
  224. if uslov:
  225. set_true.append(row)
  226. else:
  227. set_false.append(row)
  228. # print(len(set_true), len(set_false))
  229. # set_true = [row for row in rows if
  230. # split_function(row, column, value)] # za sekoj row od rows za koj split_function vrakja true
  231. # set_false = [row for row in rows if
  232. # not split_function(row, column, value)] # za sekoj row od rows za koj split_function vrakja false
  233. return (set_true, set_false)
  234.  
  235.  
  236. def uniquecounts(rows):
  237. d = {}
  238. for r in rows:
  239. # print(r[-1])
  240. d.setdefault(r[-1], 0)
  241. d[r[-1]] += 1
  242. return d
  243.  
  244.  
  245. from math import log
  246.  
  247. log2 = lambda x: log(x) / log(2)
  248.  
  249.  
  250. def entropy(rows):
  251. results = uniquecounts(rows)
  252. # Now calculate the entropy
  253. ent = 0.0
  254. n = len(rows)
  255. for label, cnt in results.items():
  256. # print(r)
  257. p = float(cnt) / n
  258. # print(label, cnt, p)
  259. ent -= p * log2(p)
  260. return ent
  261.  
  262.  
  263. def info_gain(current_score, sets, scoref=entropy):
  264. m = sum([len(s) for s in sets])
  265. gain = current_score
  266. for s in sets:
  267. n = len(s)
  268. p = 1. * n / m
  269. gain -= p * scoref(s)
  270. return gain
  271.  
  272.  
  273. def buildtree(rows, scoref=entropy):
  274. if len(rows) == 0:
  275. return decisionnode()
  276. current_score = scoref(rows)
  277.  
  278. # Set up some variables to track the best criteria
  279. best_gain = 0.0
  280. best_column = -1
  281. best_value = None
  282. best_subsetf = None
  283. best_subsett = None
  284.  
  285. column_count = len(rows[0]) - 1
  286. for col in range(column_count):
  287. # Generate the list of different values in
  288. # this column
  289. # column_values = set()
  290. # for row in rows:
  291. # column_values.add(row[col])
  292. # print(column_values)
  293. column_values = set([row[col] for row in rows])
  294. # print('Zemame vo predvid podelba po:', col, len(column_values), column_values)
  295. # continue
  296. # Now try dividing the rows up for each value
  297. # in this column
  298. for value in column_values:
  299. sets = divideset(rows, col, value)
  300.  
  301. # Information gain
  302. # p = float(len(set1)) / len(rows)
  303. # gain = current_score - p * scoref(set1) - (1 - p) * scoref(set2)
  304. gain = info_gain(current_score, sets, scoref)
  305. if gain > best_gain and len(sets) > 0 and len(sets[0]) > 0 and len(sets[1]) > 0:
  306. best_gain = gain
  307. best_column = col
  308. best_value = value
  309. best_subsett = sets[0]
  310. best_subsetf = sets[1]
  311. # best_criteria = (col, value)
  312. # best_sets = (set1, set2)
  313. # print('Dividing dataset', col, value, gain, sets)
  314. # pronajden e korenot
  315. # return
  316. # Create the subbranches
  317. if best_gain > 0:
  318. # print(best_subsett)
  319. # print(best_subsetf)
  320. #print(best_column, best_value, best_gain)
  321. #print('Starting true subbranch')
  322. trueBranch = buildtree(best_subsett, scoref)
  323. #print()
  324. #print('Starting false subbranch')
  325. falseBranch = buildtree(best_subsetf, scoref)
  326. #print()
  327. return decisionnode(col=best_column, value=best_value,
  328. tb=trueBranch, fb=falseBranch)
  329.  
  330. else:
  331. #print('Terminalen jazol')
  332. #print()
  333. return decisionnode(results=uniquecounts(rows))
  334.  
  335.  
  336. def printtree(tree, indent=''):
  337. # Is this a leaf node?
  338. if tree.results != None:
  339. print(indent + str(sorted(tree.results.items())))
  340. else:
  341. # Print the criteria
  342. print(indent + str(tree.col) + ':' + str(tree.value) + '? ')
  343. # Print the branches
  344. print(indent + 'T->')
  345. printtree(tree.tb, indent + ' ')
  346. print(indent + 'F->')
  347. printtree(tree.fb, indent + ' ')
  348.  
  349.  
  350. def classify(observation, tree):
  351. if tree.results != None:
  352. # return tree.results
  353. if len(tree.results) == 1:
  354. return list(tree.results.keys())[0], 1.0
  355. else:
  356. inv = sorted([(cnt, label) for label, cnt in tree.results.items()], reverse=True)
  357. label = inv[0][1]
  358. cnt = inv[0][0]
  359. total_count = sum(tree.results.values())
  360. return label, cnt / total_count
  361. # return tree.results
  362. else:
  363. vrednost = observation[tree.col]
  364. if compare_values(vrednost, tree.value):
  365. branch = tree.tb
  366. else:
  367. branch = tree.fb
  368. return classify(observation, branch)
  369.  
  370.  
  371. def classify(observation, tree):
  372. if tree.results != None:
  373. return list(tree.results.keys())
  374. else:
  375. vrednost = observation[tree.col]
  376. branch = None
  377.  
  378. if isinstance(vrednost, int) or isinstance(vrednost, float):
  379. if vrednost >= tree.value:
  380. branch = tree.tb
  381. else:
  382. branch = tree.fb
  383. else:
  384. if vrednost == tree.value:
  385. branch = tree.tb
  386. else:
  387. branch = tree.fb
  388.  
  389. return classify(observation, branch)
  390.  
  391.  
  392. if __name__ == "__main__":
  393. # if _name_ == "_main_":
  394. index = int(input())
  395. x = 0
  396. # index=int(index)
  397. lista =[]
  398. for i in range(len(data)):
  399. if(data[i][6] == "Bream"):
  400. if(data[i][6] != x):
  401. counter = 0
  402. if(counter < 25):
  403. lista.append(data[i])
  404. counter = counter+1
  405. x = data[i][6]
  406. for i in range(len(data)):
  407. if(data[i][6] =="Perch"):
  408. if(data[i][6] != x):
  409. counter=0;
  410. if(counter < 25):
  411. lista.append(data[i])
  412. counter=counter+1
  413. x = data[i][6]
  414.  
  415. print(lista)
  416. tree = buildtree(lista)
  417. c = classify(data,tree)
  418. print(c)
Add Comment
Please, Sign In to add comment