Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.5 kB
6
Indexable
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import cv2
import pandas as pd
from tensorflow import keras
from tensorflow.keras import datasets, layers, models
from sklearn.model_selection import train_test_split

from google.colab import drive
drive.mount('/content/drive')

df_1 = pd.read_csv("/content/drive/MyDrive/DWTPreictalCompleto.csv")
df_2 = pd.read_csv("/content/drive/MyDrive/DWTIctalCompleto.csv")
df_3 = pd.read_csv("/content/drive/MyDrive/DWT.csv")

df = pd.concat([df_1, df_2, df_3], axis=0, ignore_index=True)
df = df.drop(['Coeficiente_0'], axis=1)

y = df.iloc[:, 0]
X = df.iloc[:, 1:]

del df_1
del df_2
del df_3

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)
X_train = np.array(X_train).reshape(X_train.shape[0], X_train.shape[1], 1)
X_test = np.array(X_test).reshape(X_test.shape[0], X_test.shape[1], 1)

# Normaliza los datos
X_train = X_train / 255.0
X_test = X_test / 255.0

print("X Train shape: ", X_train.shape)
print("X Test shape: ", X_test.shape)

# Crear modelo secuencial
cnn_model = tf.keras.models.Sequential()

# Primera capa CNN con 32 filtros, kernel de 3, activación ReLU y relleno 'same'
cnn_model.add(Conv1D(filters=32, kernel_size=3, padding='same', activation='relu', input_shape=(X_train.shape[1], 1)))
cnn_model.add(BatchNormalization())
cnn_model.add(Conv1D(filters=64, kernel_size=3, padding='same', activation='relu'))
cnn_model.add(BatchNormalization())
cnn_model.add(MaxPool1D(pool_size=2))
cnn_model.add(Conv1D(filters=128, kernel_size=3, padding='same', activation='relu'))
cnn_model.add(BatchNormalization())
cnn_model.add(MaxPool1D(pool_size=2))
cnn_model.add(Conv1D(filters=256, kernel_size=3, padding='same', activation='relu'))
cnn_model.add(BatchNormalization())
cnn_model.add(MaxPool1D(pool_size=2))
cnn_model.add(Flatten())
cnn_model.add(Dense(units=128, activation='relu'))
cnn_model.add(Dense(units=64, activation='relu'))
cnn_model.add(Dense(units=1, activation='sigmoid'))  # Usar 'sigmoid' para clasificación binaria

# Compilar el modelo
cnn_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Entrenar el modelo
history = cnn_model.fit(X_train, y_train, epochs=10, batch_size=64, validation_data=(X_test, y_test))

# Evaluar el modelo en el conjunto de prueba
test_loss, test_accuracy = cnn_model.evaluate(X_test, y_test)
print(f"Accuracy en el conjunto de prueba: {test_accuracy * 100:.2f}%")