Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Navod na pouziti, Mgr. Hynek Mlčoušek, v Brne 2.5.2024
- #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 ","
- # [ [23.657800719276743,18.859916797201468,0],
- # [22.573729142097473,17.96922325097786,0],
- # [32.55342396968757,29.463651408558803,0],
- # [6.718035041529263,25.704665468161718,1],
- # [14.401918566243225,16.770856492924658,0],
- # [17.457907312962234,21.76521470574044,0],
- # [20.02796946568093,73.45445954770891,1],
- # [30.295138369778076,62.901112886193246,1],
- # [15.128977804449633,32.40267702110393,0],
- # [30.179457395820013,58.982492125646104,1],
- # [28.01649701854089,63.92781357637711,1],
- # [16.791838457871147,42.33482314089884,0],
- # [10.583694293380976,19.61926728942497,0],
- # [26.634447074406467,91.96624817360987,1],
- # [26.217868623367643,36.400293587062976,0],
- # [17.689396788624936,60.79797114006423,1],
- # [33.17193822527976,66.75277364959176,1],
- # [23.793952755709153,22.57501437360518,0]]
- #kliknete na cerne tlacitko s trojuhelnickem vlevo nahore
- #pod kodem se objevi moznost spustit dialogove okenko, kliknete na nej
- #soubor, ktery mate z bodu vyse vyberte a nahrajte
- #Najdete v tomto kodu retezec:
- ###ZDE VLOZTE DATA OD NOVYCH PACIENTU
- #Vlozte do pole
- # new_persons_results = []
- # 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
- #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
- #zaroven s tim se vypise realne cislo mezi 0 a 1 znacici jak moc je pacient zdravy (blizke 0) ci nemocny (blizke 1)
- #cisla uprostred pak indikuji zadany oranzovy semafor.
- #je na lekarich nastavit tresholdy (tedy pravdepodobnosti: cisla mezi 0 a 1) ktere pak daji zaver, zda je pacient cerveny, oranzovy ci zeleny
- # prosim o komnetare a vysledky na realnych datech, je zadouci aby radku v matici, tedy pacientu byly stovky a sloupcu desitky
- # Moznosti vyuziti: onkologicka diagnoza vs. zdrava kontorlni skupina, diabetes (pritomnost/nepritomnost), testovani noveho leku oproti placebu atd.
- #kod zaroven vyhodi confusion matici, tedy mozne True Negative a False Positive plus spravne zarazene hodnoty spolu s presnosti,F1 score recall atd.
- #poznamka ke kodu: jde o epxerimentalni verzi, ktera krome skutecne potrebneho kodu obsahuje ladici informace, ruzne duplicity, nadbytecne prikazy atd.
- # 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
- #Dekuji profesoru Petru Dostalovi za namet k teto praci a poskytnuta data, byt je potreba mit data realna
- import numpy as np
- import matplotlib.pyplot as plt
- import tensorflow as tf
- from tqdm import tqdm
- from IPython.display import display
- from IPython.display import Javascript
- display(Javascript('IPython.OutputArea.auto_scroll_threshold = 9999;'))
- label_colors = {0: [0, 128, 0], 1: [255, 0, 0]}
- label_colors_testing = {0: [0, 128, 0], 1: [255, 0, 0]}
- %matplotlib inline
- # Function to create images based on predictions
- def create_image(data, predictions):
- num_rows, num_columns = len(data), len(data[0])
- image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
- for i in range(num_rows):
- for j in range(num_columns):
- pixel_value = int(np.interp(data[i][j], [np.min(data), np.max(data)], [0, 255]))
- image[i, j] = np.array([pixel_value] * 3)
- # Create a gradient based on the normalized values
- gradient_value = int(np.interp(predictions[i], [0, 1], [0, 255]))
- image[i, -1] = np.array([gradient_value] * 3)
- return image
- def create_image(data, predictions):
- num_rows, num_columns = len(data), len(data[0])
- image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
- for i in range(num_rows):
- for j in range(num_columns):
- pixel_value = int(np.interp(data[i][j], [np.min(data), np.max(data)], [0, 255]))
- image[i, j] = np.array([pixel_value] * 3)
- # Use red for class 0 and green for class 1
- if predictions[i] == 0:
- image[i, -1] = np.array([255, 0, 0]) # Red
- elif predictions[i] == 1:
- image[i, -1] = np.array([0, 128, 0]) # Green
- return image
- def create_image(data, predictions, label_colors):
- num_rows, num_columns = len(data), len(data[0])
- image = np.zeros((num_rows, num_columns + 1, 3), dtype=np.uint8)
- for i in range(num_rows):
- for j in range(num_columns):
- pixel_value = int(np.interp(data[i][j], [np.min(data), np.max(data)], [0, 255]))
- image[i, j] = np.array([pixel_value] * 3)
- # Use the specified color for the last column based on the label
- image[i, -1] = label_colors[predictions[i]]
- return image
- def create_imageN(data, predictions, label_colors=None):
- num_training_rows = len(data) # Set the number of rows based on the data
- num_columns = len(data[0])
- image_training = np.zeros((num_training_rows, num_columns + 1, 3), dtype=np.uint8)
- min_pixel_value = np.min(X_train_normalized)
- max_pixel_value = np.max(X_train_normalized)
- for i in range(num_training_rows):
- # Normalize the first columns independently
- for j in range(num_columns):
- pixel_value = int(np.interp(data[i][j], [min_pixel_value, max_pixel_value], [0, 255]))
- image_training[i, j] = np.array([pixel_value] * 3)
- # Normalize the last column separately to achieve grayscale
- pixel_value_last = int(np.interp(data[i][-1], [min_pixel_value, max_pixel_value], [0, 255]))
- image_training[i, -1] = np.array([pixel_value_last] * 3)
- # Use the specified color for the last column based on the label
- if label_colors is not None:
- image_training[i, -1] = label_colors[predictions[i]]
- return image_training
- # Load data from a file
- #file_path = 'C:/Users/Hynek/Desktop/example4.txt'
- from google.colab import files
- uploaded = files.upload()
- # Tento kód otevře dialogové okno pro výběr souboru z vašeho počítače.
- import io
- import pandas as pd
- # Předpokládáme, že jste nahráli CSV soubor
- for fn in uploaded.keys():
- print('User uploaded file "{name}" with length {length} bytes'.format(
- name=fn, length=len(uploaded[fn])))
- path = io.BytesIO(uploaded[fn]) # Pro soubory, které potřebují být čteny jako binární objekty
- df = pd.read_csv(path)
- print(df.head()) # Vypíše prvních pět řádků DataFrame
- all_results = []
- #with open(file_path, 'r') as file:
- # file_content = file.read()
- # Execute the content as Python code
- ##exec(file_content)
- import os
- import shutil
- import ast
- for filename in uploaded.keys():
- original_path = f"/content/{filename}"
- destination_path = os.path.join("/content/", "/content/DATA2")
- shutil.move(original_path, destination_path)
- print(f"Soubor {filename} byl přesunut do {destination_path}")
- file_path = '/content/DATA2' # Cesta k souboru
- with open(file_path, 'r') as file:
- code = file.read()
- A_list = ast.literal_eval(code)
- # Převod na NumPy pole
- A = np.array(A_list)
- #exec(code)
- # Now, all_results contains lists corresponding to each row in the CSV file
- ##print(all_results)
- # Assign values to variables dynamically based on the rows of matrix A
- for i, row in enumerate(A, start=1):
- globals()[f"person{i}_results"] = list(row)
- # Print the assigned variables
- for i in range(1, len(A) + 1):
- # print(f"person{i}_results {globals()[f'person{i}_results']}")
- all_results.append(f"person{i}_results")
- ##print(all_results)
- result_variables = []
- # Loop through the variable names and get the corresponding variables using globals()
- for var_name in all_results:
- result_variables.append(globals()[var_name])
- # Now, result_variables contains the variables with names specified in variable_names
- #print(result_variables)
- all_results = result_variables
- new_persons_results = result_variables
- labels = [results[-1] for results in all_results]
- # Odstranění posledního sloupce z datasetu
- data = [results[:-1] for results in all_results]
- # Definice počtu řádků pro trénování a testování
- num_training_rows = 50
- num_testing_rows = 50
- # Rozdělení datasetu na trénovací a testovací sady
- X_train, X_test, y_train, y_test = data[:num_training_rows], data[:num_testing_rows], labels[:num_training_rows], labels[:num_testing_rows]
- # Převod na NumPy pole
- X_train = np.array(X_train)
- X_test = np.array(X_test)
- y_train = np.array(y_train)
- y_test = np.array(y_test)
- # # Normalizace dat (s ohledem na -1)
- # min_values = np.min(X_train[X_train != -1], axis=0)
- # max_values = np.max(X_train[X_train != -1], axis=0)
- # X_train_normalized = (X_train - min_values) / (max_values - min_values)
- # import numpy as np
- # import tensorflow as tf
- # import matplotlib.pyplot as plt
- # from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
- # import seaborn as sns
- # from tqdm.notebook import tqdm_notebook
- import numpy as np
- import matplotlib.pyplot as plt
- import tensorflow as tf
- from tqdm.notebook import tqdm_notebook
- # Průměry (mu) a směrodatné odchylky (sigma)
- mu = np.mean(X_train, axis=0)
- sigma = np.std(X_train, axis=0)
- # Normalizace každého sloupce zvlášť
- X_train_standardized = (X_train - mu) / sigma
- X_test_standardized = (X_test - mu) / sigma
- # Vylepšený model
- model = tf.keras.Sequential([
- tf.keras.layers.Dense(256, activation='relu', input_shape=(len(X_train[0]),)),
- tf.keras.layers.Dropout(0.3),
- tf.keras.layers.Dense(128, activation='relu'),
- tf.keras.layers.Dropout(0.3),
- tf.keras.layers.Dense(64, activation='relu'),
- tf.keras.layers.Dropout(0.3),
- tf.keras.layers.Dense(1, activation='sigmoid')
- ])
- # Použití Adam optimizer s learning rate schedulerem
- lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
- initial_learning_rate=1e-3,
- decay_steps=10000,
- decay_rate=0.9
- )
- optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
- # Kompilace modelu
- model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
- # Lists to store accuracy values
- accuracy_history = []
- # Create images for the training data
- image_training = np.zeros((num_training_rows, len(X_train[0]) + 1, 3), dtype=np.uint8)
- min_pixel_value = np.min(X_train_standardized, axis=0)
- max_pixel_value = np.max(X_train_standardized, axis=0)
- for i, label in enumerate(y_train):
- for j in range(len(X_train_standardized[0])):
- pixel_value = int(np.interp(X_train_standardized[i][j], [min_pixel_value[j], max_pixel_value[j]], [0, 255]))
- image_training[i, j] = np.array([pixel_value] * 3)
- image_training[i, -1] = np.array([128, 128, 128])
- if label == 0:
- image_training[i, -1] = np.array([0, 128, 0])
- elif label == 1:
- image_training[i, -1] = np.array([255, 0, 0])
- # Training the model
- epochs = 139
- new_persons_results = [
- # [23.65780072, 18.8599168],
- # [22.57372914, 17.96922325],
- # [32.55342397, 29.46365141],
- # [ 6.71803504, 25.70466547],
- # [14.40191857, 16.77085649],
- # [17.45790731, 21.76521471],
- # [20.02796947, 73.45445955],
- # [26.2042, 10.6782],
- # [35.7258, 12.8027],
- # [21.2, 7.8],
- # [50.1, 40.2],
- # [32.739, 42.0152],
- # [28.1, 10.1],
- # [32.1656, 46.005 ],
- # [24.9647, 51.3433],
- # [34.6134, 44.4012],
- [0.0697400418162155,0.048866857264291144,0.28641370855472326,0.2721997143501177],
- [0.14159602676789837,0.1747877034447084,0.35616475477076587,0.3349487498168958],
- [0.11173253224821383,0.18794447828677996,0.3254176976987727,0.3413023918178341],
- [0.09630381764770453,0.05449810810962146,0.26767869268577593,0.21134056616439179],
- [0.17834821693532132,0.18466538062866059,0.3199711146234129,0.3968137366419059],
- [0.06045619825051427,0.05598696779492471,0.21592696351263593,0.22040624440430515],
- [0.08666288081774745,0.015388075894536557,0.2041876616268118,0.20706370434663773],
- [0.03130184508345673,0.015266595360551428,0.27183777103946916,0.2867664339707584],
- [0.05547626859495597,0.05808291988099526,0.2542166524648567,0.2573313511422864],
- [0.1772, 0.0076, 0.3565, 0.2584],
- ]
- import sys
- for epoch in tqdm_notebook(range(epochs)):
- history = model.fit(X_train_standardized, np.array(y_train), epochs=1, verbose=0, shuffle=False)
- accuracy_history.append(history.history['accuracy'][0])
- if epoch == 1:
- # Normalize the testing data
- X_test_standardized = (X_test - mu) / sigma
- y_pred_after_2nd_epoch = model.predict(X_test_standardized)
- y_pred_binary_after_2nd_epoch = [1 if pred >= 0.5 else 0 for pred in y_pred_after_2nd_epoch]
- image_testing_before_2nd_epoch = create_image(X_test_standardized, y_pred_binary_after_2nd_epoch, label_colors_testing)
- if epoch >= epochs-1:
- print(f"HERE HERE Epoch: {epoch}, Epochs: {epochs}\n")
- sys.stdout.flush()
- # Iterate through new persons
- for idx, personNEW_results in enumerate(new_persons_results, start=0):
- # Ensure that personNEW_results has the same number of features as the model expects
- assert len(personNEW_results) == len(X_train[0]), "Mismatch in the number of features."
- personNEW_results_standardized = (np.array(personNEW_results) - mu) / sigma
- personNEW_prediction = model.predict(np.array([personNEW_results_standardized]))
- personNEW_label = 1 if personNEW_prediction >= 0.5 else 0
- y_pred_after_50_epochs = model.predict(X_test_standardized)
- y_pred_binary_after_50_epochs = [1 if pred >= 0.5 else 0 for pred in y_pred_after_50_epochs]
- image_testing_after_50_epochs = create_image(X_test_standardized, y_pred_binary_after_50_epochs, label_colors_testing)
- # Create an image for the new person
- image_personNEW = create_imageN([personNEW_results_standardized], [personNEW_label], label_colors)
- # Display the images
- plt.figure(figsize=(5, 5))
- plt.imshow(image_personNEW)
- plt.title(f"New Person {idx}\nLabel: {personNEW_label}, Prediction: {personNEW_prediction}, personNEW_results: {personNEW_results}")
- plt.axis("off")
- plt.show()
- # Display the images
- plt.figure(figsize=(25, 15))
- plt.subplot(2, 2, 1)
- plt.imshow(image_training)
- plt.title("Training Data")
- plt.axis("off")
- plt.subplot(2, 2, 2)
- plt.imshow(image_testing_before_2nd_epoch)
- plt.title("Testing Data (2nd Epoch)")
- plt.axis("off")
- plt.subplot(2, 2, 3)
- plt.imshow(image_testing_after_50_epochs)
- plt.title(f"Testing Data ({epochs} Epochs)")
- plt.axis("off")
- plt.subplot(2, 2, 4)
- plt.imshow(image_personNEW)
- plt.title(f"New Person\nLabel: {personNEW_label},[{personNEW_prediction}]")
- plt.axis("off")
- # Plot accuracy history
- plt.figure(figsize=(12, 5))
- plt.plot(range(1, epochs + 1), accuracy_history, marker='o')
- plt.title('Accuracy Over Epochs')
- plt.xlabel('Epochs')
- plt.ylabel('Accuracy')
- plt.grid()
- # Print standardized data
- print("Standardized PersonNEW Data:")
- print(personNEW_results_standardized)
- plt.show()
- print("X_train before standardization:")
- print(X_train)
- print("X_test before standardization:")
- print(X_test)
- import seaborn as sns
- print("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
- print(X_test)
- print("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHH")
- print(X_train)
- print("LLLLLLLLLLLLLLLLLLLLLLLLLLLLL")
- # y_pred_binary = [1 if pred >= 0.5 else 0 for pred in model.predict(X_test_standardized)]
- # # Create confusion matrix
- # conf_matrix = confusion_matrix(y_train, y_pred_binary)
- # print(conf_matrix)
- from sklearn.metrics import confusion_matrix
- from tensorflow.keras.utils import to_categorical
- np.set_printoptions(threshold=np.inf, precision=4, suppress=True)
- # Assuming X_test_standardized and y_test are your test set data
- y_pred_binary = [1 if pred >= 0.5 else 0 for pred in model.predict(X_test_standardized)]
- # Create confusion matrix using the test set
- conf_matrix = confusion_matrix(y_test, y_pred_binary)
- print(conf_matrix)
- plt.figure(figsize=(6, 6))
- sns.heatmap(conf_matrix, annot=True, fmt="d", cmap="Blues", xticklabels=['Predicted 0', 'Predicted 1'], yticklabels=['Actual 0', 'Actual 1'])
- plt.xlabel("Predicted Label")
- plt.ylabel("True Label")
- plt.title("Confusion Matrix")
- plt.show()
- X_train = np.array(X_train)
- #y_train_one_hot = np.array(y_train_one_hot)
- # Rozdělení dat na trénovací a testovací množiny
- X_train, X_test, y_train, y_test = data[:num_training_rows], data[:num_testing_rows], labels[:num_training_rows], labels[:num_testing_rows]
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
- import tensorflow as tf
- import seaborn as sns
- # Assuming data splitting and model definition have been done correctly
- # Compile the model
- model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
- # Train the model
- print("Training Start")
- for epoch in tqdm_notebook(range(100), desc="Training Progress"):
- model.fit(np.array(X_train_standardized), np.array(y_train), epochs=1, verbose=0)
- print("Training Complete")
- # Generate predictions from the model
- predictions = (model.predict(X_test_standardized) > 0.5).astype(int)
- # Convert y_test to a numpy array and then to binary labels
- y_test_array = np.array(y_test) # Convert y_test to a numpy array
- y_test_binary = (y_test_array > 0.5).astype(int) # Convert to binary
- # Compute the confusion matrix
- conf_matrix = confusion_matrix(y_test_binary, predictions)
- # Evaluate the model's performance
- accuracy = accuracy_score(y_test_binary, predictions)
- precision = precision_score(y_test_binary, predictions)
- recall = recall_score(y_test_binary, predictions)
- f1 = f1_score(y_test_binary, predictions)
- # Display the confusion matrix
- sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
- plt.xlabel('Predicted')
- plt.ylabel('Actual')
- plt.title('Confusion Matrix')
- plt.show()
- print(f"Accuracy: {accuracy:.4f}")
- print(f"Precision: {precision:.4f}")
- print(f"Recall: {recall:.4f}")
- print(f"F1 Score: {f1:.4f}")
- print(f"Confusion Matrix2122:\n{conf_matrix}")
- import random
- def find_best_pair(min_val, max_val, num_features, model, min_values, max_values):
- best_pair = None
- best_prediction = 1
- for _ in range(1000): # Number of iterations to find the best pair
- new_data = np.random.uniform(min_val, max_val, num_features)
- new_data_standardized = (new_data - mu) / sigma
- # Suppress model output
- tf.get_logger().setLevel('ERROR')
- with tf.device('/CPU:0'): # Ensure to run on CPU to minimize unwanted logs
- prediction = model.predict(np.array([new_data_standardized]), verbose=0)[0][0]
- tf.get_logger().setLevel('INFO')
- if prediction < best_prediction:
- best_prediction = prediction
- best_pair = new_data
- return best_pair, best_prediction
- best_pair, best_prediction = find_best_pair(min_values, max_values, len(X_train[0]), model, min_values, max_values)
- def find_worst_pair(min_val, max_val, num_features, model, min_values, max_values):
- worst_pair = None
- worst_prediction = 0
- for _ in range(1000): # Number of iterations to find the best pair
- new_data = np.random.uniform(min_val, max_val, num_features)
- new_data_standardized = (new_data - mu) / sigma
- # Suppress model output
- tf.get_logger().setLevel('ERROR')
- with tf.device('/CPU:0'): # Ensure to run on CPU to minimize unwanted logs
- prediction = model.predict(np.array([new_data_standardized]), verbose=0)[0][0]
- tf.get_logger().setLevel('INFO')
- if prediction > worst_prediction:
- worst_prediction = prediction
- worst_pair = new_data
- return worst_pair, worst_prediction
- worst_pair, worst_prediction = find_worst_pair(min_values, max_values, len(X_train[0]), model, min_values, max_values)
- print(f"Best Pair: {best_pair}, Best Prediction: {best_prediction}")
- print(f"Worst Pair: {worst_pair}, Worst Prediction: {worst_prediction}")
- import numpy as np
- import matplotlib.pyplot as plt
- import tensorflow as tf
- from sklearn.metrics import recall_score, confusion_matrix, accuracy_score, precision_score, f1_score
- import seaborn as sns
- from tqdm.notebook import tqdm_notebook
- from sklearn.decomposition import PCA
- from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
- # Rozdělení na vstupní data (X) a cílové proměnné (y)
- X = A[:, :-1]
- y = A[:, -1]
- # Rozdělení na trénovací a testovací sadu (v tomto příkladě použijeme celou sadu jako trénovací pro jednoduchost)
- X_train, y_train = X, y
- X_test, y_test = X, y
- # Výpočet průměru a směrodatné odchylky pro každý sloupec
- mu = np.mean(X_train, axis=0)
- sigma = np.std(X_train, axis=0)
- # Normalizace každého sloupce zvlášť
- X_train_standardized = (X_train - mu) / sigma
- # Normalizace testovacích dat
- X_test_standardized = (X_test - mu) / sigma
- # Definice modelu
- model = tf.keras.Sequential([
- tf.keras.layers.Dense(256, activation='relu', input_shape=(X_train_standardized.shape[1],)),
- tf.keras.layers.Dropout(0.3),
- tf.keras.layers.Dense(128, activation='relu'),
- tf.keras.layers.Dropout(0.3),
- tf.keras.layers.Dense(64, activation='relu'),
- tf.keras.layers.Dropout(0.3),
- tf.keras.layers.Dense(1, activation='sigmoid')
- ])
- # Použití Adam optimizer s learning rate schedulerem
- lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
- initial_learning_rate=1e-3,
- decay_steps=10000,
- decay_rate=0.9
- )
- optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
- # Kompilace modelu
- model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy', tf.keras.metrics.Recall()])
- # Trénování modelu
- history = model.fit(X_train_standardized, y_train, epochs=50, verbose=0, shuffle=False)
- # Predikce
- y_pred_prob = model.predict(X_test_standardized)
- y_pred = (y_pred_prob > 0.5).astype(int)
- # Výpočet metrik
- recall = recall_score(y_test, y_pred)
- conf_matrix = confusion_matrix(y_test, y_pred)
- # Vyhodnocení výkonu modelu
- accuracy = accuracy_score(y_test, y_pred)
- precision = precision_score(y_test, y_pred)
- f1 = f1_score(y_test, y_pred)
- # Výpis metrik
- print(f"Recall: {recall:.4f}")
- print(f"Accuracy: {accuracy:.4f}")
- print(f"Precision: {precision:.4f}")
- print(f"F1 Score: {f1:.4f}")
- print(f"Confusion Matrix:\n{conf_matrix}")
- # Zobrazení confusion matrix
- sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
- plt.xlabel('Predicted')
- plt.ylabel('Actual')
- plt.title('Confusion Matrix')
- plt.show()
- print("KKKKKKKKKKKKK")
- # import numpy as np
- # import tensorflow as tf
- # import matplotlib.pyplot as plt
- # from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
- # import seaborn as sns
- # from tqdm.notebook import tqdm_notebook
- # # Výpočet průměru a směrodatné odchylky pro každý sloupec
- # mu = np.mean(X_train, axis=0)
- # sigma = np.std(X_train, axis=0)
- # # Normalizace každého sloupce zvlášť
- # X_train_standardized = (X_train - mu) / sigma
- # # Normalizace testovacích dat
- # X_test_standardized = (X_test - mu) / sigma
- # # Převedení dat na správný tvar pro CNN (přidání kanálu)
- # X_train_standardized = X_train_standardized[..., np.newaxis]
- # X_test_standardized = X_test_standardized[..., np.newaxis]
- # # Definice modelu
- # model = tf.keras.Sequential([
- # tf.keras.layers.Conv1D(32, 2, activation='relu', input_shape=(X_train_standardized.shape[1], 1)),
- # tf.keras.layers.MaxPooling1D(2),
- # tf.keras.layers.Flatten(),
- # tf.keras.layers.Dense(128, activation='relu'),
- # tf.keras.layers.Dropout(0.3),
- # tf.keras.layers.Dense(64, activation='relu'),
- # tf.keras.layers.Dropout(0.3),
- # tf.keras.layers.Dense(1, activation='sigmoid')
- # ])
- # # Použití Adam optimizer s learning rate schedulerem
- # lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
- # initial_learning_rate=1e-3,
- # decay_steps=10000,
- # decay_rate=0.9
- # )
- # optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
- # # Kompilace modelu
- # model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy', tf.keras.metrics.Recall()])
- # # Trénování modelu
- # history = model.fit(X_train_standardized, y_train, epochs=50, verbose=0, shuffle=False)
- # # Predikce
- # y_pred_prob = model.predict(X_test_standardized)
- # y_pred = (y_pred_prob > 0.5).astype(int)
- # # Výpočet metrik
- # recall = recall_score(y_test, y_pred)
- # conf_matrix = confusion_matrix(y_test, y_pred)
- # # Vyhodnocení výkonu modelu
- # accuracy = accuracy_score(y_test, y_pred)
- # precision = precision_score(y_test, y_pred)
- # f1 = f1_score(y_test, y_pred)
- # # Výpis metrik
- # print(f"Recall: {recall:.4f}")
- # print(f"Accuracy: {accuracy:.4f}")
- # print(f"Precision: {precision:.4f}")
- # print(f"F1 Score: {f1:.4f}")
- # print(f"Confusion Matrix:\n{conf_matrix}")
- # # Zobrazení confusion matrix
- # sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
- # plt.xlabel('Predicted')
- # plt.ylabel('Actual')
- # plt.title('Confusion Matrix')
- # plt.show()
- # import numpy as np
- # import tensorflow as tf
- # import matplotlib.pyplot as plt
- # from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
- # import seaborn as sns
- # from tqdm.notebook import tqdm_notebook
- # # Výpočet průměru a směrodatné odchylky pro každý sloupec
- # mu = np.mean(X_train, axis=0)
- # sigma = np.std(X_train, axis=0)
- # # Normalizace každého sloupce zvlášť
- # X_train_standardized = (X_train - mu) / sigma
- # # Normalizace testovacích dat
- # X_test_standardized = (X_test - mu) / sigma
- # # Převedení dat na správný tvar pro CNN (přidání kanálu)
- # X_train_standardized = X_train_standardized[..., np.newaxis]
- # X_test_standardized = X_test_standardized[..., np.newaxis]
- # # Definice modelu
- # model = tf.keras.Sequential([
- # tf.keras.layers.Conv1D(32, 2, activation='relu', input_shape=(X_train_standardized.shape[1], 1)),
- # tf.keras.layers.MaxPooling1D(2),
- # tf.keras.layers.Conv1D(64, 2, activation='relu'),
- # tf.keras.layers.MaxPooling1D(2),
- # tf.keras.layers.Flatten(),
- # tf.keras.layers.Dense(128, activation='relu'),
- # tf.keras.layers.Dropout(0.3),
- # tf.keras.layers.Dense(64, activation='relu'),
- # tf.keras.layers.Dropout(0.3),
- # tf.keras.layers.Dense(1, activation='sigmoid')
- # ])
- # # Použití Adam optimizer s learning rate schedulerem
- # lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
- # initial_learning_rate=1e-3,
- # decay_steps=10000,
- # decay_rate=0.9
- # )
- # optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
- # # Kompilace modelu
- # model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy', tf.keras.metrics.Recall()])
- # # Trénování modelu
- # history = model.fit(X_train_standardized, y_train, epochs=50, verbose=0, shuffle=False)
- # # Predikce
- # y_pred_prob = model.predict(X_test_standardized)
- # y_pred = (y_pred_prob > 0.5).astype(int)
- # # Výpočet metrik
- # recall = recall_score(y_test, y_pred)
- # conf_matrix = confusion_matrix(y_test, y_pred)
- # # Vyhodnocení výkonu modelu
- # accuracy = accuracy_score(y_test, y_pred)
- # precision = precision_score(y_test, y_pred)
- # f1 = f1_score(y_test, y_pred)
- # # Výpis metrik
- # print(f"Recall: {recall:.4f}")
- # print(f"Accuracy: {accuracy:.4f}")
- # print(f"Precision: {precision:.4f}")
- # print(f"F1 Score: {f1:.4f}")
- # print(f"Confusion Matrix:\n{conf_matrix}")
- # # Zobrazení confusion matrix
- # sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
- # plt.xlabel('Predicted')
- # plt.ylabel('Actual')
- # plt.title('Confusion Matrix')
- # plt.show()
- # # Odstranění mezivýstupů
- # num_iterations = 500
- # best_row = None
- # best_prediction = None
- # best_diff = float('inf')
- # for _ in range(num_iterations):
- # new_data = np.random.normal(mu, sigma)
- # new_data_standardized = (new_data - mu) / sigma
- # new_data_standardized = new_data_standardized[np.newaxis, ..., np.newaxis] # Přidání nového rozměru pro CNN
- # prediction_prob = model.predict(new_data_standardized, verbose=0)[0][0]
- # diff = abs(prediction_prob - 0.67)
- # if diff < best_diff:
- # best_diff = diff
- # best_row = new_data
- # best_prediction = prediction_prob
- # print(f"Nejlepší řádek: {best_row}")
- # print(f"Predikovaná hodnota: {best_prediction}")
- # print(f"Rozdíl: {best_diff}")
- import numpy as np
- import tensorflow as tf
- import matplotlib.pyplot as plt
- from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
- import seaborn as sns
- from tqdm.notebook import tqdm_notebook
- # Výpočet průměru a směrodatné odchylky pro každý sloupec
- mu = np.mean(X_train, axis=0)
- sigma = np.std(X_train, axis=0)
- # Normalizace každého sloupce zvlášť
- X_train_standardized = (X_train - mu) / sigma
- # Normalizace testovacích dat
- X_test_standardized = (X_test - mu) / sigma
- # Převedení dat na správný tvar pro CNN (přidání kanálu)
- X_train_standardized = X_train_standardized[..., np.newaxis]
- X_test_standardized = X_test_standardized[..., np.newaxis]
- # Definice modelu
- model = tf.keras.Sequential([
- tf.keras.layers.Conv1D(32, 1, activation='relu', input_shape=(X_train_standardized.shape[1], 1)),
- tf.keras.layers.MaxPooling1D(1),
- tf.keras.layers.Conv1D(64, 1, activation='relu'),
- tf.keras.layers.MaxPooling1D(1),
- tf.keras.layers.Flatten(),
- tf.keras.layers.Dense(128, activation='relu'),
- tf.keras.layers.Dropout(0.3),
- tf.keras.layers.Dense(64, activation='relu'),
- tf.keras.layers.Dropout(0.3),
- tf.keras.layers.Dense(1, activation='sigmoid')
- ])
- # Použití Adam optimizer s learning rate schedulerem
- lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
- initial_learning_rate=1e-3,
- decay_steps=10000,
- decay_rate=0.9
- )
- optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
- # Kompilace modelu
- model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy', tf.keras.metrics.Recall()])
- # Trénování modelu
- history = model.fit(X_train_standardized, y_train, epochs=50, verbose=0, shuffle=False)
- # Predikce
- y_pred_prob = model.predict(X_test_standardized)
- y_pred = (y_pred_prob > 0.5).astype(int)
- # Výpočet metrik
- recall = recall_score(y_test, y_pred)
- conf_matrix = confusion_matrix(y_test, y_pred)
- # Vyhodnocení výkonu modelu
- accuracy = accuracy_score(y_test, y_pred)
- precision = precision_score(y_test, y_pred)
- f1 = f1_score(y_test, y_pred)
- # Výpis metrik
- print(f"Recall: {recall:.4f}")
- print(f"Accuracy: {accuracy:.4f}")
- print(f"Precision: {precision:.4f}")
- print(f"F1 Score: {f1:.4f}")
- print(f"Confusion Matrix:\n{conf_matrix}")
- # Zobrazení confusion matrix
- sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
- plt.xlabel('Predicted')
- plt.ylabel('Actual')
- plt.title('Confusion Matrix')
- plt.show()
- # Odstranění mezivýstupů
- num_iterations = 500
- best_row = None
- best_prediction = None
- best_diff = float('inf')
- for _ in range(num_iterations):
- new_data = np.random.normal(mu, sigma)
- new_data_standardized = (new_data - mu) / sigma
- new_data_standardized = new_data_standardized[np.newaxis, ..., np.newaxis] # Přidání nového rozměru pro CNN
- prediction_prob = model.predict(new_data_standardized, verbose=0)[0][0]
- diff = abs(prediction_prob - 0.67)
- if diff < best_diff:
- best_diff = diff
- best_row = new_data
- best_prediction = prediction_prob
- print(f"Nejlepší řádek: {best_row}")
- print(f"Predikovaná hodnota: {best_prediction}")
- print(f"Rozdíl: {best_diff}")
- # # Odstranění mezivýstupů
- # num_iterations = 50
- # best_row = None
- # best_prediction = None
- # best_diff = float('inf')
- # for _ in range(num_iterations):
- # new_data = np.random.normal(mu, sigma)
- # new_data_standardized = (new_data - mu) / sigma
- # prediction_prob = model.predict(np.array([new_data_standardized]), verbose=0)[0][0]
- # diff = abs(prediction_prob - 0.67)
- # if diff < best_diff:
- # best_diff = diff
- # best_row = new_data
- # best_prediction = prediction_prob
- # print(f"Nejlepší řádek: {best_row}")
- # print(f"Predikovaná hodnota: {best_prediction}")
- # print(f"Rozdíl: {best_diff}")
- # Vizualizace výsledků pomocí PCA
- X_standardized = (X - mu) / sigma
- pca = PCA(n_components=2) # Snížení na 2 komponenty
- X_pca = pca.fit_transform(X_standardized)
- plt.figure()
- plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y)
- plt.xlabel('První hlavní komponenta')
- plt.ylabel('Druhá hlavní komponenta')
- plt.title('PCA na vašich datech')
- plt.show()
- # Vizualizace výsledků pomocí LDA
- lda = LDA(n_components=1)
- X_lda = lda.fit_transform(X_standardized, y)
- plt.figure()
- plt.scatter(X_lda[:, 0], np.zeros_like(X_lda), c=y)
- plt.xlabel('První diskriminační komponenta')
- plt.title('LDA s učitelem')
- plt.show()
- # Vytvoření obrazu pro trénovací data
- min_pixel_value = -3
- max_pixel_value = 3
- image_training = np.zeros((len(X_train_standardized), len(X_train_standardized[0]) + 1, 3), dtype=np.uint8)
- for i, label in enumerate(y_train):
- for j in range(len(X_train_standardized[0])):
- pixel_value = int(np.interp(X_train_standardized[i][j], [min_pixel_value, max_pixel_value], [0, 255]))
- image_training[i, j] = np.array([pixel_value] * 3)
- image_training[i, -1] = np.array([128, 128, 128]) # Šedý sloupec pro všechny řádky
- if label == 0:
- image_training[i, -1] = np.array([0, 128, 0]) # Zelený sloupec pro label 0
- elif label == 1:
- image_training[i, -1] = np.array([255, 0, 0]) # Červený sloupec pro label 1
- # Zobrazení obrazu
- #plt.imshow(image_training)
- #plt.title("Training Data")#
- ##plt.axis("off")
- #plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement