0% found this document useful (0 votes)
0 views

Vertopal.com Untitled27

The document outlines a machine learning workflow using deep learning models to predict a target variable from a dataset. It includes data preprocessing steps, the construction and training of a Deep Neural Network (DNN) and a Recurrent Neural Network (RNN) using LSTM layers, and evaluation of their performance based on Mean Squared Error (MSE). The RNN model outperformed the DNN, indicating it as the optimized model for this task.

Uploaded by

coding1172001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Vertopal.com Untitled27

The document outlines a machine learning workflow using deep learning models to predict a target variable from a dataset. It includes data preprocessing steps, the construction and training of a Deep Neural Network (DNN) and a Recurrent Neural Network (RNN) using LSTM layers, and evaluation of their performance based on Mean Squared Error (MSE). The RNN model outperformed the DNN, indicating it as the optimized model for this task.

Uploaded by

coding1172001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import pandas as pd

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM, Input
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt

# Load dataset
df = pd.read_excel("dataset_28112024new.xlsx")

# Drop unnamed column if it exists


df = df.loc[:, ~df.columns.str.contains('^Unnamed')]

# Features and target


X = df.drop(columns=["PDISS (uW)"])
y = df["PDISS (uW)"]

# Train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)

# Normalize data
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

### Deep Neural Network (DNN)


dnn_model = Sequential([
Input(shape=(X_train_scaled.shape[1],)),
Dense(64, activation='relu'),
Dense(32, activation='relu'),
Dense(1)
])
dnn_model.compile(optimizer=Adam(0.001), loss='mse')
dnn_history = dnn_model.fit(X_train_scaled, y_train, epochs=50,
batch_size=16, validation_split=0.1, verbose=0)

### Recurrent Neural Network (RNN) - using LSTM


# Reshape for LSTM input: (samples, timesteps, features)
X_train_rnn = X_train_scaled.reshape((X_train_scaled.shape[0], 1,
X_train_scaled.shape[1]))
X_test_rnn = X_test_scaled.reshape((X_test_scaled.shape[0], 1,
X_test_scaled.shape[1]))

rnn_model = Sequential([
LSTM(64, input_shape=(1, X_train_scaled.shape[1]),
return_sequences=False),
Dense(32, activation='relu'),
Dense(1)
])
rnn_model.compile(optimizer=Adam(0.001), loss='mse')
rnn_history = rnn_model.fit(X_train_rnn, y_train, epochs=50,
batch_size=16, validation_split=0.1, verbose=0)

# Evaluate models
dnn_mse = dnn_model.evaluate(X_test_scaled, y_test, verbose=0)
rnn_mse = rnn_model.evaluate(X_test_rnn, y_test, verbose=0)

print(f"DNN Test MSE: {dnn_mse:.4f}")


print(f"RNN Test MSE: {rnn_mse:.4f}")

# Plot training history


plt.figure(figsize=(12, 5))
plt.plot(dnn_history.history['loss'], label='DNN Train Loss')
plt.plot(dnn_history.history['val_loss'], label='DNN Val Loss')
plt.plot(rnn_history.history['loss'], label='RNN Train Loss')
plt.plot(rnn_history.history['val_loss'], label='RNN Val Loss')
plt.legend()
plt.title("Training Loss Comparison")
plt.xlabel("Epochs")
plt.ylabel("MSE Loss")
plt.grid(True)
plt.show()

# Choose best model based on lower MSE


if dnn_mse < rnn_mse:
print("✅ DNN performs better and is the optimized model.")
else:
print("✅ RNN performs better and is the optimized model.")

C:\Users\21031\AppData\Local\Programs\Python\Python312\Lib\site-
packages\keras\src\layers\rnn\rnn.py:204: UserWarning: Do not pass an
`input_shape`/`input_dim` argument to a layer. When using Sequential
models, prefer using an `Input(shape)` object as the first layer in
the model instead.
super().__init__(**kwargs)

DNN Test MSE: 30462.2539


RNN Test MSE: 30399.7324
✅ RNN performs better and is the optimized model.

You might also like