Crash Course: Neural Networks Part 5: Easy Python Implementation – DataDrivenInvestor

As promised in Part 4 of this neural network crash course, I will now teach you how to implement a neural network in python, even if you have no prior experience with programming. I will walk you through each step of the way, from installing the required program, Anaconda, and installing the required packages in Python. Arm yourself with patience, and let’s get right into it!

Preethi Viswanathan on Unsplash

Anaconda Installation

The easiest way to set up your environment for programming machine learning-related projects is using Anaconda. Anaconda is a programming environment focused on Python and Machine learning.

It comes with all the tools you might need by just installing the program. I will teach you how to do this on Windows, but it should also be easy to do on Mac. And if you are on Linux, you should already know how to do this

To install Anaconda you need to go to this link.

Once there, click on Download, wait for it to finish, and then open the executable. Then just click next, next, I agree… etc. It should be really easy to install.

Once you have it installed you just need to use the search bar and write “anaconda”. Click the anaconda prompt to open it, and then we can get to the next part. Don’t worry if you are not familiar with the command prompt. I promise everything I show you is very easy, and at the end of this tutorial, you will feel like a hacker, working with the console!

By Author

Virtual Environment

Once you have the anaconda command prompt open, we can start doing something very important to ensure we don’t screw anything up. We will create a python virtual environment to install any tool we will need for our coding session.

First things first, create an environment by writing the following command in the Anaconda Prompt: “conda create -n nn_tutorial -y”. If everything goes well, you should have this:

Creating Virtual Environment in anaconda — By Author

As you can see above, after creating the virtual environment, it tells you how to activate it by using the “conda activate nn_tutorial”. Let’s do this right now!

Activating environment — By Author

That is all you need to do to create a virtual environment. As you can see, the “(base)” now changed into “(nn_tutorial)”, showing that you are inside the “nn_tutorial” virtual environment.

It’s very easy, right? Let’s get to installing the packages needed to create a neural network. This is the last step before actually building a neural network!

Installing packages

Let’s install the packages we need. This will also be very easy, just copy and paste the commands I give you, or write them by hand exactly as I say. the installation may take a bit, under 3–5 minutes. Be sure to run the commands in order so that everything works well! Also press “Yes” if you get a prompt while installing any of the packages.

  1. Install spyder using “conda install spyder -y”.
  2. Update python using “conda update python -y

2. Install pandas using “conda install pandas -y”.

3. Install TensorFlow using “pip install tensorflow”.

4. Install ScikitLearn using “conda install scikit-learn -y

Now you should have all the tools you need to build a Neural Network in Python. Write the following command in the console to open the IDE in which we will code the neural network: “spyder”.

A program called “Spyder” should open right now. We are done setting things up!

Let’s build the neural network!

The Neural Network

Now, for the fun part, you will learn how to build a neural network. Don’t worry, you will get the exact code template needed to implement it, you will just have to do a simple copy and paste.

But first, you need to get the dataset we will use for this little experiment and place it in the same folder as your neural network python file. I will teach you how to do both of them, step by step.

We will use the simplest of them all, the Iris dataset. This is an extremely simple dataset. It has 4 independent variables (or features/properties) and 1 dependent variable (what you need to predict). The purpose of the dataset is to detect to which species an iris plant belongs.

The 4 independent variables are:

  • SepalLength in Cm
  • SepalWidth in Cm
  • PetalLength in Cm
  • PetalWidth in Cm

The dependent variable is simply one of 3 species to which the iris plant belongs, based on the independent variables above. Here is a sneak peek of the dataset.

Iris dataset in Excel — By Author

You can get the dataset from Kaggle by clicking this text, but you first need to register an account if you want to download it.

Otherwise, I set you a direct google drive link for direct download.

You need to now create a folder, wherever you like, to place the dataset in. Remember where that folder is, as you need to also save the python file you will use, for simplicity’s sake.

Now, you should have the Spyder program we installed earlier opened. You need to go to the top left corner at File->Save as-> and make a save of your code in the same folder as the Iris dataset.

Saving python file — By Author

With your new file saved in the same folder as your dataset, there is only one thing left to do before pasting the code I am going to give you: to set the console working directory with one simple click, as you can see below. You do this by right-clicking the file, as in the image.

Setting working directory — by Author

Now, all you have to do is copy the following code to your Spyder screen. Sorry for the rough no-space format, but I had to make sure there won’t be any problems (hopefully) when you copy-paste this code into your Spyder.

#=======================================

import pandas as pd
import tensorflow as tf
dataset = pd.read_csv(‘Iris.csv’)
X = dataset.iloc[:,:-1].values # All columns except the last one in the Independent Variable Vector
y = dataset.iloc[:,-1].values # Last Column in the Dependent variable vector
#Transforming the Dependent variable from text to matematical values! Neural Networks work with Numbers!
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y = le.fit_transform(y)
y = pd.get_dummies(y)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test=train_test_split(X,y,test_size=0.2,random_state=42)
#Scaling the values, for better results
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
#Neural Network Model
ann = tf.keras.models.Sequential()
#Hidden Neuron Layers
ann.add(tf.keras.layers.Dense(units=6,activation=’relu’))# First Neural Layer
ann.add(tf.keras.layers.Dense(units=6,activation=’relu’))# Second Neural Layer
ann.add(tf.keras.layers.Dense(units=6,activation=’relu’))# Third Neural Layer
#Output Layer
ann.add(tf.keras.layers.Dense(units=3,activation=’softmax’)) #softmax for multiple categories
##Compile NN
#categorical crossentropy for non binary
adam_opt = tf.keras.optimizers.Adam(learning_rate=0.005)
ann.compile(optimizer=adam_opt,loss=’categorical_crossentropy’,metrics=[‘accuracy’])
#Train the NN
ann.fit(X_train,y_train,batch_size=8,epochs=100,validation_split=0.1)
# Testing your model on the dataset
y_pred=ann.predict(X_test)

#=======================================

Now you should be able to train the network! You just have to press F5 on your keyboard while in Spyder or click Run on the top left side of the Spyder Screen, and the network should start training. You should see the following screen on the bottom right side.

Neural Network Training — By Author

Also, click the Variable Explorer button, as in the image above, so that you can see how good your model was at predicting the Iris Species, after training!

To do that, you have to double-click the y_pred, which is the values predicted by the neural network you just trained, and then double-click the y_test, which is what the neural network should have predicted if it was perfect. Open them up now! It should look like this:

Network Prediction vs Real Prediction — by Author

As you can see above, on the left side you can see what your network predicted. On the right side, you can see what it should have predicted. As you can see, it did a pretty good job! You just trained a neural network to “think”. It can now predict Iris species!

In part 6

I hope you had the patience and perseverance to go through this whole article and successfully implement your neural network. If anything from the above didn’t work, let me know and I will try to help you fix it! Or, if you have another dataset you want to apply the code to, I will help you fit the code to do that.

In the next neural network tutorial, we will be talking about Convolutional Neural Networks, which are a type of neural network model that can detect objects in images. We will talk a bit about their history and then learn about how they actually work.

Stay tuned!

Spread the love

Leave a Reply

Your email address will not be published.