# Running a Neural Network on the Raspberry Pi

1,021

5

Posted

## Introduction: Running a Neural Network on the Raspberry Pi

In my previous instructable, we installed Keras on our Raspberry Pi and in this instructable, we will be building a neural network using Keras. The neural network will be used to predict a person’s chances of having diabetes. First let's obtain our training data. You can get the training data here, download and name it as data.csv. The data is in the following format, where the list indices indicate the column number of the data in the CSV file.

1. Number of times pregnant
2. Plasma glucose concentration a 2 hours in an oral glucose tolerance test
3. Diastolic blood pressure (mm Hg)
4. Triceps skin fold thickness (mm)
5. 2-Hour serum insulin (mu U/ml)
6. Body mass index (weight in kg/(height in m)^2)
7. Diabetes pedigree function
8. Age (years)
9. Class (1 – Diabetic, 0 – Not Diabetic)

This project was done by me, Nikhil Raghavendra, a Diploma in Computer Engineering student from Singapore Polytechnic, School of Electrical and Electronic Engineering, under the guidance of my mentor Mr Teo Shin Jen.

## Step 1: The Code

Boot up your Raspberry Pi and go to your Desktop, create a folder and create a file named data.csv. Now copy the contents of the data.csv file in your computer onto the data.csv on your Raspberry Pi. After saving and closing it, create a main.py file and type the code found below.

```from keras.models import Sequentialfrom keras.layers import Dense
import numpy as np

np.random.seed(7)

# split into input (X) and output (Y) variables
X = dataset[:, 0:8]
Y = dataset[:, 8]

# create model
model = Sequential()

# Compile model
# Fit the model
model.fit(X, Y, epochs=500, batch_size=10, verbose=2)
# calculate predictions
# The list below contains patient data for whom we want to carry out the prediction
test = np.array([[6,148,72,35,0,33.6,0.627,50]])
predictions = model.predict(test)
# predict
rounded = [round(x[0]) for x in predictions]
print(rounded[0])```

## Step 2: Running the Network

Save it and then run it. It should take about 3 minutes to complete execution (Training, testing and prediction). I was only able to achieve an accuracy of around 80% so use your knowledge to tweak the code to achieve a higher accuracy.

## Recommendations

• ### 3D CAM and CNC Class

464 Enrolled

• ### Make it Move Contest

We have a be nice policy.