How to Predict Students Gender Using Height and Weight Data with Deep Neural Networks in Keras and Python?
Share
Condition for Predicting Students Gender Using Height and Weight Data with Deep Neural Networks in Keras and Python
Description: This code implements a deep learning model to predict a student's gender based on their height and weight using an artificial neural network (ANN) in TensorFlow. The dataset is preprocessed by encoding gender labels, splitting data into train-test sets, and handling class balance.
Step-by-Step Process
Step1: Import necessary libraries such as pandas, TensorFlow, scikit-learn,and visualization tools (matplotlib, seaborn).
Step2: Read the dataset (weight-height.csv) using pandas and display the first few rows to understand its structure.
Step3: Check if there are any missing values in the dataset using isnull().sum() to ensure data quality.
Step4: Encode the categorical Gender column into numeric values using LabelEncoder for compatibility with the model.
Step5: Split the data into input features (x with height and weight) and output labels (y with gender).
Step6: Calculate and visualize the distribution of the target variable Gender using a bar plot to check for class imbalance.
Step7: Split the data into training and testing sets using train_test_split,ensuring the model has separate data for training and evaluation.
Step8: Define the architecture of the ANN with an input layer,two hidden layers (with ReLU activation), and a sigmoid output layer for binary classification.
Step9: Train the model using the training set with the Adam optimizer and binary cross-entropy loss, evaluating performance on the validation set.
Step10: Make predictions on the test set, calculate performance metrics(accuracy, F1-score, precision, recall), and display the classification report and confusion matrix.
Sample Code
#Import Necessary Libraries
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import seaborn as sns
from tensorflow.keras.layers import Dense,Dropout,Input
from tensorflow.keras.models import Model
from sklearn.metrics import (classification_report,confusion_matrix,accuracy_score,
f1_score,recall_score,precision_score)
import warnings
warnings.filterwarnings("ignore")
df = pd.read_csv("/home/soft12/Downloads/sample_dataset/Website/Dataset/weight- height.csv")
#Check null values in a dataset
df.isnull().sum()
label = LabelEncoder()
df['Gender'] = label.fit_transform(df['Gender'])
x = df.drop(['Gender'],axis=1)
y = df['Gender']
#check class imbalance
class_counts = y.value_counts()
# Plot the class distribution
plt.figure(figsize=(8, 6))
sns.barplot(x=class_counts.index, y=class_counts.values, palette="viridis")
plt.title('Class Balance Check', fontsize=16)
plt.xlabel('Class', fontsize=14)
plt.ylabel('Count', fontsize=14)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()
X_train,X_test,y_train,y_test = train_test_split(x,y,test_size=.2,random_state=42)
def ANN_model(input_shape):
# Input layer
inputs = Input(shape=(input_shape,))
# Hidden layers
layer1 = Dense(64, activation='relu')(inputs)
Dropout1 = Dropout(0.2)(layer1)
layer2 = Dense(32, activation='relu')(Dropout1)
Dropout2 = Dropout(0.2)(layer2)
# Output layer
output_layer = Dense(1, activation='sigmoid')(Dropout2)
# Build the model
ann_model = Model(inputs=inputs, outputs=output_layer)
# Compile the model with Adam optimizer and binary crossentropy loss function
ann_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return ann_model
model = ANN_model(X_train.shape[1])
model.fit(X_train,y_train,batch_size=2,epochs=10,validation_data=(X_test,y_test))
y_pred = model.predict(X_test)
y_pred = [1 if i>0.5 else 0 for i in y_pred]
print("___Performance_Metrics___\n")
print('Classification_Report:\n',classification_report(y_test, y_pred))
print('Confusion_Matrix:\n',confusion_matrix(y_test, y_pred))
print('Accuracy_Score: ',accuracy_score(y_test, y_pred))
print('F1_Score: ',f1_score(y_test, y_pred))
print('Recall_Score: ',recall_score(y_test, y_pred))
print('Precision_Score: ',precision_score(y_test, y_pred))