Question

In: Computer Science

Write a neural network in python for multiclass classification of an imbalanced dataset that makes a...

Write a neural network in python for multiclass classification of an imbalanced dataset that makes a successful model that both trains and evaluates the model and prints the Accuracy, Precision, Recall, and F1 score. Data will be in the form of a CSV file with 600,000 samples (or rows in the CSV) of 15 classes (for y_train and y_test) and 78 input dimensions/features. The imbalance will have some classes that have a low of only 8 samples taken. Your job is the make a neural network to fix those imbalances and optimize the network for the best Accuracy, Precision, Recall, and F1 score for all 15 classes.

Sample data -

22 6 386359 22 20 1912 2665 640 0 86.90909 137.688 976 0 133.25 268.7713 11846.5 108.7072 9423.39 22717.21 122019 5 385442 18354.38 29591.49 122019 212 386353 20334.37 43298.97 161209 10 0 0 0 0 712 648 56.94186 51.76533 0 976 106.4419 207.2919 42969.92 0 0 0 1 0 0 0 0 0 108.9762 86.90909 133.25 0 0 0 0 0 0 22 1912 20 2665 26883 230 16 32 0 0 0 0 0 0 0 0 Class 4
0 0 1.13E+08 3 0 0 0 0 0 0 0 0 0 0 0 0 0.026634 56319965 33.23402 56319988 56319941 1.13E+08 56319965 33.23402 56319988 56319941 0 0 0 0 0 0 0 0 0 0 0 0.026634 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 -1 -1 0 0 0 0 0 0 56319965 33.23402 56319988 56319941 Class 7

Solutions

Expert Solution

#Importing the numpy to perform Linear Algebraic operations on the data

import numpy as np

#Import pandas library to perform the data preprocessing

import pandas as pd

#importing the Keras deep learning framework of Python

import keras

#Importing the Sequential model from keras

from keras.models import Sequential

#Importing the types of layers in the Neural Network that we are going to have

from keras.layers import Dense

#Importing the train_test_split function which is useful in dividing the dataset into the training and testing data

from sklearn.model_selection import train_test_split

#Importing the StandardScaler function to perform the standardisation/scaling of the data

from sklearn.preprocessing import StandardScaler

#Importing the metries for the performance evaluation of our deep learning model

from sklearn import metrics

def data_preprocessing(data):

#As you have told that there are 78 features hence the 78th index position will be having the class labels

#This is because the indexing starts from 0 in Pandas in Python

X = data.iloc[:, 0:78]

y = data.iloc[:78]

#I have splitted the dataset into a ratio of 80:20 between the train and test you can try other ratios as well

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 23)

#Creating an object of StandardScaler

sc = StandardScaler()

#Scaling the data using the StandardScaler() object

X_train = sc.fit_transform(X_train)

X_test = sc.transform(X_test)

return X_train, X_test, y_train, y_test

def train_and_evaluate(X_train, X_test, y_train, y_test):

neural_classifier = Sequential()

#output_dim = number of nuerons in first hidden layer

#init = initializing the weights of the neural network

#input_dim = number of neuron in the input layer = number of input features = 78 in your case

#Actiavtion = activation function that is used in each layer

#Dense is the type of layer

#Input layer

neural_classifier.add(Dense(output_dim = 100, init = 'uniform', activation = 'relu', input_dim = 78))

#First hidden layer

neural_classifier.add(Dense(output_dim = 150, init = 'uniform', activation = 'relu'))

#Second hidden layer

neural_classifier.add(Dense(output_dim = 200, init = 'uniform', activation = 'relu'))

#Third hidden layer

neural_classifier.add(Dense(output_dim = 250, init = 'uniform', activation = 'relu'))

#Fourth hidden layer

neural_classifier.add(Dense(output_dim = 300, init = 'uniform', activation = 'relu'))

#Fifth hidden layer

neural_classifier.add(Dense(output_dim = 350, init = 'uniform', activation = 'relu'))

#Sixth hidden layer

neural_classifier.add(Dense(output_dim = 400, init = 'uniform', activation = 'relu'))

#Seventh hidden layer

neural_classifier.add(Dense(output_dim = 250, init = 'uniform', activation = 'relu'))

#Eighth hidden layer

neural_classifier.add(Dense(output_dim = 300, init = 'uniform', activation = 'relu'))

#Output layer

#output layer has 15 neurons because there are 15 classes in your dataset

#Since it is a multiclass classification problem hence we are using the softmax activation function

#If it was a binary classification problem then we could have used thee sigmoid activation function

