Describe the entire code briefly:
Step I - Load the Dataset
[8]
# Load the Dataset
data = pd.read_csv(‘dataset.csv’)
data.head()

[9]
0s
# Summary of the dataset
data.info()
<class ‘pandas.core.frame.DataFrame’>
RangeIndex: 766 entries, 0 to 765
Data columns (total 8 columns):
# Column Non-Null Count Dtype
— ------ -------------- -----
0 f1 766 non-null object
1 f2 766 non-null object
2 f3 766 non-null int64
3 f4 766 non-null object
4 f5 766 non-null object
5 f6 766 non-null object
6 f7 766 non-null object
7 target 766 non-null int64
dtypes: int64(2), object(6)
memory usage: 48.0+ KB
[5]
0s
data.describe()

[ ]
plt.figure(figsize=(4, 3))
plt.hist(data[‘target’])
plt.xlabel(‘Target’)
plt.ylabel(‘Frequency’)
plt.title(‘Distribution of Target Variable’)
plt.show()

[ ]
# Visualize scatter plot for features against the target
plt.figure(figsize= (4,3))
sns.scatterplot(data=data, x=data.columns[2], y=‘target’)
plt.title(‘Feature-3 vs Target’)
plt.show()


Step II - Preprocessing the Dataset
[10]
0s
# Identify categorical columns
categorical_columns = data.select_dtypes(include=[‘object’]).columns

# Create an empty list to store invalid values
invalid_values = []

# Iterate over all object columns and find the invalid values
for col in categorical_columns:
invalid_values.extend(data.loc[data[col].str.isalpha(), col].unique())

# Print the object columns & the unique list of invalid values
print(‘Categorical columns:’ , categorical_columns.to_list(), ‘Invalid Values:’, set(invalid_values))
Categorical columns: [‘f1’, ‘f2’, ‘f4’, ‘f5’, ‘f6’, ‘f7’] Invalid Values: {‘e’, ‘b’, ‘f’, ‘d’, ‘a’, ‘c’}
[11]
0s
# Replace missing/invalid values with pd.NA
data = data.replace(invalid_values, np.nan)
[12]
0s
# Find the missing values
missing_values = data.isna().sum()
print(‘Missing values:’)
print(missing_values)
Missing values:
f1 1
f2 1
f3 0
f4 1
f5 1
f6 1
f7 1
target 0
dtype: int64
[13]
0s
# Fill missing values with mode or mean, depending on column type

fill_columns = [k for k, v in missing_values.to_dict().items() if v != 0]

for col in fill_columns:
if data[col].dtype == ‘object’:
data[col].fillna(data[col].mode()[0], inplace=True)
else:
data[col].fillna(data[col].mean(), inplace=True)
[14]
0s
# Convert the dataframe to numeric
data = data.astype(‘float’)
[ ]
# Identify any correlations or patterns in the data
sns.pairplot(data, vars=[‘f1’, ‘f2’, ‘f3’, ‘f4’, ‘f5’, ‘f6’, ‘f7’], hue=‘target’)
plt.show()

[15]
1s
# Correlation Heatmap : # Identify any correlations
corr = data.corr(numeric_only= True)
sns.heatmap(corr, annot=True, cmap=‘coolwarm’)
plt.show()

[16]
0s
# Statistics for each column in the dataframe
data.describe()

[18]
0s
# Store Features and Target in X and y respectively
X = data.drop(columns=[‘target’])
y = data[‘target’]
[19]
0s
# Scale numerical variables to have zero mean and unit variance.
scaler = StandardScaler(with_mean= False)
X_scaled = scaler.fit_transform(X)
[20]
0s
# Compute the mean and variance of each column
mean = np.mean(X_scaled, axis=0)
var = np.var(X_scaled, axis=0)

print(f’Mean: {mean} Variance: {var}‘)

Mean: [1.14094142 3.78762242 3.56940252 1.28569127 0.69396998 4.05658553
1.42462597] Variance: [1. 1. 1. 1. 1. 1. 1.]
[21]
0s
# Split the data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42, shuffle= False)
Step III & IV - Defining the Neural Network
[22]
0s
# Define the Neural Network and its Architecture

class NNClassifier(nn.Module):
def init(self, input_size, hidden_size, output_size):
super(NNClassifier, self).init()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu1 = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, hidden_size)
self.relu2 = nn.ReLU()
self.fc3 = nn.Linear(hidden_size,hidden_size)
self.relu3 = nn.ReLU()
self.fc4 = nn.Linear(hidden_size, output_size)
self.sigmoid = nn.Sigmoid()

