Research breakthrough possible @S-Logix pro@slogix.in

Office Address

Social List

How to Build a Deep Neural Network Model Using Keras in Python?

Build Deep Neural Network Model using Keras in Python

Condition for Building a Deep Neural Network Model Using Keras in Python

  • Description:
    The code builds a deep neural network (DNN) using Keras for diabetesprediction based on the Pima Indians Diabetes dataset. It preprocesses the data by scaling the features, splits it into training and testing sets, and defines a simple neural network with two hidden layers. The model is trained, evaluated, and performance metrics (accuracy, F1-score, recall, precision) along with a confusion matrix heatmap are displayed for assessing its predictive performance.
Step-by-Step Process
  • Step1: Essential libraries like pandas, Keras, and scikit-learn are imported for data handling, neural network modeling, and evaluation.
  • Step2: The Pima Indians Diabetes dataset is loaded using pandas to prepare for preprocessing.
  • Step3: Null values in the dataset are checked to ensure data quality before proceeding with preprocessing.
  • Step4: A correlation matrix is generated using df.corr() to identify relationships between numeric features, followed by a heatmap to visualize these correlations.
  • Step5: Features are scaled using StandardScaler to normalize the data,ensuring better performance in neural networks.
  • Step6: The dataset is split into training and testing sets using train_test_split to evaluate the model's performance on unseen data.
  • Step7: A deep neural network (DNN) with two hidden layers is defined using Keras Dense and Dropout layers for classification.
  • Step8: The model is compiled with the Adam optimizer and binary cross-entropy loss function, suitable for binary classification tasks.
  • Step9: The model is trained on the training data for 50 epochs with a batch size of 2, and the validation data is used to evaluate performance during training.
  • Step10: After training, predictions are made on the test set,and performance metrics (accuracy, precision, recall, F1-score) are computed, followed by visualization of the confusion matrix.
Sample Code
  • #Import Necessary Libraries
    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    from sklearn.preprocessing import StandardScaler
    from sklearn.model_selection import train_test_split
    from tensorflow.keras.layers import Dense,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/diabetes.csv")
    #check null values
    df.isnull().sum()
    #calculate correlation between features
    # Compute the correlation matrix
    correlation_matrix = df.corr()
    # Display the correlation matrix
    print(correlation_matrix)
    # Plot the heatmap
    plt.figure(figsize=(10, 8))
    sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt='.2f',
    linewidths=0.5)
    plt.title('Correlation Heatmap')
    plt.show()
    x = df.drop('Outcome',axis=1)
    y = df['Outcome']
    #Scaling the input data
    scaler = StandardScaler()
    x = scaler.fit_transform(x)
    #Split the train_test_data
    X_train,X_test,y_train,y_test =
    train_test_split(x,y,test_size=.2,random_state=42)
    def DNN_model(input_shape):
    # Input layer
    inputs = Input(shape=(input_shape,))
    # Hidden layers
    layer1 = Dense(32, activation='relu')(inputs)
    layer2 = Dense(16, activation='relu')(layer1)
    # Output layer
    output_layer = Dense(1, activation='sigmoid')(layer2)
    # 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 = DNN_model(X_train.shape[1])
    model.fit(X_train,y_train,batch_size=2,epochs=50,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]
    # Calculate confusion matrix
    cm = confusion_matrix(y_test, y_pred)
    class_labels = ['Negative', 'Positive']
    # Plot the heatmap with correct labels
    plt.figure(figsize=(6, 5))
    sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=class_labels,
    yticklabels=class_labels)
    plt.title('Confusion Matrix Heatmap')
    plt.xlabel('Predicted Label')
    plt.ylabel('True Label')
    plt.show()
    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))
Screenshots
  • Build Deep Neural Network