neural_classifier.add(Dense(output_dim = 15, init = 'uniform', activation = 'softmax'))

#Optimizer is ADAM which is a kind of optimization algorithm for minimizing the loss on each and every epoch

neural_classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

#Epochs = number of times we will train our network

neural_classifier.fit(X_train, y_train, batch_size = 10, epochs = 100)

#Predicting the labels for the test data

y_pred = neural_classifier.predict(X_test)

#Calculating the accuracy score

accuracy = metrics.accuracy_score(y_test, y_pred)

#Calculating the precision score

precision = metrics.precision_score(y_test, y_pred)

#Calculating the recall score

recall = metrics.recall_score(y_test, y_pred, average='weighted')

#Calculating the f1-score

f1score = metrics.f1_score(y_test, y_pred, average='weighted')

return accuracy, precision, recall, f1score

def main():

data = pd.read_csv('Filename.csv')

X_train, X_test, y_train, y_test = data_preprocessing(data)

accuracy, precision, recall, f1_score = train_and_evaluate(X_train, X_test, y_train, y_test)

print("Accuracy score of the model is :", accuracy)

print("precision score of the model is :", accuracy)

print("Recall score of the model is :", accuracy)

print("f1-score of the model is :", accuracy)

main()

screenshots:


Related Solutions

if someone has a dataset and they want to build a neural network, how would someone...
if someone has a dataset and they want to build a neural network, how would someone be able to progam that in python for a machine learning project
write a fully-connected neural network to work with MNIST and Tensorflow 2.x. The labels will be...
write a fully-connected neural network to work with MNIST and Tensorflow 2.x. The labels will be input and MNIST image vectors as output labels. use tf.GradientTape instead fit. show the 28x28 image with the input vector. PLEASE WRITE CODE AND DO NOT SENT ME DIFFERENT PARTS TO A WEBSITE. It is clear, it's a homework assignment.  
From the MNIST dataset introduced in class, write code in Python that a) Splits the 42000...
From the MNIST dataset introduced in class, write code in Python that a) Splits the 42000 training images into a training set (50% of all the data) and a test set (the rest). The labels should also be split accordingly. (PLEASE ONLY SOLVE 2 & 3) 2) Basically repeat Part 1, but now use 80% of the images for training and the other 20% for testing. Report scores. [10 points] 3) Use the SVM model from part 2 to print...
Predictive Modeling Using Neural Networks (For SAS Enterprise Miner software) In preparation for a neural network...
Predictive Modeling Using Neural Networks (For SAS Enterprise Miner software) In preparation for a neural network model, is the imputation of missing values needed? Why or why not?
what does resent50 function do in neural network ?
what does resent50 function do in neural network ?
Briefly assess the relationships between Biological and Artificial Neural Network
Briefly assess the relationships between Biological and Artificial Neural Network
Subject: Neural network and Pattern recognition (Deep Learning) Given the following partial network definitioon: Input -...
Subject: Neural network and Pattern recognition (Deep Learning) Given the following partial network definitioon: Input - A 28 x 28 RGB image First Layer - 7, 5 x 5 filters. Activation function: Sigmoid Second Layer - 23, 3 x 3 filters. Activation function: RelU Third Layer - 20, 3 x 3 filters. Activation function: RelU You task is to do design(s) of the next part of the network (so that it uses the output of the third layer). The purpose...
Say that a neural network has been constructed to predict the creditworthiness of applicants. There are...
Say that a neural network has been constructed to predict the creditworthiness of applicants. There are two output nodes: one for yes (1 = yes, 0 = no) and one for no (1 = no, 0 = yes). An applicant receives a score of 0.83 for the “yes” output node and a 0.44 for the “no” output node. Discuss what may have happened and whether the applicant is a good credit risk. explain with your own words please
short essay describe how a neural network function. include the " use it or lose it"...
short essay describe how a neural network function. include the " use it or lose it" concept and the description of how individual cells work to transmit information. 2) you are driving a car. describe the functions of the various parts of your nervous system, including brain structures, are performing. include at least ten parts of the nervous system in your description.
Training a convolutional neural network for speech recognition, one finds that performance on the training set...
Training a convolutional neural network for speech recognition, one finds that performance on the training set is very good while the performance on the validation set is unacceptably low. A reasonable fix might be to: (Select the single best answer) And please give a explanation why they are true or false (A) Decrease the weight decay (B) Reduce the training set size (C) Reduce the number of layers and neurons (D) Increase the number of layers and neurons
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT