Advertisement
max2201111

final VK ok vse

May 26th, 2024
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 36.27 KB | Science | 0 0
  1. #Navod na pouziti, Mgr. Hynek Mlčoušek, v Brne 2.5.2024
  2. #Ulozte do lokalniho souboru u sebe na PC data tohoto tvaru vzdy ukoncene 0 ci 1 (jde o uceni s ucitelem: 1 = nemocny, 0 = prezil/zdravy, ve vystupu bude zelena znacit 0, cervena 1)  a bez znaku #; pozor na ","
  3.  
  4. # [ [23.657800719276743,18.859916797201468,0],
  5. # [22.573729142097473,17.96922325097786,0],
  6. # [32.55342396968757,29.463651408558803,0],
  7. # [6.718035041529263,25.704665468161718,1],
  8. # [14.401918566243225,16.770856492924658,0],
  9. # [17.457907312962234,21.76521470574044,0],
  10. # [20.02796946568093,73.45445954770891,1],
  11. # [30.295138369778076,62.901112886193246,1],
  12. # [15.128977804449633,32.40267702110393,0],
  13. # [30.179457395820013,58.982492125646104,1],
  14. # [28.01649701854089,63.92781357637711,1],
  15. # [16.791838457871147,42.33482314089884,0],
  16. # [10.583694293380976,19.61926728942497,0],
  17. # [26.634447074406467,91.96624817360987,1],
  18. # [26.217868623367643,36.400293587062976,0],
  19. # [17.689396788624936,60.79797114006423,1],
  20. # [33.17193822527976,66.75277364959176,1],
  21. # [23.793952755709153,22.57501437360518,0]]
  22.  
  23. #kliknete na cerne tlacitko s trojuhelnickem vlevo nahore
  24. #pod kodem se objevi moznost spustit dialogove okenko, kliknete na nej
  25. #soubor, ktery mate z bodu vyse vyberte a nahrajte
  26. #Najdete v tomto kodu retezec:
  27. ###ZDE VLOZTE DATA OD NOVYCH PACIENTU
  28.  
  29. #Vlozte do pole
  30. # new_persons_results = []
  31. # data o nekolika malo novych pacientech bez ukoncovaci 0 a 1, ale se stejnym poctem sloupcu jako ma soubor z Vaseho lokalniho disku, vyse by tedy toto bylo rovno 2
  32. #kod vyhodi hned po natrenovani, (jehoz prubeh muzete sledovat na modre progres bare) pro kazdy radek z new_persons_results bilo-sedo-cerne ctverecky vznikle z normalizace poskytnutych dat a ukoncovaci ctverecek cerveny pripadne zeleny
  33. #zaroven s tim se vypise realne cislo mezi 0 a 1 znacici jak moc je pacient zdravy (blizke 0) ci nemocny (blizke 1)
  34. #cisla uprostred pak indikuji zadany oranzovy semafor.
  35. #je na lekarich nastavit tresholdy (tedy pravdepodobnosti: cisla mezi 0 a 1) ktere pak daji zaver, zda je pacient cerveny, oranzovy ci zeleny
  36.  
  37. # prosim o komnetare a vysledky na realnych datech, je zadouci aby radku v matici, tedy pacientu byly stovky a sloupcu desitky
  38. # Moznosti vyuziti: onkologicka diagnoza vs. zdrava kontorlni skupina, diabetes (pritomnost/nepritomnost), testovani noveho leku oproti placebu atd.
  39.  
  40. #kod zaroven vyhodi confusion matici, tedy mozne True Negative a False Positive plus spravne zarazene hodnoty spolu s presnosti,F1 score recall atd.
  41. #poznamka ke kodu: jde o epxerimentalni verzi, ktera krome skutecne potrebneho kodu obsahuje ladici informace, ruzne duplicity, nadbytecne prikazy atd.
  42. # Na uvod behu programu se pro kontorlu vypise poskytnuta matice a jeji normalizovana verze, je treba sjet jezdcem napravo nize na obrazky a dalsi vystupy
  43.  
  44. #Dekuji profesoru Petru Dostalovi za namet k teto praci a poskytnuta data, byt je potreba mit data realna
  45.  
  46. import numpy as np
  47. import matplotlib.pyplot as plt
  48. import tensorflow as tf
  49. from tqdm import tqdm
  50.  
  51.  
  52. from IPython.display import display
  53. from IPython.display import Javascript
  54. display(Javascript('IPython.OutputArea.auto_scroll_threshold = 9999;'))
  55.  
  56. label_colors = {0: [0, 128, 0], 1: [255, 0, 0]}
  57. label_colors_testing = {0: [0, 128, 0], 1: [255, 0, 0]}
  58.  
  59.  
  60. %matplotlib inline
  61.  
  62.  
  63.  
  64. # Function to create images based on predictions
  65. def create_image(data, predictions):
  66.     num_rows, num_columns = len(data), len(data[0])
  67.     image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
  68.  
  69.     for i in range(num_rows):
  70.         for j in range(num_columns):
  71.             pixel_value = int(np.interp(data[i][j], [np.min(data), np.max(data)], [0, 255]))
  72.             image[i, j] = np.array([pixel_value] * 3)
  73.  
  74.         # Create a gradient based on the normalized values
  75.         gradient_value = int(np.interp(predictions[i], [0, 1], [0, 255]))
  76.         image[i, -1] = np.array([gradient_value] * 3)
  77.  
  78.     return image
  79.  
  80. def create_image(data, predictions):
  81.     num_rows, num_columns = len(data), len(data[0])
  82.     image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
  83.  
  84.     for i in range(num_rows):
  85.         for j in range(num_columns):
  86.             pixel_value = int(np.interp(data[i][j], [np.min(data), np.max(data)], [0, 255]))
  87.             image[i, j] = np.array([pixel_value] * 3)
  88.  
  89.         # Use red for class 0 and green for class 1
  90.         if predictions[i] == 0:
  91.             image[i, -1] = np.array([255, 0, 0])  # Red
  92.         elif predictions[i] == 1:
  93.             image[i, -1] = np.array([0, 128, 0])  # Green
  94.  
  95.     return image
  96.  
  97. def create_image(data, predictions, label_colors):
  98.     num_rows, num_columns = len(data), len(data[0])
  99.     image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
  100.  
  101.     for i in range(num_rows):
  102.         for j in range(num_columns):
  103.             pixel_value = int(np.interp(data[i][j], [np.min(data), np.max(data)], [0, 255]))
  104.             image[i, j] = np.array([pixel_value] * 3)
  105.  
  106.         # Use the specified color for the last column based on the label
  107.         image[i, -1] = label_colors[predictions[i]]
  108.  
  109.     return image
  110.  
  111. # def create_imageN(data, predictions, label_colors=None):
  112. #     num_rows, num_columns = len(data), len(data[0])
  113. #     image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
  114.  
  115. #     for i in range(num_rows):
  116. #         for j in range(num_columns):
  117. #             # Map data values to the full range of 0 to 255
  118. #             pixel_value = int(np.interp(data[i][j], [np.min(data), np.max(data)], [0, 255]))
  119. #             image[i, j] = np.array([pixel_value] * 3)
  120.  
  121. #         # Use the specified color for the last column based on the label
  122. #         if label_colors is not None:
  123. #             image[i, -1] = label_colors[predictions[i]]
  124. #         else:
  125. #             # If label_colors is not provided, set the last column to grayscale
  126. #             pixel_value = int(np.interp(predictions[i], [0, 1], [0, 255]))
  127. #             image[i, -1] = np.array([pixel_value] * 3)
  128.  
  129. #     return image
  130.  
  131. # def create_imageN(data, predictions, label_colors=None):
  132. #     num_rows, num_columns = len(data), len(data[0])
  133. #     image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
  134.  
  135. #     for i in range(num_rows):
  136. #         for j in range(num_columns):
  137. #             # Map data values to the full range of 0 to 255
  138. #             pixel_value = int(np.interp(data[i][j], [np.min(data), np.max(data)], [0, 255]))
  139. #             image[i, j] = np.array([pixel_value] * 3)
  140.  
  141. #         # Use the specified color for the last column based on the label
  142. #         if label_colors is not None:
  143. #             image[i, -1] = label_colors[predictions[i]]
  144. #         else:
  145. #             # If label_colors is not provided, set the last column to grayscale
  146. #             pixel_value = int(np.interp(predictions[i], [np.min(data), np.max(data)], [0, 255]))
  147. #             image[i, -1] = np.array([pixel_value] * 3)
  148.  
  149. #     return image
  150.  
  151. # def create_imageN(data, predictions, label_colors=None):
  152. #     num_rows, num_columns = len(data), len(data[0])
  153. #     image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
  154.  
  155. #     for i in range(num_rows):
  156. #         for j in range(num_columns - 1):  # Exclude the last column for now
  157. #             # Map data values to the full range of 0 to 255
  158. #             pixel_value = int(np.interp(data[i][j], [np.min(data[:, j]), np.max(data[:, j])], [0, 255]))
  159. #             image[i, j] = np.array([pixel_value] * 3)
  160.  
  161. #         # Use the specified color for the last column based on the label
  162. #         if label_colors is not None:
  163. #             image[i, -1] = label_colors[predictions[i]]
  164. #         else:
  165. #             # If label_colors is not provided, set the last column to grayscale
  166. #             pixel_value = int(np.interp(predictions[i], [0, 1], [0, 255]))
  167. #             image[i, -1] = np.array([pixel_value] * 3)
  168.  
  169. #     return image
  170.  
  171.  
  172. # def create_imageN(data, predictions, label_colors=None):
  173. #     num_rows, num_columns = len(data), len(data[0])
  174. #     image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
  175.  
  176. #     data_array = np.array(data)  # Convert data to a NumPy array
  177.  
  178. #     for i in range(num_rows):
  179. #         for j in range(num_columns - 1):  # Exclude the last column for now
  180. #             # Map data values to the full range of 0 to 255
  181. #             pixel_value = int(np.interp(data_array[i, j], [np.min(data_array[:, j]), np.max(data_array[:, j])], [0, 255]))
  182. #             image[i, j] = np.array([pixel_value] * 3)
  183.  
  184. #         # Use the specified color for the last column based on the label
  185. #         if label_colors is not None:
  186. #             image[i, -1] = label_colors[predictions[i]]
  187. #         else:
  188. #             # If label_colors is not provided, set the last column to grayscale
  189. #             pixel_value = int(np.interp(predictions[i], [0, 1], [0, 255]))
  190. #             image[i, -1] = np.array([pixel_value] * 3)
  191.  
  192. #     return image
  193.  
  194. # def create_imageN(data, predictions, label_colors=None):
  195. #     num_rows, num_columns = len(data), len(data[0])
  196. #     image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
  197.  
  198. #     data_array = np.array(data)  # Convert data to a NumPy array
  199.  
  200. #     for i in range(num_rows):
  201. #         for j in range(num_columns - 1):  # Exclude the last column for now
  202. #             # Map data values to the full range of 0 to 255
  203. #             pixel_value = int(np.interp(data_array[i, j], [np.min(data_array[:, j]), np.max(data_array[:, j])], [0, 255]))
  204. #             image[i, j] = np.array([pixel_value] * 3)
  205.  
  206. #         # Use the specified color for the last column based on the label
  207. #         if label_colors is not None:
  208. #             image[i, -1] = label_colors[predictions[i]]
  209. #         else:
  210. #             # If label_colors is not provided, set the last column to grayscale
  211. #             pixel_value = int(np.interp(predictions[i], [0, 1], [0, 255]))
  212. #             image[i, -1] = np.array([pixel_value] * 3)
  213.  
  214. #     # Now, normalize the last column separately to achieve grayscale
  215. #     min_pixel_value = np.min(image[:, -1])
  216. #     max_pixel_value = np.max(image[:, -1])
  217. #     for i in range(num_rows):
  218. #         pixel_value = int(np.interp(image[i, -1], [min_pixel_value, max_pixel_value], [0, 255]))
  219. #         image[i, -1] = np.array([pixel_value] * 3)
  220.  
  221. #     return image
  222.  
  223. # def create_imageN(data, predictions, label_colors=None):
  224. #     num_rows, num_columns = len(data), len(data[0])
  225. #     image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
  226.  
  227. #     for i in range(num_rows):
  228. #         for j in range(num_columns):
  229. #             # Map data values to the full range of 0 to 255
  230. #             pixel_value = int(np.interp(data[i][j], [np.min(data), np.max(data)], [0, 255]))
  231. #             image[i, j] = np.array([pixel_value] * 3)
  232.  
  233. #         # Now, normalize the last column separately to achieve grayscale
  234.  
  235.  
  236. #         min_pixel_value = np.min(data[:, -1])
  237. #         max_pixel_value = np.max(data[:, -1])
  238. #         pixel_value = int(np.interp(data[i][-1], [min_pixel_value, max_pixel_value], [0, 255]))
  239.  
  240. #         # Use the specified color for the last column based on the label
  241. #         if label_colors is not None:
  242. #             image[i, -1] = label_colors[predictions[i]]
  243.  
  244. #     return image
  245.  
  246. # def create_imageN(data, predictions, label_colors=None):
  247. #     num_rows, num_columns = len(data), len(data[0])
  248. #     image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
  249.  
  250. #     for i in range(num_rows):
  251. #         for j in range(num_columns):
  252. #             # Map data values to the full range of 0 to 255
  253. #             pixel_value = int(np.interp(data[i][j], [np.min(data), np.max(data)], [0, 255]))
  254. #             image[i, j] = np.array([pixel_value] * 3)
  255.  
  256. #         # Normalize the last column separately to achieve grayscale
  257. #         min_pixel_value = np.min(data[i])
  258. #         max_pixel_value = np.max(data[i])
  259. #         pixel_value = int(np.interp(data[i][-1], [min_pixel_value, max_pixel_value], [0, 255]))
  260. #         image[i, -1] = np.array([pixel_value] * 3)
  261.  
  262. #         # Use the specified color for the last column based on the label
  263. #         if label_colors is not None:
  264. #             image[i, -1] = label_colors[predictions[i]]
  265.  
  266. #     return image
  267.  
  268.  
  269. # def create_imageN(data, predictions, label_colors=None):
  270. #     num_rows, num_columns = len(data), len(data[0])
  271. #     image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
  272.  
  273. #     # Normalize the first two columns independently
  274. #     for j in range(2):
  275. #         min_pixel_value = np.min(data[:, j])
  276. #         max_pixel_value = np.max(data[:, j])
  277. #         for i in range(num_rows):
  278. #             pixel_value = int(np.interp(data[i][j], [min_pixel_value, max_pixel_value], [0, 255]))
  279. #             image[i, j] = np.array([pixel_value] * 3)
  280.  
  281. #     # Normalize the last column separately to achieve grayscale
  282. #     min_pixel_value = np.min(data[:, -1])
  283. #     max_pixel_value = np.max(data[:, -1])
  284. #     for i in range(num_rows):
  285. #         pixel_value = int(np.interp(data[i][-1], [min_pixel_value, max_pixel_value], [0, 255]))
  286. #         image[i, -1] = np.array([pixel_value] * 3)
  287.  
  288. #         # Use the specified color for the last column based on the label
  289. #         if label_colors is not None:
  290. #             image[i, -1] = label_colors[predictions[i]]
  291.  
  292. #     return image
  293.  
  294. # def create_imageN(data, predictions, label_colors=None):
  295. #     # Convert data to a NumPy array
  296. #     data = np.array(data)
  297.  
  298. #     num_rows, num_columns = data.shape
  299. #     image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
  300.  
  301. #     # Normalize the first two columns independently
  302. #     for j in range(2):
  303. #         min_pixel_value = np.min(data[:, j])
  304. #         max_pixel_value = np.max(data[:, j])
  305. #         for i in range(num_rows):
  306. #             pixel_value = int(np.interp(data[i][j], [min_pixel_value, max_pixel_value], [0, 255]))
  307. #             image[i, j] = np.array([pixel_value] * 3)
  308.  
  309. #     # Normalize the last column separately to achieve grayscale
  310. #     min_pixel_value = np.min(data[:, -1])
  311. #     max_pixel_value = np.max(data[:, -1])
  312. #     for i in range(num_rows):
  313. #         pixel_value = int(np.interp(data[i][-1], [min_pixel_value, max_pixel_value], [0, 255]))
  314. #         image[i, -1] = np.array([pixel_value] * 3)
  315.  
  316. #         # Use the specified color for the last column based on the label
  317. #         if label_colors is not None:
  318. #             image[i, -1] = label_colors[predictions[i]]
  319.  
  320. #     return image
  321.  
  322.  
  323. # def create_imageN(data, predictions, label_colors=None):
  324. #     # Convert data to a NumPy array
  325. #     data = np.array(data)
  326.  
  327. #     num_rows, num_columns = data.shape
  328. #     image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
  329.  
  330. #     # Normalize the first two columns independently
  331. #     for j in range(2):
  332. #         min_pixel_value = np.min(data[:, j])
  333. #         max_pixel_value = np.max(data[:, j])
  334. #         for i in range(num_rows):
  335. #             pixel_value = int(np.interp(data[i][j], [min_pixel_value, max_pixel_value], [0, 255]))
  336. #             image[i, j] = np.array([pixel_value] * 3)
  337.  
  338. #     # Normalize the last column separately to achieve grayscale
  339. #     min_pixel_value_last = np.min(data[:, -1])
  340. #     max_pixel_value_last = np.max(data[:, -1])
  341. #     for i in range(num_rows):
  342. #         pixel_value_last = int(np.interp(data[i][-1], [min_pixel_value_last, max_pixel_value_last], [0, 255]))
  343. #         image[i, -1] = np.array([pixel_value_last] * 3)
  344.  
  345. #         # Use the specified color for the last column based on the label
  346. #         if label_colors is not None:
  347. #             image[i, -1] = label_colors[predictions[i]]
  348.  
  349. #     return image
  350.  
  351. # def create_imageN(data, predictions, label_colors=None):
  352. #     image_training = np.zeros((num_training_rows, len(X_train[0]) + 1, 3), dtype=np.uint8)
  353.  
  354.  
  355. #     print("**************************",num_training_rows,"*******************")
  356.  
  357. #     min_pixel_value = np.min(X_train_normalized)
  358. #     max_pixel_value = np.max(X_train_normalized)
  359.  
  360. #     # Populate image_training with consistent gray and red/green colors based on the labels in the last column
  361. #     # for i, label in enumerate(y_train):
  362. #     #     for j in range(len(X_train[0])
  363. #     #         pixel_value = int(np.interp(X_train_normalized[i][j], [min_pixel_value, max_pixel_value], [0, 255]))
  364. #     #         image_training[i, j] = np.array([pixel_value] * 3)
  365. #     #         image_training[i, -1] = np.array([128, 128, 128])
  366. #     #     if label == 0:
  367. #     #         image_training[i, -1] = np.array([0, 128, 0])
  368. #     #     elif label == 1:
  369. #     #         image_training[i, -1] = np.array([255, 0, 0])
  370.  
  371.  
  372.  
  373. #     # Populate image_training with consistent gray and red/green colors based on the labels in the last column
  374. #     for i, label in enumerate(y_train):
  375. #         for j in range(len(X_train[0])):
  376. #             pixel_value = int(np.interp(X_train_normalized[i][j], [min_pixel_value, max_pixel_value], [0, 255]))
  377. #             image_training[i, j] = np.array([pixel_value] * 3)
  378. #         image_training[i, -1] = np.array([128, 128, 128])
  379. #         if label == 0:
  380. #             image_training[i, -1] = np.array([0, 128, 0])
  381. #         elif label == 1:
  382. #             image_training[i, -1] = np.array([255, 0, 0])
  383.  
  384.  
  385. #     return image_training
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394. # def create_imageN(data, predictions, label_colors=None):
  395. #     num_training_rows = 1  # Set the number of rows to 1
  396. #     image_training = np.zeros((num_training_rows, len(X_train[0]) + 1, 3), dtype=np.uint8)
  397.  
  398. #     min_pixel_value = np.min(X_train_normalized)
  399. #     max_pixel_value = np.max(X_train_normalized)
  400.  
  401. #     # Populate image_training with consistent gray and red/green colors based on the labels in the last column
  402. #     for j in range(len(X_train[0])):
  403. #         pixel_value = int(np.interp(data[0][j], [min_pixel_value, max_pixel_value], [0, 255]))
  404. #         image_training[0, j] = np.array([pixel_value] * 3)
  405.  
  406. #     image_training[0, -1] = np.array([128, 128, 128])  # Set a consistent gray background
  407.  
  408. #     label = y_train[0]
  409. #     if label == 0:
  410. #         image_training[0, -1] = np.array([0, 128, 0])  # Green for label 0
  411. #     elif label == 1:
  412. #         image_training[0, -1] = np.array([255, 0, 0])  # Red for label 1
  413.  
  414. #     return image_training
  415.  
  416. def create_imageN(data, predictions, label_colors=None):
  417.     num_training_rows = len(data)  # Set the number of rows based on the data
  418.     num_columns = len(data[0])
  419.  
  420.     image_training = np.zeros((num_training_rows, num_columns + 1, 3), dtype=np.uint8)
  421.  
  422.     min_pixel_value = np.min(X_train_normalized)
  423.     max_pixel_value = np.max(X_train_normalized)
  424.  
  425.     for i in range(num_training_rows):
  426.         # Normalize the first columns independently
  427.         for j in range(num_columns):
  428.             pixel_value = int(np.interp(data[i][j], [min_pixel_value, max_pixel_value], [0, 255]))
  429.             image_training[i, j] = np.array([pixel_value] * 3)
  430.  
  431.         # Normalize the last column separately to achieve grayscale
  432.         pixel_value_last = int(np.interp(data[i][-1], [min_pixel_value, max_pixel_value], [0, 255]))
  433.         image_training[i, -1] = np.array([pixel_value_last] * 3)
  434.  
  435.         # Use the specified color for the last column based on the label
  436.         if label_colors is not None:
  437.             image_training[i, -1] = label_colors[predictions[i]]
  438.  
  439.     return image_training
  440.  
  441.  
  442.  
  443.  
  444. # Load data from a file
  445. #file_path = 'C:/Users/Hynek/Desktop/example4.txt'
  446. from google.colab import files
  447. uploaded = files.upload()
  448.  
  449. # Tento kód otevře dialogové okno pro výběr souboru z vašeho počítače.
  450. import io
  451. import pandas as pd
  452.  
  453. # Předpokládáme, že jste nahráli CSV soubor
  454. for fn in uploaded.keys():
  455.   print('User uploaded file "{name}" with length {length} bytes'.format(
  456.       name=fn, length=len(uploaded[fn])))
  457.   path = io.BytesIO(uploaded[fn])  # Pro soubory, které potřebují být čteny jako binární objekty
  458.   df = pd.read_csv(path)
  459.   print(df.head())  # Vypíše prvních pět řádků DataFrame
  460.  
  461.  
  462. all_results = []
  463. #with open(file_path, 'r') as file:
  464. #    file_content = file.read()
  465.  
  466. # Execute the content as Python code
  467. ##exec(file_content)
  468.  
  469. import os
  470. import shutil
  471. import ast
  472.  
  473. for filename in uploaded.keys():
  474.     original_path = f"/content/{filename}"
  475.     destination_path = os.path.join("/content/", "/content/DATA2")
  476.     shutil.move(original_path, destination_path)
  477.     print(f"Soubor {filename} byl přesunut do {destination_path}")
  478.  
  479. file_path = '/content/DATA2'  # Cesta k souboru
  480. with open(file_path, 'r') as file:
  481.     code = file.read()
  482.  
  483. A_list = ast.literal_eval(code)
  484.  
  485.  
  486. # Převod na NumPy pole
  487. A = np.array(A_list)
  488.  
  489. #exec(code)
  490.  
  491. # Now, all_results contains lists corresponding to each row in the CSV file
  492. ##print(all_results)
  493.  
  494. # Assign values to variables dynamically based on the rows of matrix A
  495. for i, row in enumerate(A, start=1):
  496.     globals()[f"person{i}_results"] = list(row)
  497.  
  498. # Print the assigned variables
  499. for i in range(1, len(A) + 1):
  500.   #  print(f"person{i}_results {globals()[f'person{i}_results']}")
  501.     all_results.append(f"person{i}_results")
  502. ##print(all_results)
  503.  
  504.  
  505.  
  506. result_variables = []
  507.  
  508. # Loop through the variable names and get the corresponding variables using globals()
  509. for var_name in all_results:
  510.     result_variables.append(globals()[var_name])
  511.  
  512. # Now, result_variables contains the variables with names specified in variable_names
  513. #print(result_variables)
  514.  
  515. all_results = result_variables
  516. new_persons_results = result_variables
  517.  
  518.  
  519. # # Define the blood test results for sixteen persons
  520. # person1_results = [80, 90, 100, 125, 120, 0]
  521. # person2_results = [95, 105, 115, 110, 135, 1]
  522. # person3_results = [110, 120, 130, 140, 150, 0]
  523. # person4_results = [100, 110, 120, 130, 140, 1]
  524. # person5_results = [105, 115, 100, 105, 110, 0]
  525. # person6_results = [90, 110, 115, 95, 120, 1]
  526. # person7_results = [116, 99, 106, 105, 119, 0]
  527. # person8_results = [111, 93, 118, 118, 107, 1]
  528. # person9_results = [107, 97, 105, 119, 98, 0]
  529. # person10_results = [92, 108, 90, 117, 111, 1]
  530. # person11_results = [118, 105, 103, 118, 99, 0]
  531. # person12_results = [97, 115, 101, 101, 113, 1]
  532. # person13_results = [95, 111, 93, 112, 120, 0]
  533. # person14_results = [100, 112, 118, 109, 103, 1]
  534. # person15_results = [113, 91, 94, 93, 99, 0]
  535. # person16_results = [103, 92, 95, 110, 98, 1]
  536.  
  537. # # Combine the results into a list
  538. # all_results = [person1_results, person2_results, person3_results, person4_results,
  539. #                person5_results, person6_results, person7_results, person8_results,
  540. #                person9_results, person10_results, person11_results, person12_results,
  541. #                person13_results, person14_results, person15_results, person16_results]
  542.  
  543.  
  544. # #all_results = [person1_results, person2_results]
  545.  
  546.  
  547. # Extract the last column (0 or 1) as labels
  548. labels = [results[-1] for results in all_results]
  549.  
  550. # Remove the last column from the dataset
  551. data = [results[:-1] for results in all_results]
  552.  
  553. # Define the number of rows for training and testing
  554. num_training_rows = 100
  555. num_testing_rows = 100
  556.  
  557. # Split the data into training and testing datasets
  558. #X_train, X_test, y_train, y_test = data[:num_training_rows], data[-num_testing_rows:], labels[:num_training_rows], labels[-num_testing_rows:]
  559.  
  560. X_train, X_test, y_train, y_test = data[:num_training_rows], data[:num_testing_rows], labels[:num_training_rows], labels[:num_testing_rows]
  561.  
  562.  
  563. # Normalize the training data
  564. min_values = np.min(X_train, axis=0)
  565. max_values = np.max(X_train, axis=0)
  566. X_train_normalized = (X_train - min_values) / (max_values - min_values)
  567.  
  568.  
  569. # Normalize the training data
  570. min_values = np.min(X_train, axis=0)
  571. max_values = np.max(X_train, axis=0)
  572. X_train_normalized = (X_train - min_values) / (max_values - min_values)
  573.  
  574. # Normalize the testing data using the min and max values of the training data
  575. X_test_normalized = (X_test - min_values) / (max_values - min_values)
  576.  
  577.  
  578. # Print normalized training data
  579. print("Normalized Training Data:")
  580. print(X_train_normalized)
  581. print("Adenormalized",X_train_normalized*(max_values - min_values)+min_values,"Bdenormalized")
  582.  
  583. # Define a simple neural network model
  584. # model = tf.keras.Sequential([
  585. #     tf.keras.layers.Dense(128, activation='relu', input_shape=(len(X_train[0]),)),
  586. #     tf.keras.layers.Dense(64, activation='relu'),
  587. #     tf.keras.layers.Dense(1, activation='sigmoid')
  588. # ])
  589.  
  590. # # Compile the model
  591. # model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
  592.  
  593.  
  594. import tensorflow as tf
  595.  
  596. # Vylepšený model
  597. model = tf.keras.Sequential([
  598.     tf.keras.layers.Dense(256, activation='relu', input_shape=(len(X_train[0]),)),
  599.     tf.keras.layers.Dropout(0.3),
  600.     tf.keras.layers.Dense(128, activation='relu'),
  601.     tf.keras.layers.Dropout(0.3),
  602.     tf.keras.layers.Dense(64, activation='relu'),
  603.     tf.keras.layers.Dropout(0.3),
  604.     tf.keras.layers.Dense(1, activation='sigmoid')
  605. ])
  606.  
  607. # Použití Adam optimizer s learning rate schedulerem
  608. lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
  609.     initial_learning_rate=1e-3,
  610.     decay_steps=10000,
  611.     decay_rate=0.9
  612. )
  613. optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
  614.  
  615. # Kompilace modelu
  616. model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
  617.  
  618.  
  619. # Lists to store accuracy values
  620. accuracy_history = []
  621.  
  622. # Create images for the training data
  623. image_training = np.zeros((num_training_rows, len(X_train[0]) + 1, 3), dtype=np.uint8)
  624.  
  625.  
  626. min_pixel_value = np.min(X_train_normalized)
  627. max_pixel_value = np.max(X_train_normalized)
  628.  
  629. # Populate image_training with consistent gray and red/green colors based on the labels in the last column
  630. # for i, label in enumerate(y_train):
  631. #     for j in range(len(X_train[0])
  632. #         pixel_value = int(np.interp(X_train_normalized[i][j], [min_pixel_value, max_pixel_value], [0, 255]))
  633. #         image_training[i, j] = np.array([pixel_value] * 3)
  634. #         image_training[i, -1] = np.array([128, 128, 128])
  635. #     if label == 0:
  636. #         image_training[i, -1] = np.array([0, 128, 0])
  637. #     elif label == 1:
  638. #         image_training[i, -1] = np.array([255, 0, 0])
  639.  
  640.  
  641.  
  642. # Populate image_training with consistent gray and red/green colors based on the labels in the last column
  643. for i, label in enumerate(y_train):
  644.     for j in range(len(X_train[0])):
  645.         pixel_value = int(np.interp(X_train_normalized[i][j], [min_pixel_value, max_pixel_value], [0, 255]))
  646.         image_training[i, j] = np.array([pixel_value] * 3)
  647.     image_training[i, -1] = np.array([128, 128, 128])
  648.     if label == 0:
  649.         image_training[i, -1] = np.array([0, 128, 0])
  650.     elif label == 1:
  651.         image_training[i, -1] = np.array([255, 0, 0])
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658. from tqdm.notebook import tqdm_notebook
  659.  
  660.  
  661. ###ZDE VLOZTE DATA OD NOVYCH PACIENTU
  662.  
  663.  
  664. # Train the model for 400 epochs
  665. epochs = 138
  666. # Assuming 'new_persons_results' is a list of new persons, where each person is represented as a list of features
  667. new_persons_results = [
  668.    # [101, 112],
  669.    # [0.54422416, 0.02778176],
  670.    # [22.57372914, 17.96922325],
  671. #    [22.57372914, 17.96922325]
  672.     # Add more new persons as needed
  673. #          [23.65780072, 18.8599168 ],
  674. #          [22.57372914, 17.96922325],
  675. #          [32.55342397, 29.46365141],
  676. #          [ 6.71803504, 25.70466547],
  677. #          [ 6.71803504, 25.70466547],
  678. #          [14.40191857, 16.77085649],
  679. #          [17.45790731, 21.76521471],
  680. #          [2110.02796947, 73.45445955],
  681. #          [30.29513837, 62.90111289],
  682. #          [15.1289778,  32.40267702],
  683.  
  684.  [23.65780072, 18.8599168 ],
  685.  [22.57372914, 17.96922325],
  686.  [32.55342397, 29.46365141],
  687.  [ 6.71803504, 25.70466547],
  688.  [14.40191857, 16.77085649],
  689.  [17.45790731, 21.76521471],
  690.  [20.02796947, 73.45445955],
  691.  [26.2042, 10.6782],
  692.  [35.7258, 82.8027],
  693.  
  694. ]
  695.  
  696. import sys
  697.  
  698. for epoch in tqdm_notebook(range(epochs)):
  699.     history = model.fit(X_train_normalized, np.array(y_train), epochs=1, verbose=0, shuffle=True)
  700.     accuracy_history.append(history.history['accuracy'][0])
  701.  
  702.     if epoch == 1:
  703.         # Normalize the testing data
  704.         X_test_normalized = (X_test - min_values) / (max_values - min_values)
  705.         y_pred_after_2nd_epoch = model.predict(X_test_normalized)
  706.         y_pred_binary_after_2nd_epoch = [1 if pred >= 0.5 else 0 for pred in y_pred_after_2nd_epoch]
  707.         image_testing_before_2nd_epoch = create_image(X_test_normalized, y_pred_binary_after_2nd_epoch, label_colors_testing)
  708.  
  709.     if epoch >= epochs-1:
  710.         print(f"HERE HERE Epoch: {epoch}, Epochs: {epochs}\n")
  711.         sys.stdout.flush()
  712.  
  713.         # Iterate through new persons
  714.         for idx, personNEW_results in enumerate(new_persons_results, start=0):
  715.             # Ensure that personNEW_results has the same number of features as the model expects
  716.             assert len(personNEW_results) == len(X_train[0]), "Mismatch in the number of features."
  717.  
  718.             personNEW_results_normalized = (np.array(personNEW_results) - min_values) / (max_values - min_values)
  719.  
  720.             personNEW_prediction = model.predict(np.array([personNEW_results_normalized]))
  721.             personNEW_label = 1 if personNEW_prediction >= 0.5 else 0
  722.             y_pred_after_50_epochs = model.predict(X_test_normalized)
  723.             y_pred_binary_after_50_epochs = [1 if pred >= 0.5 else 0 for pred in y_pred_after_50_epochs]
  724.             image_testing_after_50_epochs = create_image(X_test_normalized, y_pred_binary_after_50_epochs, label_colors_testing)
  725.  
  726.             # Create an image for the new person
  727.             image_personNEW = create_imageN([personNEW_results_normalized], [personNEW_label], label_colors)
  728.  
  729.             # Display the images
  730.             plt.figure(figsize=(5, 5))
  731.             plt.imshow(image_personNEW)
  732.             plt.title(f"New Person {idx}\nLabel: {personNEW_label}, Prediction: {personNEW_prediction},personNEW_results: {personNEW_results}")
  733.             plt.axis("off")
  734.             plt.show()
  735.  
  736.  
  737. # Display the images
  738. plt.figure(figsize=(25, 15))
  739. plt.subplot(2, 2, 1)
  740. plt.imshow(image_training)
  741. plt.title("Training Data")
  742. plt.axis("off")
  743.  
  744. plt.subplot(2, 2, 2)
  745. plt.imshow(image_testing_before_2nd_epoch)
  746. plt.title("Testing Data (2nd Epoch)")
  747. plt.axis("off")
  748.  
  749. plt.subplot(2, 2, 3)
  750. plt.imshow(image_testing_after_50_epochs)
  751. plt.title(f"Testing Data ({epochs} Epochs)")
  752. plt.axis("off")
  753.  
  754. plt.subplot(2, 2, 4)
  755. plt.imshow(image_personNEW)
  756. plt.title(f"New Person\nLabel: {personNEW_label},[{personNEW_prediction}]")
  757. plt.axis("off")
  758.  
  759. # Plot accuracy history
  760. plt.figure(figsize=(12, 5))
  761. plt.plot(range(1, epochs + 1), accuracy_history, marker='o')
  762. plt.title('Accuracy Over Epochs')
  763. plt.xlabel('Epochs')
  764. plt.ylabel('Accuracy')
  765. plt.grid()
  766.  
  767. # Print normalized data
  768. print("Normalized PersonNEW Data:")
  769. print(personNEW_results_normalized)
  770.  
  771. plt.show()
  772.  
  773. print("X_train before normalization:")
  774. print(X_train)
  775. print("X_test before normalization:")
  776. print(X_test)
  777.  
  778. import seaborn as sns
  779.  
  780.  
  781. print("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
  782. print(X_test)
  783. print("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHH")
  784. print(X_train)
  785. print("LLLLLLLLLLLLLLLLLLLLLLLLLLLLL")
  786.  
  787.  
  788. # y_pred_binary = [1 if pred >= 0.5 else 0 for pred in model.predict(X_test_normalized)]
  789.  
  790. # # Create confusion matrix
  791. # conf_matrix = confusion_matrix(y_train, y_pred_binary)
  792. # print(conf_matrix)
  793.  
  794.  
  795. from sklearn.metrics import confusion_matrix
  796. from tensorflow.keras.utils import to_categorical
  797.  
  798. # # Normalize the training data
  799. # min_values = np.min(np.concatenate([X_train, X_test], axis=0), axis=0)
  800. # max_values = np.max(np.concatenate([X_train, X_test], axis=0), axis=0)
  801. # X_train_normalized = (X_train - min_values) / (max_values - min_values)
  802. # X_test_normalized = (X_test - min_values) / (max_values - min_values)
  803.  
  804. np.set_printoptions(threshold=np.inf, precision=4, suppress=True)
  805.  
  806.  
  807. # # Assuming X_test_normalized and y_test are your test set data
  808. # y_pred_binary = [1 if pred >= 0.5 else 0 for pred in model.predict(X_test_normalized)]
  809.  
  810. # # Create confusion matrix using the test set
  811. # conf_matrix = confusion_matrix(y_test, y_pred_binary)
  812. # print(conf_matrix)
  813.  
  814.  
  815.  
  816. # plt.figure(figsize=(6, 6))
  817. # sns.heatmap(conf_matrix, annot=True, fmt="d", cmap="Blues", xticklabels=['Predicted 0', 'Predicted 1'], yticklabels=['Actual 0', 'Actual 1'])
  818. # plt.xlabel("Predicted Label")
  819. # plt.ylabel("True Label")
  820. # plt.title("Confusion Matrix")
  821. # plt.show()
  822.  
  823. # X_train = np.array(X_train)
  824. # y_train_one_hot = np.array(y_train_one_hot)
  825.  
  826. # Rozdělení dat na trénovací a testovací množiny
  827. ###X_train, X_test, y_train, y_test = data[:num_training_rows], data[-num_testing_rows:], labels[:num_training_rows], labels[-num_testing_rows:]
  828.  
  829. ###X_train, X_test, y_train, y_test = data[:num_training_rows], data[:num_training_rows], labels[:num_training_rows], labels[:num_training_rows]
  830. X_train, X_test, y_train, y_test = data[:num_training_rows], data[:num_testing_rows], labels[:num_training_rows], labels[:num_testing_rows]
  831.  
  832. import numpy as np
  833. import matplotlib.pyplot as plt
  834. from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
  835. import tensorflow as tf
  836. import seaborn as sns
  837.  
  838. # Assuming data splitting and model definition have been done correctly
  839.  
  840. # Compile the model
  841. model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
  842.  
  843. # Train the model
  844. print("Training Start")
  845. for epoch in tqdm_notebook(range(100), desc="Training Progress"):
  846.     model.fit(np.array(X_train_normalized), np.array(y_train), epochs=1, verbose=0)
  847. print("Training Complete")
  848.  
  849. # Generate predictions from the model
  850. predictions = (model.predict(X_test_normalized) > 0.5).astype(int)
  851.  
  852. # Convert y_test to a numpy array and then to binary labels
  853. y_test_array = np.array(y_test)  # Convert y_test to a numpy array
  854. y_test_binary = (y_test_array > 0.5).astype(int)  # Convert to binary
  855.  
  856. # Compute the confusion matrix
  857. conf_matrix = confusion_matrix(y_test_binary, predictions)
  858.  
  859. # Evaluate the model's performance
  860. accuracy = accuracy_score(y_test_binary, predictions)
  861. precision = precision_score(y_test_binary, predictions)
  862. recall = recall_score(y_test_binary, predictions)
  863. f1 = f1_score(y_test_binary, predictions)
  864.  
  865. # Display the confusion matrix
  866. sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
  867. plt.xlabel('Predicted')
  868. plt.ylabel('Actual')
  869. plt.title('Confusion Matrix')
  870. plt.show()
  871.  
  872. print(f"Accuracy: {accuracy:.4f}")
  873. print(f"Precision: {precision:.4f}")
  874. print(f"Recall: {recall:.4f}")
  875. print(f"F1 Score: {f1:.4f}")
  876.  
  877. print(f"Confusion Matrix2122:\n{conf_matrix}")
  878.  
  879.  
  880. import random
  881.  
  882. def find_best_pair(min_val, max_val, num_features, model, min_values, max_values):
  883.     best_pair = None
  884.     best_prediction = 1
  885.     for _ in range(1000):  # Number of iterations to find the best pair
  886.         new_data = np.random.uniform(min_val, max_val, num_features)
  887.         new_data_normalized = (new_data - min_values) / (max_values - min_values)
  888.        
  889.         # Suppress model output
  890.         tf.get_logger().setLevel('ERROR')
  891.         with tf.device('/CPU:0'):  # Ensure to run on CPU to minimize unwanted logs
  892.             prediction = model.predict(np.array([new_data_normalized]), verbose=0)[0][0]
  893.         tf.get_logger().setLevel('INFO')
  894.        
  895.         if prediction < best_prediction:
  896.             best_prediction = prediction
  897.             best_pair = new_data
  898.     return best_pair, best_prediction
  899.  
  900.  
  901.  
  902. best_pair, best_prediction = find_best_pair(min_values, max_values, len(X_train[0]), model, min_values, max_values)
  903.  
  904.  
  905. def find_worst_pair(min_val, max_val, num_features, model, min_values, max_values):
  906.     worst_pair = None
  907.     worst_prediction = 0
  908.     for _ in range(1000):  # Number of iterations to find the best pair
  909.         new_data = np.random.uniform(min_val, max_val, num_features)
  910.         new_data_normalized = (new_data - min_values) / (max_values - min_values)
  911.        
  912.         # Suppress model output
  913.         tf.get_logger().setLevel('ERROR')
  914.         with tf.device('/CPU:0'):  # Ensure to run on CPU to minimize unwanted logs
  915.             prediction = model.predict(np.array([new_data_normalized]), verbose=0)[0][0]
  916.         tf.get_logger().setLevel('INFO')
  917.        
  918.         if prediction > worst_prediction:
  919.             worst_prediction = prediction
  920.             worst_pair = new_data
  921.     return worst_pair, worst_prediction
  922.  
  923.  
  924.  
  925. worst_pair, worst_prediction = find_worst_pair(min_values, max_values, len(X_train[0]), model, min_values, max_values)
  926.  
  927.  
  928. print(f"Best Pair: {best_pair}, Best Prediction: {best_prediction}")
  929. print(f"Worst Pair: {worst_pair}, Worst Prediction: {worst_prediction}")
  930.  
  931.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement