Python: Predictive Analysis - supervised learning: k-nearest neighbors algorithm | APDaga Tech

Python: Predictive Analysis - supervised learning: k-nearest neighbours algorithm | APDaga Tech
In this post,
We will load the data from the csv file kept on local machine and then apply basic machine learning algorithm: k-nearest neighbors on it.

This is a type of supervised learning, we will provide new input to the program and will check whether we are getting desirable results.



Programming Language: Python 3
Software: Anaconda Jupyter Notebook
Here, csv file contains temperature data and the format of the the csv file is as follows:
S0, S1, S2  => Input Parameters
CONDITION  => Output

You can read it as:
For given S0, S1, S2 parameters, Condition of the system is given.

Eg.
When S0 = 51, S1 = 35 and S2 = 14 then the Condition is good.



input_dataset.csv:
S0,S1,S2,CONDITION
51,35,14,good
49,30,14,good
47,32,13,good
46,31,15,good
50,36,14,good
54,39,18,good
46,34,14,good
50,34,15,good
44,29,14,good
49,31,15,good
54,37,15,good
48,34,16,good
48,30,14,good
43,30,11,good
58,40,12,good
57,44,15,good
54,39,13,good
51,35,14,good
57,38,17,good
54,34,17,good
51,38,15,good
51,37,15,broken
46,36,10,broken
51,33,17,broken
48,34,19,broken
50,30,16,broken
50,34,16,broken
52,35,15,broken
52,34,14,broken
47,32,16,broken
69,32,57,broken
56,28,49,broken
77,28,67,broken
63,27,49,broken
67,33,57,broken
72,32,60,broken
62,28,48,broken
61,30,49,broken

Run the following code in Anaconda Jupyter Notebook.
code block and its expected output is shown below:

Code In[1]:

# coding: utf-8

# In[1]:
## In this example, we will predict the Condition with 3 input parameters S0, S1 & S2.
## Here, we are demonstrating supervised learning: k-nearest neighbours algorithm
## Input data for learning is: input_dataset.csv

## Run this code in Anaconda Jupyter (Python 3)
import pandas as pd
data= pd.read_csv(open('/Users/apdaga/Downloads/input_dataset.csv'))
data
Output In[1]:
S0 S1 S2 CONDITION
0 51 35 14 good
1 49 30 14 good
2 47 32 13 good
3 46 31 15 good
4 50 36 14 good
5 54 39 18 good
6 46 34 14 good
7 50 34 15 good
8 44 29 14 good
9 49 31 15 good
10 54 37 15 good
11 48 34 16 good
12 48 30 14 good
13 43 30 11 good
14 58 40 12 good
15 57 44 15 good
16 54 39 13 good
17 51 35 14 good
18 57 38 17 good
19 54 34 17 good
20 51 38 15 good
21 51 37 15 broken
22 46 36 10 broken
23 51 33 17 broken
24 48 34 19 broken
25 50 30 16 broken
26 50 34 16 broken
27 52 35 15 broken
28 52 34 14 broken
29 47 32 16 broken
30 69 32 57 broken
31 56 28 49 broken
32 77 28 67 broken
33 63 27 49 broken
34 67 33 57 broken
35 72 32 60 broken
36 62 28 48 broken
37 61 30 49 broken


Code In[2]:
# In[2]:
out_condition = data['CONDITION']
out_condition
Output In[2]:
0  good
1 good
2 good
3 good
4 good
5 good
6 good
7 good
8 good
9 good
10 good
11 good
12 good
13 good
14 good
15 good
16 good
17 good
18 good
19 good
20 good
21 broken
22 broken
23 broken
24 broken
25 broken
26 broken
27 broken
28 broken
29 broken
30 broken
31 broken
32 broken
33 broken
34 broken
35 broken
36 broken
37 broken
Name: CONDITION, dtype: object




Code In[3]:
# In[3]:
del data['CONDITION']
print (data)
Output In[3]:
S0 S1 S2
0 51 35 14
1 49 30 14
2 47 32 13
3 46 31 15
4 50 36 14
5 54 39 18
6 46 34 14
7 50 34 15
8 44 29 14
9 49 31 15
10 54 37 15
11 48 34 16
12 48 30 14
13 43 30 11
14 58 40 12
15 57 44 15
16 54 39 13
17 51 35 14
18 57 38 17
19 54 34 17
20 51 38 15
21 51 37 15
22 46 36 10
23 51 33 17
24 48 34 19
25 50 30 16
26 50 34 16
27 52 35 15
28 52 34 14
29 47 32 16
30 69 32 57
31 56 28 49
32 77 28 67
33 63 27 49
34 67 33 57
35 72 32 60
36 62 28 48
37 61 30 49

X_pred is the new input to the program for which we will find out the condition based on predictive analysis using supervised learning: k-nearest neighbors algorithm.

Code In[4]:

# In[4]:
from sklearn import neighbors, datasets
x, y = data, out_condition

# create the model
knn = neighbors.KNeighborsClassifier(n_neighbors=5, weights='uniform')

# fit the model
knn.fit(x, y)

#Predict the condition
X_pred = [30, 30, 40] # Input values for which we need to predict the condition
result = knn.predict([X_pred, ]) # Predicting the output condition
print(result) # print the predicted condition
#print(knn.predict_proba([X_pred, ]))
Output In[4]:
['broken']

--------------------------------------------------------------------------------

Click here to see more codes for Raspberry Pi 3 and similar Family.
&
Click here to see more codes for NodeMCU ESP8266 and similar Family.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.

Feel free to ask doubts in the comment section. I will try my best to solve it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.

Thanks and Regards,
-Akshay P. Daga


Post a Comment (0)
Previous Post Next Post