From 77eb99cbf58fd4212c75fd412cf7330bb1c3694e Mon Sep 17 00:00:00 2001 From: fferreira Date: Mon, 1 Sep 2025 21:43:35 +0100 Subject: [PATCH] first commit --- Model.py | 31 +++++++++++++++++++++++++++++++ Predict.py | 14 ++++++++++++++ graph.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 Model.py create mode 100644 Predict.py create mode 100644 graph.py diff --git a/Model.py b/Model.py new file mode 100644 index 0000000..b8817bc --- /dev/null +++ b/Model.py @@ -0,0 +1,31 @@ +import pandas as pd +import numpy as np + +# Make numpy values easier to read. +np.set_printoptions(precision=3, suppress=True) + +import tensorflow as tf +from tensorflow.keras import layers + +ecvalve = pd.read_csv( + "/home/fferreira/Documents/teste_ecvalve.csv", + names=[ "frequency", "state", "pressure", "capacity"]) +print("Data shape:", ecvalve.shape) +print("Columns:", ecvalve.columns) + +ecvalve.head() +ecvalve_features = ecvalve.copy() +ecvalve_labels = ecvalve_features.pop('state') + +ecvalve_features = np.array(ecvalve_features) + +ecvalve_model = tf.keras.Sequential([ + layers.Dense(64, activation='relu', input_shape=(ecvalve_features.shape[1],)), + layers.Dense(32, activation='relu'), # Add another layer + layers.Dense(1, activation='sigmoid') # Sigmoid for binary classification +]) +ecvalve_model.compile(loss=tf.keras.losses.BinaryCrossentropy(), optimizer=tf.keras.optimizers.Adam(), metrics=['accuracy']) + +ecvalve_model.fit(ecvalve_features, ecvalve_labels, epochs=20) + +ecvalve_model.save('EcValve_1.h5') \ No newline at end of file diff --git a/Predict.py b/Predict.py new file mode 100644 index 0000000..666ebd7 --- /dev/null +++ b/Predict.py @@ -0,0 +1,14 @@ +import tensorflow as tf +import numpy as np + +import tensorflow as tf +import numpy as np + +model = tf.keras.models.load_model('EcValve_1.h5') + +input_data = np.array([[-0.029184604, 7, 60]], dtype=np.float32) # Shape: (1, 3) + +prediction = model.predict(input_data) + +print("Prediction (probability of leak):", prediction) +print("Leak detected" if prediction[0] > 0.5 else "No leak") \ No newline at end of file diff --git a/graph.py b/graph.py new file mode 100644 index 0000000..eac7c65 --- /dev/null +++ b/graph.py @@ -0,0 +1,42 @@ +import pandas as pd +import matplotlib.pyplot as plt +import seaborn as sns +import numpy as np + +# Set seaborn style for better visuals +sns.set(style="whitegrid") + +# Load CSV +ecvalve = pd.read_csv( + "/home/fferreira/Documents/teste_ecvalve.csv", + names=["frequency", "state", "pressure", "capacity"]) +print("Data shape:", ecvalve.shape) +print("Label distribution:", ecvalve['state'].value_counts()) + +# Group by frequency and get unique states +freq_states = ecvalve.groupby('frequency')['state'].unique().reset_index() +freq_states['category'] = freq_states['state'].apply( + lambda x: '0' if len(x) == 1 and x[0] == 0 else '1' if len(x) == 1 and x[0] == 1 else 'Both' +) + +print("\nFrequencies and their state categories:") +print(freq_states[['frequency', 'category']].sort_values('frequency')) + +# Create scatter plot +plt.figure(figsize=(10, 6)) +sns.scatterplot( + x='frequency', + y='category', + hue='category', + style='category', + data=freq_states, + palette={'0': '#36A2EB', '1': '#FF6384', 'Both': '#FFCE56'}, + s=100 +) +plt.title('Frequencies by State Category (0 = No Leak, 1 = Leak, Both = Mixed)') +plt.xlabel('Frequency (Hz)') +plt.ylabel('State Category') +plt.savefig('/home/fferreira/Documents/HarvardX/teste/frequency_state_categories.png') +plt.close() + +print("Graph saved: /home/fferreira/Documents/HarvardX/teste/frequency_state_categories.png") \ No newline at end of file