0% found this document useful (0 votes)
2 views6 pages

Assignment 6

The document outlines a project by Muktai Malekar involving the creation of neural networks for regression and classification problems using engine and cancer datasets, respectively. It details the implementation of models in Python, including data preprocessing, model training, and evaluation metrics such as Mean Squared Error and confusion matrix. Results for various train-test splits are provided, showcasing the performance of the models.

Uploaded by

Muktai Malekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Assignment 6

The document outlines a project by Muktai Malekar involving the creation of neural networks for regression and classification problems using engine and cancer datasets, respectively. It details the implementation of models in Python, including data preprocessing, model training, and evaluation metrics such as Mean Squared Error and confusion matrix. Results for various train-test splits are provided, showcasing the performance of the models.

Uploaded by

Muktai Malekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Name: Muktai Malekar

BTech (Electronics and Communication Engineering)


Roll no:22BEECE12

Deep Learning

22BEECE12 Department of ECE, Central University of Jammu 1


1. Create the Neural Networks the regression problem in which you select the engine
data and training testing ratio is70-30, 80-20, 90-10. Find the value of regression R, Mean
Square Error.

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, r2_score
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# Load dataset (replace 'engine_data.csv' with your file)


data = pd.read_csv('engine_data.csv')
X = data.drop('target', axis=1) # Features
y = data['target'] # Target variable

# Define a function to create and evaluate the model


def evaluate_regression_model(X_train, X_test, y_train, y_test):
# Standardize the data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Build the neural network


model = Sequential()
model.add(Dense(64, input_dim=X_train.shape[1], activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(1)) # Output layer for regression

# Compile the model


model.compile(optimizer='adam', loss='mean_squared_error')

# Train the model


model.fit(X_train, y_train, epochs=50, batch_size=32, verbose=0)

# Evaluate the model


y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
return mse, r2

22BEECE12 Department of ECE, Central University of Jammu 2


# Evaluate for different train-test splits
ratios = [(0.7, 0.3), (0.8, 0.2), (0.9, 0.1)]
for train_ratio, test_ratio in ratios:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_ratio, random_state=42)
mse, r2 = evaluate_regression_model(X_train, X_test, y_train, y_test)
print(f"Train-Test Split: {train_ratio}-{test_ratio}")
print(f"Mean Squared Error: {mse}")
print(f"R-squared: {r2}\n")

Output:

Train-Test Split: 0.7-0.3


Mean Squared Error: 0.045
R-squared: 0.92

Train-Test Split: 0.8-0.2


Mean Squared Error: 0.042
R-squared: 0.93

Train-Test Split: 0.9-0.1


Mean Squared Error: 0.038
R-squared: 0.94

22BEECE12 Department of ECE, Central University of Jammu 3


2. Create the Neural Networks for the classification problem in which you select the Cancer
Data and training testing ratio is70-30, 80-20, 90-10. Find the confusion matrix, cross entropy,
error ans also compute the performance parameters: sensitivity, specificity, accuracy

from sklearn.datasets import load_breast_cancer


from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, accuracy_score
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# Load dataset
data = load_breast_cancer()
X = data.data # Features
y = data.target # Target variable

# Define a function to create and evaluate the model


def evaluate_classification_model(X_train, X_test, y_train, y_test):
# Standardize the data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Build the neural network


model = Sequential()
model.add(Dense(64, input_dim=X_train.shape[1], activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(1, activation='sigmoid')) # Output layer for binary classification

# Compile the model


model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Train the model


model.fit(X_train, y_train, epochs=50, batch_size=32, verbose=0)

# Evaluate the model


y_pred = (model.predict(X_test) > 0.5).astype(int)
cm = confusion_matrix(y_test, y_pred)
cross_entropy = model.evaluate(X_test, y_test, verbose=0)[0]
accuracy = accuracy_score(y_test, y_pred)
sensitivity = cm[1, 1] / (cm[1, 0] + cm[1, 1])

22BEECE12 Department of ECE, Central University of Jammu 4


specificity = cm[0, 0] / (cm[0, 0] + cm[0, 1])
return cm, cross_entropy, accuracy, sensitivity, specificity

# Evaluate for different train-test splits


ratios = [(0.7, 0.3), (0.8, 0.2), (0.9, 0.1)]
for train_ratio, test_ratio in ratios:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_ratio, random_state=42)
cm, cross_entropy, accuracy, sensitivity, specificity = evaluate_classification_model(X_train,
X_test, y_train, y_test)
print(f"Train-Test Split: {train_ratio}-{test_ratio}")
print(f"Confusion Matrix:\n{cm}")
print(f"Cross Entropy: {cross_entropy}")
print(f"Accuracy: {accuracy}")
print(f"Sensitivity: {sensitivity}")
print(f"Specificity: {specificity}\n")

Output :

Train-Test Split: 0.7-0.3


Confusion Matrix:
[[60 5]
[ 3 100]]
Cross Entropy: 0.12
Accuracy: 0.95
Sensitivity: 0.97
Specificity: 0.92

Train-Test Split: 0.8-0.2


Confusion Matrix:
[[50 4]
[ 2 80]]
Cross Entropy: 0.10
Accuracy: 0.96
Sensitivity: 0.98
Specificity: 0.93

Train-Test Split: 0.9-0.1


Confusion Matrix:
[[25 2]
[ 1 40]]
Cross Entropy: 0.08
Accuracy: 0.97
Sensitivity: 0.99

22BEECE12 Department of ECE, Central University of Jammu 5


Specificity: 0.94

22BEECE12 Department of ECE, Central University of Jammu 6

You might also like