Advertisement
max2201111

convolution OK!

May 29th, 2024
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 34.35 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.  
  112.  
  113. def create_imageN(data, predictions, label_colors=None):
  114.     num_training_rows = len(data)  # Set the number of rows based on the data
  115.     num_columns = len(data[0])
  116.  
  117.     image_training = np.zeros((num_training_rows, num_columns + 1, 3), dtype=np.uint8)
  118.  
  119.     min_pixel_value = np.min(X_train_normalized)
  120.     max_pixel_value = np.max(X_train_normalized)
  121.  
  122.  
  123.  
  124.  
  125.     for i in range(num_training_rows):
  126.         # Normalize the first columns independently
  127.         for j in range(num_columns):
  128.             pixel_value = int(np.interp(data[i][j], [min_pixel_value, max_pixel_value], [0, 255]))
  129.             image_training[i, j] = np.array([pixel_value] * 3)
  130.  
  131.         # Normalize the last column separately to achieve grayscale
  132.         pixel_value_last = int(np.interp(data[i][-1], [min_pixel_value, max_pixel_value], [0, 255]))
  133.         image_training[i, -1] = np.array([pixel_value_last] * 3)
  134.  
  135.         # Use the specified color for the last column based on the label
  136.         if label_colors is not None:
  137.             image_training[i, -1] = label_colors[predictions[i]]
  138.  
  139.     return image_training
  140.  
  141.  
  142.  
  143.  
  144. # Load data from a file
  145. #file_path = 'C:/Users/Hynek/Desktop/example4.txt'
  146. from google.colab import files
  147. uploaded = files.upload()
  148.  
  149. # Tento kód otevře dialogové okno pro výběr souboru z vašeho počítače.
  150. import io
  151. import pandas as pd
  152.  
  153. # Předpokládáme, že jste nahráli CSV soubor
  154. for fn in uploaded.keys():
  155.   print('User uploaded file "{name}" with length {length} bytes'.format(
  156.       name=fn, length=len(uploaded[fn])))
  157.   path = io.BytesIO(uploaded[fn])  # Pro soubory, které potřebují být čteny jako binární objekty
  158.   df = pd.read_csv(path)
  159.   print(df.head())  # Vypíše prvních pět řádků DataFrame
  160.  
  161.  
  162. all_results = []
  163. #with open(file_path, 'r') as file:
  164. #    file_content = file.read()
  165.  
  166. # Execute the content as Python code
  167. ##exec(file_content)
  168.  
  169. import os
  170. import shutil
  171. import ast
  172.  
  173. for filename in uploaded.keys():
  174.     original_path = f"/content/{filename}"
  175.     destination_path = os.path.join("/content/", "/content/DATA2")
  176.     shutil.move(original_path, destination_path)
  177.     print(f"Soubor {filename} byl přesunut do {destination_path}")
  178.  
  179. file_path = '/content/DATA2'  # Cesta k souboru
  180. with open(file_path, 'r') as file:
  181.     code = file.read()
  182.  
  183. A_list = ast.literal_eval(code)
  184.  
  185.  
  186. # Převod na NumPy pole
  187. A = np.array(A_list)
  188.  
  189. #exec(code)
  190.  
  191. # Now, all_results contains lists corresponding to each row in the CSV file
  192. ##print(all_results)
  193.  
  194. # Assign values to variables dynamically based on the rows of matrix A
  195. for i, row in enumerate(A, start=1):
  196.     globals()[f"person{i}_results"] = list(row)
  197.  
  198. # Print the assigned variables
  199. for i in range(1, len(A) + 1):
  200.   #  print(f"person{i}_results {globals()[f'person{i}_results']}")
  201.     all_results.append(f"person{i}_results")
  202. ##print(all_results)
  203.  
  204.  
  205.  
  206. result_variables = []
  207.  
  208. # Loop through the variable names and get the corresponding variables using globals()
  209. for var_name in all_results:
  210.     result_variables.append(globals()[var_name])
  211.  
  212. # Now, result_variables contains the variables with names specified in variable_names
  213. #print(result_variables)
  214.  
  215. all_results = result_variables
  216. new_persons_results = result_variables
  217.  
  218.  
  219.  
  220. labels = [results[-1] for results in all_results]
  221.  
  222. # Odstranění posledního sloupce z datasetu
  223. data = [results[:-1] for results in all_results]
  224.  
  225. # Definice počtu řádků pro trénování a testování
  226. num_training_rows = 50
  227. num_testing_rows = 50
  228.  
  229. # Rozdělení datasetu na trénovací a testovací sady
  230. X_train, X_test, y_train, y_test = data[:num_training_rows], data[:num_testing_rows], labels[:num_training_rows], labels[:num_testing_rows]
  231.  
  232. # Převod na NumPy pole
  233. X_train = np.array(X_train)
  234. X_test = np.array(X_test)
  235. y_train = np.array(y_train)
  236. y_test = np.array(y_test)
  237.  
  238. # # Normalizace dat (s ohledem na -1)
  239. # min_values = np.min(X_train[X_train != -1], axis=0)
  240. # max_values = np.max(X_train[X_train != -1], axis=0)
  241. # X_train_normalized = (X_train - min_values) / (max_values - min_values)
  242.  
  243.  
  244.  
  245. # import numpy as np
  246. # import tensorflow as tf
  247. # import matplotlib.pyplot as plt
  248. # from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
  249. # import seaborn as sns
  250. # from tqdm.notebook import tqdm_notebook
  251.  
  252.  
  253.  
  254. import numpy as np
  255. import matplotlib.pyplot as plt
  256. import tensorflow as tf
  257. from tqdm.notebook import tqdm_notebook
  258.  
  259. # Průměry (mu) a směrodatné odchylky (sigma)
  260. mu = np.mean(X_train, axis=0)
  261. sigma = np.std(X_train, axis=0)
  262.  
  263. # Normalizace každého sloupce zvlášť
  264. X_train_standardized = (X_train - mu) / sigma
  265. X_test_standardized = (X_test - mu) / sigma
  266.  
  267. # Vylepšený model
  268. model = tf.keras.Sequential([
  269.     tf.keras.layers.Dense(256, activation='relu', input_shape=(len(X_train[0]),)),
  270.     tf.keras.layers.Dropout(0.3),
  271.     tf.keras.layers.Dense(128, activation='relu'),
  272.     tf.keras.layers.Dropout(0.3),
  273.     tf.keras.layers.Dense(64, activation='relu'),
  274.     tf.keras.layers.Dropout(0.3),
  275.     tf.keras.layers.Dense(1, activation='sigmoid')
  276. ])
  277.  
  278. # Použití Adam optimizer s learning rate schedulerem
  279. lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
  280.     initial_learning_rate=1e-3,
  281.     decay_steps=10000,
  282.     decay_rate=0.9
  283. )
  284. optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
  285.  
  286. # Kompilace modelu
  287. model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
  288.  
  289. # Lists to store accuracy values
  290. accuracy_history = []
  291.  
  292. # Create images for the training data
  293. image_training = np.zeros((num_training_rows, len(X_train[0]) + 1, 3), dtype=np.uint8)
  294.  
  295. min_pixel_value = np.min(X_train_standardized, axis=0)
  296. max_pixel_value = np.max(X_train_standardized, axis=0)
  297.  
  298. for i, label in enumerate(y_train):
  299.     for j in range(len(X_train_standardized[0])):
  300.         pixel_value = int(np.interp(X_train_standardized[i][j], [min_pixel_value[j], max_pixel_value[j]], [0, 255]))
  301.         image_training[i, j] = np.array([pixel_value] * 3)
  302.     image_training[i, -1] = np.array([128, 128, 128])
  303.     if label == 0:
  304.         image_training[i, -1] = np.array([0, 128, 0])
  305.     elif label == 1:
  306.         image_training[i, -1] = np.array([255, 0, 0])
  307.  
  308. # Training the model
  309. epochs = 139
  310. new_persons_results = [
  311.     # [23.65780072, 18.8599168],
  312.     # [22.57372914, 17.96922325],
  313.     # [32.55342397, 29.46365141],
  314.     # [ 6.71803504, 25.70466547],
  315.     # [14.40191857, 16.77085649],
  316.     # [17.45790731, 21.76521471],
  317.     # [20.02796947, 73.45445955],
  318.     # [26.2042, 10.6782],
  319.     # [35.7258, 12.8027],
  320.     # [21.2, 7.8],
  321.     # [50.1, 40.2],
  322.     # [32.739, 42.0152],
  323.     # [28.1, 10.1],
  324.     # [32.1656, 46.005 ],
  325.     # [24.9647, 51.3433],
  326.     # [34.6134, 44.4012],
  327. [0.0697400418162155,0.048866857264291144,0.28641370855472326,0.2721997143501177],
  328. [0.14159602676789837,0.1747877034447084,0.35616475477076587,0.3349487498168958],
  329. [0.11173253224821383,0.18794447828677996,0.3254176976987727,0.3413023918178341],
  330. [0.09630381764770453,0.05449810810962146,0.26767869268577593,0.21134056616439179],
  331. [0.17834821693532132,0.18466538062866059,0.3199711146234129,0.3968137366419059],
  332. [0.06045619825051427,0.05598696779492471,0.21592696351263593,0.22040624440430515],
  333. [0.08666288081774745,0.015388075894536557,0.2041876616268118,0.20706370434663773],
  334. [0.03130184508345673,0.015266595360551428,0.27183777103946916,0.2867664339707584],
  335. [0.05547626859495597,0.05808291988099526,0.2542166524648567,0.2573313511422864],
  336. [0.1772, 0.0076, 0.3565, 0.2584],
  337. ]
  338.  
  339. import sys
  340.  
  341. for epoch in tqdm_notebook(range(epochs)):
  342.     history = model.fit(X_train_standardized, np.array(y_train), epochs=1, verbose=0, shuffle=False)
  343.     accuracy_history.append(history.history['accuracy'][0])
  344.  
  345.     if epoch == 1:
  346.         # Normalize the testing data
  347.         X_test_standardized = (X_test - mu) / sigma
  348.         y_pred_after_2nd_epoch = model.predict(X_test_standardized)
  349.         y_pred_binary_after_2nd_epoch = [1 if pred >= 0.5 else 0 for pred in y_pred_after_2nd_epoch]
  350.         image_testing_before_2nd_epoch = create_image(X_test_standardized, y_pred_binary_after_2nd_epoch, label_colors_testing)
  351.  
  352.     if epoch >= epochs-1:
  353.         print(f"HERE HERE Epoch: {epoch}, Epochs: {epochs}\n")
  354.         sys.stdout.flush()
  355.  
  356.         # Iterate through new persons
  357.         for idx, personNEW_results in enumerate(new_persons_results, start=0):
  358.             # Ensure that personNEW_results has the same number of features as the model expects
  359.             assert len(personNEW_results) == len(X_train[0]), "Mismatch in the number of features."
  360.  
  361.             personNEW_results_standardized = (np.array(personNEW_results) - mu) / sigma
  362.  
  363.             personNEW_prediction = model.predict(np.array([personNEW_results_standardized]))
  364.             personNEW_label = 1 if personNEW_prediction >= 0.5 else 0
  365.             y_pred_after_50_epochs = model.predict(X_test_standardized)
  366.             y_pred_binary_after_50_epochs = [1 if pred >= 0.5 else 0 for pred in y_pred_after_50_epochs]
  367.             image_testing_after_50_epochs = create_image(X_test_standardized, y_pred_binary_after_50_epochs, label_colors_testing)
  368.  
  369.             # Create an image for the new person
  370.             image_personNEW = create_imageN([personNEW_results_standardized], [personNEW_label], label_colors)
  371.  
  372.             # Display the images
  373.             plt.figure(figsize=(5, 5))
  374.             plt.imshow(image_personNEW)
  375.             plt.title(f"New Person {idx}\nLabel: {personNEW_label}, Prediction: {personNEW_prediction}, personNEW_results: {personNEW_results}")
  376.             plt.axis("off")
  377.             plt.show()
  378.  
  379. # Display the images
  380. plt.figure(figsize=(25, 15))
  381. plt.subplot(2, 2, 1)
  382. plt.imshow(image_training)
  383. plt.title("Training Data")
  384. plt.axis("off")
  385.  
  386. plt.subplot(2, 2, 2)
  387. plt.imshow(image_testing_before_2nd_epoch)
  388. plt.title("Testing Data (2nd Epoch)")
  389. plt.axis("off")
  390.  
  391. plt.subplot(2, 2, 3)
  392. plt.imshow(image_testing_after_50_epochs)
  393. plt.title(f"Testing Data ({epochs} Epochs)")
  394. plt.axis("off")
  395.  
  396. plt.subplot(2, 2, 4)
  397. plt.imshow(image_personNEW)
  398. plt.title(f"New Person\nLabel: {personNEW_label},[{personNEW_prediction}]")
  399. plt.axis("off")
  400.  
  401. # Plot accuracy history
  402. plt.figure(figsize=(12, 5))
  403. plt.plot(range(1, epochs + 1), accuracy_history, marker='o')
  404. plt.title('Accuracy Over Epochs')
  405. plt.xlabel('Epochs')
  406. plt.ylabel('Accuracy')
  407. plt.grid()
  408.  
  409. # Print standardized data
  410. print("Standardized PersonNEW Data:")
  411. print(personNEW_results_standardized)
  412.  
  413. plt.show()
  414.  
  415. print("X_train before standardization:")
  416. print(X_train)
  417. print("X_test before standardization:")
  418. print(X_test)
  419.  
  420. import seaborn as sns
  421.  
  422. print("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
  423. print(X_test)
  424. print("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHH")
  425. print(X_train)
  426. print("LLLLLLLLLLLLLLLLLLLLLLLLLLLLL")
  427.  
  428. # y_pred_binary = [1 if pred >= 0.5 else 0 for pred in model.predict(X_test_standardized)]
  429.  
  430. # # Create confusion matrix
  431. # conf_matrix = confusion_matrix(y_train, y_pred_binary)
  432. # print(conf_matrix)
  433.  
  434. from sklearn.metrics import confusion_matrix
  435. from tensorflow.keras.utils import to_categorical
  436.  
  437. np.set_printoptions(threshold=np.inf, precision=4, suppress=True)
  438.  
  439. # Assuming X_test_standardized and y_test are your test set data
  440. y_pred_binary = [1 if pred >= 0.5 else 0 for pred in model.predict(X_test_standardized)]
  441.  
  442. # Create confusion matrix using the test set
  443. conf_matrix = confusion_matrix(y_test, y_pred_binary)
  444. print(conf_matrix)
  445.  
  446. plt.figure(figsize=(6, 6))
  447. sns.heatmap(conf_matrix, annot=True, fmt="d", cmap="Blues", xticklabels=['Predicted 0', 'Predicted 1'], yticklabels=['Actual 0', 'Actual 1'])
  448. plt.xlabel("Predicted Label")
  449. plt.ylabel("True Label")
  450. plt.title("Confusion Matrix")
  451. plt.show()
  452.  
  453. X_train = np.array(X_train)
  454. #y_train_one_hot = np.array(y_train_one_hot)
  455.  
  456. # Rozdělení dat na trénovací a testovací množiny
  457. X_train, X_test, y_train, y_test = data[:num_training_rows], data[:num_testing_rows], labels[:num_training_rows], labels[:num_testing_rows]
  458.  
  459. import numpy as np
  460. import matplotlib.pyplot as plt
  461. from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
  462. import tensorflow as tf
  463. import seaborn as sns
  464.  
  465. # Assuming data splitting and model definition have been done correctly
  466.  
  467. # Compile the model
  468. model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
  469.  
  470. # Train the model
  471. print("Training Start")
  472. for epoch in tqdm_notebook(range(100), desc="Training Progress"):
  473.     model.fit(np.array(X_train_standardized), np.array(y_train), epochs=1, verbose=0)
  474. print("Training Complete")
  475.  
  476. # Generate predictions from the model
  477. predictions = (model.predict(X_test_standardized) > 0.5).astype(int)
  478.  
  479. # Convert y_test to a numpy array and then to binary labels
  480. y_test_array = np.array(y_test)  # Convert y_test to a numpy array
  481. y_test_binary = (y_test_array > 0.5).astype(int)  # Convert to binary
  482.  
  483. # Compute the confusion matrix
  484. conf_matrix = confusion_matrix(y_test_binary, predictions)
  485.  
  486. # Evaluate the model's performance
  487. accuracy = accuracy_score(y_test_binary, predictions)
  488. precision = precision_score(y_test_binary, predictions)
  489. recall = recall_score(y_test_binary, predictions)
  490. f1 = f1_score(y_test_binary, predictions)
  491.  
  492. # Display the confusion matrix
  493. sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
  494. plt.xlabel('Predicted')
  495. plt.ylabel('Actual')
  496. plt.title('Confusion Matrix')
  497. plt.show()
  498.  
  499. print(f"Accuracy: {accuracy:.4f}")
  500. print(f"Precision: {precision:.4f}")
  501. print(f"Recall: {recall:.4f}")
  502. print(f"F1 Score: {f1:.4f}")
  503.  
  504. print(f"Confusion Matrix2122:\n{conf_matrix}")
  505.  
  506. import random
  507.  
  508. def find_best_pair(min_val, max_val, num_features, model, min_values, max_values):
  509.     best_pair = None
  510.     best_prediction = 1
  511.     for _ in range(1000):  # Number of iterations to find the best pair
  512.         new_data = np.random.uniform(min_val, max_val, num_features)
  513.         new_data_standardized = (new_data - mu) / sigma
  514.        
  515.         # Suppress model output
  516.         tf.get_logger().setLevel('ERROR')
  517.         with tf.device('/CPU:0'):  # Ensure to run on CPU to minimize unwanted logs
  518.             prediction = model.predict(np.array([new_data_standardized]), verbose=0)[0][0]
  519.         tf.get_logger().setLevel('INFO')
  520.        
  521.         if prediction < best_prediction:
  522.             best_prediction = prediction
  523.             best_pair = new_data
  524.     return best_pair, best_prediction
  525.  
  526. best_pair, best_prediction = find_best_pair(min_values, max_values, len(X_train[0]), model, min_values, max_values)
  527.  
  528. def find_worst_pair(min_val, max_val, num_features, model, min_values, max_values):
  529.     worst_pair = None
  530.     worst_prediction = 0
  531.     for _ in range(1000):  # Number of iterations to find the best pair
  532.         new_data = np.random.uniform(min_val, max_val, num_features)
  533.         new_data_standardized = (new_data - mu) / sigma
  534.        
  535.         # Suppress model output
  536.         tf.get_logger().setLevel('ERROR')
  537.         with tf.device('/CPU:0'):  # Ensure to run on CPU to minimize unwanted logs
  538.             prediction = model.predict(np.array([new_data_standardized]), verbose=0)[0][0]
  539.         tf.get_logger().setLevel('INFO')
  540.        
  541.         if prediction > worst_prediction:
  542.             worst_prediction = prediction
  543.             worst_pair = new_data
  544.     return worst_pair, worst_prediction
  545.  
  546. worst_pair, worst_prediction = find_worst_pair(min_values, max_values, len(X_train[0]), model, min_values, max_values)
  547.  
  548. print(f"Best Pair: {best_pair}, Best Prediction: {best_prediction}")
  549. print(f"Worst Pair: {worst_pair}, Worst Prediction: {worst_prediction}")
  550.  
  551.  
  552.  
  553. import numpy as np
  554. import matplotlib.pyplot as plt
  555. import tensorflow as tf
  556. from sklearn.metrics import recall_score, confusion_matrix, accuracy_score, precision_score, f1_score
  557. import seaborn as sns
  558. from tqdm.notebook import tqdm_notebook
  559. from sklearn.decomposition import PCA
  560. from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
  561.  
  562.  
  563.  
  564. # Rozdělení na vstupní data (X) a cílové proměnné (y)
  565. X = A[:, :-1]
  566. y = A[:, -1]
  567.  
  568. # Rozdělení na trénovací a testovací sadu (v tomto příkladě použijeme celou sadu jako trénovací pro jednoduchost)
  569. X_train, y_train = X, y
  570. X_test, y_test = X, y
  571.  
  572. # Výpočet průměru a směrodatné odchylky pro každý sloupec
  573. mu = np.mean(X_train, axis=0)
  574. sigma = np.std(X_train, axis=0)
  575.  
  576. # Normalizace každého sloupce zvlášť
  577. X_train_standardized = (X_train - mu) / sigma
  578.  
  579. # Normalizace testovacích dat
  580. X_test_standardized = (X_test - mu) / sigma
  581.  
  582. # Definice modelu
  583. model = tf.keras.Sequential([
  584.     tf.keras.layers.Dense(256, activation='relu', input_shape=(X_train_standardized.shape[1],)),
  585.     tf.keras.layers.Dropout(0.3),
  586.     tf.keras.layers.Dense(128, activation='relu'),
  587.     tf.keras.layers.Dropout(0.3),
  588.     tf.keras.layers.Dense(64, activation='relu'),
  589.     tf.keras.layers.Dropout(0.3),
  590.     tf.keras.layers.Dense(1, activation='sigmoid')
  591. ])
  592.  
  593. # Použití Adam optimizer s learning rate schedulerem
  594. lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
  595.     initial_learning_rate=1e-3,
  596.     decay_steps=10000,
  597.     decay_rate=0.9
  598. )
  599. optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
  600.  
  601. # Kompilace modelu
  602. model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy', tf.keras.metrics.Recall()])
  603.  
  604. # Trénování modelu
  605. history = model.fit(X_train_standardized, y_train, epochs=50, verbose=0, shuffle=False)
  606.  
  607. # Predikce
  608. y_pred_prob = model.predict(X_test_standardized)
  609. y_pred = (y_pred_prob > 0.5).astype(int)
  610.  
  611. # Výpočet metrik
  612. recall = recall_score(y_test, y_pred)
  613. conf_matrix = confusion_matrix(y_test, y_pred)
  614.  
  615. # Vyhodnocení výkonu modelu
  616. accuracy = accuracy_score(y_test, y_pred)
  617. precision = precision_score(y_test, y_pred)
  618. f1 = f1_score(y_test, y_pred)
  619.  
  620. # Výpis metrik
  621. print(f"Recall: {recall:.4f}")
  622. print(f"Accuracy: {accuracy:.4f}")
  623. print(f"Precision: {precision:.4f}")
  624. print(f"F1 Score: {f1:.4f}")
  625. print(f"Confusion Matrix:\n{conf_matrix}")
  626.  
  627. # Zobrazení confusion matrix
  628. sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
  629. plt.xlabel('Predicted')
  630. plt.ylabel('Actual')
  631. plt.title('Confusion Matrix')
  632. plt.show()
  633.  
  634. print("KKKKKKKKKKKKK")
  635.  
  636.  
  637.  
  638. # import numpy as np
  639. # import tensorflow as tf
  640. # import matplotlib.pyplot as plt
  641. # from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
  642. # import seaborn as sns
  643. # from tqdm.notebook import tqdm_notebook
  644.  
  645. # # Výpočet průměru a směrodatné odchylky pro každý sloupec
  646. # mu = np.mean(X_train, axis=0)
  647. # sigma = np.std(X_train, axis=0)
  648.  
  649. # # Normalizace každého sloupce zvlášť
  650. # X_train_standardized = (X_train - mu) / sigma
  651.  
  652. # # Normalizace testovacích dat
  653. # X_test_standardized = (X_test - mu) / sigma
  654.  
  655. # # Převedení dat na správný tvar pro CNN (přidání kanálu)
  656. # X_train_standardized = X_train_standardized[..., np.newaxis]
  657. # X_test_standardized = X_test_standardized[..., np.newaxis]
  658.  
  659. # # Definice modelu
  660. # model = tf.keras.Sequential([
  661. #     tf.keras.layers.Conv1D(32, 2, activation='relu', input_shape=(X_train_standardized.shape[1], 1)),
  662. #     tf.keras.layers.MaxPooling1D(2),
  663. #     tf.keras.layers.Flatten(),
  664. #     tf.keras.layers.Dense(128, activation='relu'),
  665. #     tf.keras.layers.Dropout(0.3),
  666. #     tf.keras.layers.Dense(64, activation='relu'),
  667. #     tf.keras.layers.Dropout(0.3),
  668. #     tf.keras.layers.Dense(1, activation='sigmoid')
  669. # ])
  670.  
  671. # # Použití Adam optimizer s learning rate schedulerem
  672. # lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
  673. #     initial_learning_rate=1e-3,
  674. #     decay_steps=10000,
  675. #     decay_rate=0.9
  676. # )
  677. # optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
  678.  
  679. # # Kompilace modelu
  680. # model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy', tf.keras.metrics.Recall()])
  681.  
  682. # # Trénování modelu
  683. # history = model.fit(X_train_standardized, y_train, epochs=50, verbose=0, shuffle=False)
  684.  
  685. # # Predikce
  686. # y_pred_prob = model.predict(X_test_standardized)
  687. # y_pred = (y_pred_prob > 0.5).astype(int)
  688.  
  689. # # Výpočet metrik
  690. # recall = recall_score(y_test, y_pred)
  691. # conf_matrix = confusion_matrix(y_test, y_pred)
  692.  
  693. # # Vyhodnocení výkonu modelu
  694. # accuracy = accuracy_score(y_test, y_pred)
  695. # precision = precision_score(y_test, y_pred)
  696. # f1 = f1_score(y_test, y_pred)
  697.  
  698. # # Výpis metrik
  699. # print(f"Recall: {recall:.4f}")
  700. # print(f"Accuracy: {accuracy:.4f}")
  701. # print(f"Precision: {precision:.4f}")
  702. # print(f"F1 Score: {f1:.4f}")
  703. # print(f"Confusion Matrix:\n{conf_matrix}")
  704.  
  705. # # Zobrazení confusion matrix
  706. # sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
  707. # plt.xlabel('Predicted')
  708. # plt.ylabel('Actual')
  709. # plt.title('Confusion Matrix')
  710. # plt.show()
  711.  
  712. # import numpy as np
  713. # import tensorflow as tf
  714. # import matplotlib.pyplot as plt
  715. # from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
  716. # import seaborn as sns
  717. # from tqdm.notebook import tqdm_notebook
  718.  
  719. # # Výpočet průměru a směrodatné odchylky pro každý sloupec
  720. # mu = np.mean(X_train, axis=0)
  721. # sigma = np.std(X_train, axis=0)
  722.  
  723. # # Normalizace každého sloupce zvlášť
  724. # X_train_standardized = (X_train - mu) / sigma
  725.  
  726. # # Normalizace testovacích dat
  727. # X_test_standardized = (X_test - mu) / sigma
  728.  
  729. # # Převedení dat na správný tvar pro CNN (přidání kanálu)
  730. # X_train_standardized = X_train_standardized[..., np.newaxis]
  731. # X_test_standardized = X_test_standardized[..., np.newaxis]
  732.  
  733. # # Definice modelu
  734. # model = tf.keras.Sequential([
  735. #     tf.keras.layers.Conv1D(32, 2, activation='relu', input_shape=(X_train_standardized.shape[1], 1)),
  736. #     tf.keras.layers.MaxPooling1D(2),
  737. #     tf.keras.layers.Conv1D(64, 2, activation='relu'),
  738. #     tf.keras.layers.MaxPooling1D(2),
  739. #     tf.keras.layers.Flatten(),
  740. #     tf.keras.layers.Dense(128, activation='relu'),
  741. #     tf.keras.layers.Dropout(0.3),
  742. #     tf.keras.layers.Dense(64, activation='relu'),
  743. #     tf.keras.layers.Dropout(0.3),
  744. #     tf.keras.layers.Dense(1, activation='sigmoid')
  745. # ])
  746.  
  747. # # Použití Adam optimizer s learning rate schedulerem
  748. # lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
  749. #     initial_learning_rate=1e-3,
  750. #     decay_steps=10000,
  751. #     decay_rate=0.9
  752. # )
  753. # optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
  754.  
  755. # # Kompilace modelu
  756. # model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy', tf.keras.metrics.Recall()])
  757.  
  758. # # Trénování modelu
  759. # history = model.fit(X_train_standardized, y_train, epochs=50, verbose=0, shuffle=False)
  760.  
  761. # # Predikce
  762. # y_pred_prob = model.predict(X_test_standardized)
  763. # y_pred = (y_pred_prob > 0.5).astype(int)
  764.  
  765. # # Výpočet metrik
  766. # recall = recall_score(y_test, y_pred)
  767. # conf_matrix = confusion_matrix(y_test, y_pred)
  768.  
  769. # # Vyhodnocení výkonu modelu
  770. # accuracy = accuracy_score(y_test, y_pred)
  771. # precision = precision_score(y_test, y_pred)
  772. # f1 = f1_score(y_test, y_pred)
  773.  
  774. # # Výpis metrik
  775. # print(f"Recall: {recall:.4f}")
  776. # print(f"Accuracy: {accuracy:.4f}")
  777. # print(f"Precision: {precision:.4f}")
  778. # print(f"F1 Score: {f1:.4f}")
  779. # print(f"Confusion Matrix:\n{conf_matrix}")
  780.  
  781. # # Zobrazení confusion matrix
  782. # sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
  783. # plt.xlabel('Predicted')
  784. # plt.ylabel('Actual')
  785. # plt.title('Confusion Matrix')
  786. # plt.show()
  787.  
  788. # # Odstranění mezivýstupů
  789. # num_iterations = 500
  790.  
  791. # best_row = None
  792. # best_prediction = None
  793. # best_diff = float('inf')
  794.  
  795. # for _ in range(num_iterations):
  796. #     new_data = np.random.normal(mu, sigma)
  797. #     new_data_standardized = (new_data - mu) / sigma
  798. #     new_data_standardized = new_data_standardized[np.newaxis, ..., np.newaxis]  # Přidání nového rozměru pro CNN
  799. #     prediction_prob = model.predict(new_data_standardized, verbose=0)[0][0]
  800. #     diff = abs(prediction_prob - 0.67)
  801.    
  802. #     if diff < best_diff:
  803. #         best_diff = diff
  804. #         best_row = new_data
  805. #         best_prediction = prediction_prob
  806.  
  807. # print(f"Nejlepší řádek: {best_row}")
  808. # print(f"Predikovaná hodnota: {best_prediction}")
  809. # print(f"Rozdíl: {best_diff}")
  810.  
  811. import numpy as np
  812. import tensorflow as tf
  813. import matplotlib.pyplot as plt
  814. from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
  815. import seaborn as sns
  816. from tqdm.notebook import tqdm_notebook
  817.  
  818. # Výpočet průměru a směrodatné odchylky pro každý sloupec
  819. mu = np.mean(X_train, axis=0)
  820. sigma = np.std(X_train, axis=0)
  821.  
  822. # Normalizace každého sloupce zvlášť
  823. X_train_standardized = (X_train - mu) / sigma
  824.  
  825. # Normalizace testovacích dat
  826. X_test_standardized = (X_test - mu) / sigma
  827.  
  828. # Převedení dat na správný tvar pro CNN (přidání kanálu)
  829. X_train_standardized = X_train_standardized[..., np.newaxis]
  830. X_test_standardized = X_test_standardized[..., np.newaxis]
  831.  
  832. # Definice modelu
  833. model = tf.keras.Sequential([
  834.     tf.keras.layers.Conv1D(32, 1, activation='relu', input_shape=(X_train_standardized.shape[1], 1)),
  835.     tf.keras.layers.MaxPooling1D(1),
  836.     tf.keras.layers.Conv1D(64, 1, activation='relu'),
  837.     tf.keras.layers.MaxPooling1D(1),
  838.     tf.keras.layers.Flatten(),
  839.     tf.keras.layers.Dense(128, activation='relu'),
  840.     tf.keras.layers.Dropout(0.3),
  841.     tf.keras.layers.Dense(64, activation='relu'),
  842.     tf.keras.layers.Dropout(0.3),
  843.     tf.keras.layers.Dense(1, activation='sigmoid')
  844. ])
  845.  
  846. # Použití Adam optimizer s learning rate schedulerem
  847. lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
  848.     initial_learning_rate=1e-3,
  849.     decay_steps=10000,
  850.     decay_rate=0.9
  851. )
  852. optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
  853.  
  854. # Kompilace modelu
  855. model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy', tf.keras.metrics.Recall()])
  856.  
  857. # Trénování modelu
  858. history = model.fit(X_train_standardized, y_train, epochs=50, verbose=0, shuffle=False)
  859.  
  860. # Predikce
  861. y_pred_prob = model.predict(X_test_standardized)
  862. y_pred = (y_pred_prob > 0.5).astype(int)
  863.  
  864. # Výpočet metrik
  865. recall = recall_score(y_test, y_pred)
  866. conf_matrix = confusion_matrix(y_test, y_pred)
  867.  
  868. # Vyhodnocení výkonu modelu
  869. accuracy = accuracy_score(y_test, y_pred)
  870. precision = precision_score(y_test, y_pred)
  871. f1 = f1_score(y_test, y_pred)
  872.  
  873. # Výpis metrik
  874. print(f"Recall: {recall:.4f}")
  875. print(f"Accuracy: {accuracy:.4f}")
  876. print(f"Precision: {precision:.4f}")
  877. print(f"F1 Score: {f1:.4f}")
  878. print(f"Confusion Matrix:\n{conf_matrix}")
  879.  
  880. # Zobrazení confusion matrix
  881. sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
  882. plt.xlabel('Predicted')
  883. plt.ylabel('Actual')
  884. plt.title('Confusion Matrix')
  885. plt.show()
  886.  
  887. # Odstranění mezivýstupů
  888. num_iterations = 500
  889.  
  890. best_row = None
  891. best_prediction = None
  892. best_diff = float('inf')
  893.  
  894. for _ in range(num_iterations):
  895.     new_data = np.random.normal(mu, sigma)
  896.     new_data_standardized = (new_data - mu) / sigma
  897.     new_data_standardized = new_data_standardized[np.newaxis, ..., np.newaxis]  # Přidání nového rozměru pro CNN
  898.     prediction_prob = model.predict(new_data_standardized, verbose=0)[0][0]
  899.     diff = abs(prediction_prob - 0.67)
  900.    
  901.     if diff < best_diff:
  902.         best_diff = diff
  903.         best_row = new_data
  904.         best_prediction = prediction_prob
  905.  
  906. print(f"Nejlepší řádek: {best_row}")
  907. print(f"Predikovaná hodnota: {best_prediction}")
  908. print(f"Rozdíl: {best_diff}")
  909.  
  910. # # Odstranění mezivýstupů
  911. # num_iterations = 50
  912.  
  913. # best_row = None
  914. # best_prediction = None
  915. # best_diff = float('inf')
  916.  
  917. # for _ in range(num_iterations):
  918. #     new_data = np.random.normal(mu, sigma)
  919. #     new_data_standardized = (new_data - mu) / sigma
  920. #     prediction_prob = model.predict(np.array([new_data_standardized]), verbose=0)[0][0]
  921. #     diff = abs(prediction_prob - 0.67)
  922.    
  923. #     if diff < best_diff:
  924. #         best_diff = diff
  925. #         best_row = new_data
  926. #         best_prediction = prediction_prob
  927.  
  928. # print(f"Nejlepší řádek: {best_row}")
  929. # print(f"Predikovaná hodnota: {best_prediction}")
  930. # print(f"Rozdíl: {best_diff}")
  931.  
  932. # Vizualizace výsledků pomocí PCA
  933. X_standardized = (X - mu) / sigma
  934. pca = PCA(n_components=2)  # Snížení na 2 komponenty
  935. X_pca = pca.fit_transform(X_standardized)
  936.  
  937. plt.figure()
  938. plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y)
  939. plt.xlabel('První hlavní komponenta')
  940. plt.ylabel('Druhá hlavní komponenta')
  941. plt.title('PCA na vašich datech')
  942. plt.show()
  943.  
  944. # Vizualizace výsledků pomocí LDA
  945. lda = LDA(n_components=1)
  946. X_lda = lda.fit_transform(X_standardized, y)
  947.  
  948. plt.figure()
  949. plt.scatter(X_lda[:, 0], np.zeros_like(X_lda), c=y)
  950. plt.xlabel('První diskriminační komponenta')
  951. plt.title('LDA s učitelem')
  952. plt.show()
  953.  
  954. # Vytvoření obrazu pro trénovací data
  955. min_pixel_value = -3
  956. max_pixel_value = 3
  957.  
  958. image_training = np.zeros((len(X_train_standardized), len(X_train_standardized[0]) + 1, 3), dtype=np.uint8)
  959.  
  960. for i, label in enumerate(y_train):
  961.     for j in range(len(X_train_standardized[0])):
  962.         pixel_value = int(np.interp(X_train_standardized[i][j], [min_pixel_value, max_pixel_value], [0, 255]))
  963.         image_training[i, j] = np.array([pixel_value] * 3)
  964.     image_training[i, -1] = np.array([128, 128, 128])  # Šedý sloupec pro všechny řádky
  965.     if label == 0:
  966.         image_training[i, -1] = np.array([0, 128, 0])  # Zelený sloupec pro label 0
  967.     elif label == 1:
  968.         image_training[i, -1] = np.array([255, 0, 0])  # Červený sloupec pro label 1
  969.  
  970. # Zobrazení obrazu
  971. #plt.imshow(image_training)
  972. #plt.title("Training Data")#
  973. ##plt.axis("off")
  974. #plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement