Introduction:
The ANN neural network contains three layers, The first one is input layer and the last one is the output layer, IN between the input and output layer there will be two hidden layers. You can ask one question like, What happens if I add extra layers, I have also the same doubt while making this script, The Information about the layers are,You can add multiple numbers of hidden layers, But You can add only 1 input and 1 output layer, You can’t add more number input and output layers. So it is the general structure of ANN, Next i will explain you what are things running behind the each and every layer.
First layer of structure is input layer, As the name suggests, It only receive the different format inputs from the programmer, It is basic duty of input layer in every deep learning neural network, And the second layers are the hidden layers, The name itself says like it is hidden layer, It will find the hidden features and patterns behind every input. Final one is output layer, It will collect the transformations of input data that operated in hidden layer and it present the data results to user. This is the general workflow of the ANN,
Source code:
Imports:
import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn import preprocessing from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split import seaborn as sns from keras.layers import Dense, BatchNormalization, Dropout, LSTM from keras.models import Sequential from tensorflow.keras.utils import to_categorical from keras import callbacks from sklearn.metrics import precision_score, recall_score, confusion_matrix, classification_report, accuracy_score, f1_score
Load data:
#loading data
data = pd.read_csv("/content/heart_failure_clinical_records_dataset.csv") data.head()
Feature selection:
#assigning values to features as X and target as y
X=data.drop(["DEATH_EVENT"],axis=1) y=data["DEATH_EVENT"]Scaling:
#Set up a standard scaler for the features
col_names = list(X.columns) s_scaler = preprocessing.StandardScaler() X_df= s_scaler.fit_transform(X) X_df = pd.DataFrame(X_df, columns=col_names) X_df.describe().T
Splitting data:#spliting test and training sets X_train, X_test, y_train,y_test = train_test_split(X_df,y,test_size=0.25,random_state=7)
Model Structure:
early_stopping = callbacks.EarlyStopping( min_delta=0.001, # minimium amount of change to count as an improvement patience=30, # how many epochs to wait before stopping restore_best_weights=True) # Initialising the NN model = Sequential() # layers model.add(Dense(units = 16, kernel_initializer = 'uniform', activation = 'relu', input_dim = 12)) model.add(Dense(units = 8, kernel_initializer = 'uniform', activation = 'relu')) model.add(Dropout(0.25)) model.add(Dense(units = 4, kernel_initializer = 'uniform', activation = 'relu')) model.add(Dropout(0.5)) model.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid')) from tensorflow.keras.optimizers import SGD # Compiling the ANN model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
Training model:
history = model.fit(X_train, y_train, batch_size = 32, epochs = 500,callbacks=[early_stopping], validation_split=0.2)
Prediction:
# Predicting the test set results y_pred = model.predict(X_test) y_pred = (y_pred > 0.5) np.set_printoptions()Confusion matrix:cmap1 = sns.diverging_palette(275,150, s=40, l=65, n=6) plt.subplots(figsize=(12,8)) cf_matrix = confusion_matrix(y_test, y_pred) sns.heatmap(cf_matrix/np.sum(cf_matrix), cmap = cmap1, annot = True, annot_kws = {'size':15})
Thanks for reading article,Support..!
Nice blog. Thank you for sharing.
ReplyDeletePython Development Service
hire Python Developer