Hands on Neural Networks with keras
Greetings geeks..! today i’m going to talk about a simple machine learning example that will predict whether a person have diabetes or not( Do we need to give some info to predict whether we have diabetes ?). Of course yes, you do need to give some information to predict the prediction. Or else how we know whether a person have diabetes or not.
So i’m guessing that you have heard about a technology name called neural network. If not lemme explain little bit about neural nets (If you are familiar with the keyword, don’t hesitate to skip below paragraph).
First of all, neural network is a sub-field of machine learning. You must have definitely heard of machine learning right now since it is a hot topic now days. Neural network is something that mimic the the human brain. It simply identify patterns and predict something further using previously identified pattern.
Since now you have a small idea about neural nets, lets move on to the main topic of my article.
When we step into a machine learning problem, first we need to ask these two questions from your self. What is your problem and how you gonna solve it. When you think about your problem, you can have quick sight about what your goal is and what are the resources you have currently to accomplish that goal.
So lets breakdown out problem according to the way we defined above.
Goal — Predict diabetes
Resources — Data-set that have diabetes data.
Now we have identified our goal and resources. But what is our data-set ?
We are using a famous data-set name called “Pima Indians Diabetes dataset”. Snapshot of few rows of the data-set is given later in this article.
In neural networks, we need to feed some data to the network to learn. Our neural network learn a pattern from the given data-set and predict a given feature. In the prima indians dataset, you can see that there are 9 columns. 9th column(Outcome) is the column that save whether a person have diabetes or not. Outcome is given as a binary value. If the value is 1, that means that certain person have diabetes and vise versa. We need 1 to 8 columns data to predict the 9th column data. 1–8 columns are called input variables while 9th column is called as output variable.
First download the data-set and load it on your text editor. Using pandas i load my data-set to the text editor.
Now we loaded our data-set into a pandas dataframe(Dataframe is a fancy name given to pandas object that use to save and represent tabular data which are rows and columns). When you run above code you get below result.
As a best practice, the first thing that i do when i use a data-set is, check whether there are missing values in our data-set. Because if there are any missing values(Null values, NaN, NA), it will affect our prediction code later. Because of that reason, i personally check for missing value in the data-set by typing below line.
dataset.isna().sum() # this will give the sum of missing value rows
We can see that we don’t have any missing values in our data-set. Yes, we are clean to go now. Now lets divide our data-set into input and output variables.
Before we split the data, we take our dataset into a numpy array and then we split the dataset.
inputVariables is shown below
outputVariables is shown below
Now lets build some modaaaal…
Neural network building is done by using keras library.
In above code snippet, we developed a neural network. But we didn’t do any training yet. We just build a simple neural network blue print and it is yet to configure and train. Lemme explain what above code really stand for.
As we all know, neural network is a collection of neurons and neural network consist of layers. Below image depicts a simple neural network with one hidden layer.
Below diagram depicts the architecture of our neural network that we build just now.
At the beginning we create a sequential model.
model = sequential()
Then we add layer by layer to our sequential model until we are comfortable with our model . In this blog, i only added 3 layers excluding input layer. When we define our first layer, we define the dimensions of our input. Always remember the last layer we add defines our output layer.
model.add(Dense()) X 3 times
Now we have given build the architecture of our model. Next we need to configure the model for training and then we train the network.
When you run above code, your network will train the dataset according to our neural network. When i fit the network, I provided the training data (inputVariables), target data (outputVariables), epoches and batch size. Our network trains the model for a given number of epochs (iterations on a dataset).
Batch size means the number of samples that feed to the network at a time. Imagine our dataset contains 30 rows of data and we define batch_size as 3. Then at the very beginning,1st,2nd,3rd rows feed to the network, then 4th, 5th and 6th rows feed to the network. Like wise whole dataset is fed to the network in one epoch. Then same process happens for number of epochs we defined.
After after training your dataset, you can check the accuracy of your network by evaluating your model.
Below screen shot shows the accuracy of the model we developed.
Full code is shown below…