def forward(self, x):
x = self.relu1(self.fc1(x))
x = self.relu2(self.fc2(x))
x = self.relu3(self.fc3(x))
x = self.sigmoid(self.fc4(x))
return x

hidden_size = 128
input_size = X_train.shape[1]
output_size = 1
model = NNClassifier(input_size, hidden_size, output_size)


Step V - Train the Neural Network
[39]
0s

# Testing segment
with torch.no_grad():
X_test_tensor = torch.tensor(X_test, dtype=torch.float)
y_test_tensor = torch.tensor(y_test.values, dtype=torch.float).view(-1, 1)

test_pred = model(X_test_tensor)
test_loss = criterion(test_pred, y_test_tensor)
test_accuracy = accuracy_score(y_test_tensor, (test_pred > 0.5).float())


[24]
0s
def plot_graphs(data, dropout = False, technique = False):
train_losses, train_accuracies, test_losses, test_accuracies = data

# Compare training and test losses
plt.plot(train_losses, label=‘Training Loss’)
plt.plot(test_losses, label=‘Test Loss’)
if dropout:
plt.title(f’Dropout {dropout}’)
elif technique:
plt.title(f’{technique} Training vs Test Loss’)
else:
plt.title(‘Training vs Test Loss’)
plt.xlabel(‘Epochs’)
plt.ylabel(‘Loss’)
plt.legend()
plt.show()

# Compare training and test accuracies
plt.plot(train_accuracies, label=‘Training Accuracy’)
plt.plot(test_accuracies, label=‘Test Accuracy’)
if dropout:
plt.title(f’Dropout {dropout}‘)
elif technique:
plt.title(f’{technique} Training vs Test Accuracy’)
else:
plt.title(‘Training vs Test Accuracy’)
plt.xlabel(‘Epochs’)
plt.ylabel(‘Accuracy’)
plt.legend()
plt.show()

[25]
21s
plot_graphs(train_model(model, X_train, y_train, X_test, y_test, epochs= 1000, batch_size=64, learning_rate=0.01))
Epoch: 1/1000, Training Loss: 0.7022176206111908, Test Loss: 0.6874094605445862, Training Accuracy: 0.3888888888888889, Test Accuracy: 0.6493506493506493
Epoch: 101/1000, Training Loss: 0.5644980251789093, Test Loss: 0.5720729827880859, Training Accuracy: 0.7156862745098039, Test Accuracy: 0.6753246753246753
Epoch: 201/1000, Training Loss: 0.49564163088798524, Test Loss: 0.5118716359138489, Training Accuracy: 0.7630718954248366, Test Accuracy: 0.7597402597402597
Epoch: 301/1000, Training Loss: 0.45333537459373474, Test Loss: 0.491641640663147, Training Accuracy: 0.7794117647058824, Test Accuracy: 0.7922077922077922
Epoch: 401/1000, Training Loss: 0.43739196956157683, Test Loss: 0.4959128797054291, Training Accuracy: 0.7908496732026143, Test Accuracy: 0.7857142857142857
Epoch: 501/1000, Training Loss: 0.426849065721035, Test Loss: 0.5039510130882263, Training Accuracy: 0.8088235294117647, Test Accuracy: 0.7792207792207793
Epoch: 601/1000, Training Loss: 0.4176767110824585, Test Loss: 0.5129557251930237, Training Accuracy: 0.8218954248366013, Test Accuracy: 0.7662337662337663
Epoch: 701/1000, Training Loss: 0.40947874933481215, Test Loss: 0.5219591856002808, Training Accuracy: 0.826797385620915, Test Accuracy: 0.7597402597402597
Epoch: 801/1000, Training Loss: 0.40165899842977526, Test Loss: 0.5320645570755005, Training Accuracy: 0.8284313725490197, Test Accuracy: 0.7532467532467533
Epoch: 901/1000, Training Loss: 0.39323500394821165, Test Loss: 0.5404998064041138, Training Accuracy: 0.8316993464052288, Test Accuracy: 0.7532467532467533


[26]
#Step 6: Save the Weights

torch.save(model.state_dict(), ‘trained_weights.h5’)
[27]
0s
# Confusion Matrix
y_pred = (model(X_test_tensor) > 0.5).float().numpy()
cm = confusion_matrix(y_test, y_pred)
print(‘Confusion matrix: \n’, cm)
Confusion matrix:
[[83 15]
[24 32]]