Amazing technological breakthrough possible @S-Logix

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • +91- 81240 01111

Social List

How to predict students gender using their height and weight data using Deep neural networks from keras in python?


To build a DNN model to predict Gender of the students with help of keras in python.


Height and Weight data set.


Confusion matrix
Classification report
Accuracy score
Precision and recall



   Load the data set.

  Define the independent and dependent variable.

  Build the deep neural networks.

  Initiate activation and optimizer functions according to the problem.

  Compile and fit the DNN model.

  Split the data into train and testing set.

  Fit the training set into the model.

  Predict the test results using DNN.

  Calculate the accuracy, precision and recall.

Sample Code

#import necessary libraries
import time
import random
import warnings
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from keras.utils import np_utils
from sklearn.metrics import classification_report, confusion_matrix

#load the data set
data = pd.read_csv(‘/home/soft50/soft50/Sathish/practice/weight-height.csv’)

#Make it as a data frame
df = pd.DataFrame(data)

#Checking missing values
print(“Checking missing values\n”)

#Count plot for target variable
plt.rcParams[“figure.figsize”] = [9,6]
ax = sns.countplot(x=”Gender”,data=df)
plt.title(“Count plot for categorical variable”)

from sklearn import preprocessing

# label_encoder object knows how to understand word labels.
label_encoder = preprocessing.LabelEncoder()

# Encode labels in column ‘Gender’.
df[‘Gender’]= label_encoder.fit_transform(df[‘Gender’])

#feature selection
X = df.iloc[:,1:3]
y = df.iloc[:,0]

#Split the data into train and testing
X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.1, random_state=42)

#Print training data
print(“Training data\n”,X_train,”\n”,Y_train)

#Print testing data
print(“Testing data\n”,X_test)

#make dependent variable categorical
Y_train = np_utils.to_categorical(Y_train,num_classes=2)
Y_test = np_utils.to_categorical(Y_test,num_classes=2)

#create model
model = Sequential()
shape = X_train.shape[1]

#add layers to model
model.add(Dense(200, activation=’relu’, input_shape=(shape,)))
model.add(Dense(200, activation=’relu’))
model.add(Dense(200, activation=’relu’))
model.add(Dense(2, activation=’softmax’))

#Compile the model
model.compile(optimizer=’adam’, loss=’categorical_crossentropy’, metrics=[‘accuracy’])

#Here we train the Network., Y_train, batch_size=30, epochs = 10, verbose = 5)

#Here we evaluate the model
score,acc = model.evaluate(X_test,Y_test,verbose = 2,batch_size = 30)
#Predict the test results
prediction = model.predict(X_test)
length = len(prediction)
y_label = np.argmax(Y_test,axis=1)
predict_label = np.argmax(prediction,axis=1)

#classification report
print(“Confusion Matrix\n”,confusion_matrix(Y_test.argmax(axis=1),prediction.argmax(axis=1)))
print(“Classification Report\n”,classification_report(Y_test.argmax(axis=1),prediction.argmax(axis=1)))
accuracy = np.sum(y_label==predict_label)/length * 100
print(“Accuracy : “,accuracy)

#predict new value
new = [[68.01349487,120.45444004]]

#predict classes for test data
DNN_X_test = np.array(new)
DNN_y_pred = []
for i in range(0,len(new)):
print(“New prediction”,DNN_y_pred)

predict students gender using their height and weight data using Deep neural networks from keras in python
import necessary libraries
Confusion matrix
Load the data set
Classification report
import warnings
Precision and recall