-
Notifications
You must be signed in to change notification settings - Fork 4
/
fruits_with_colors_KNNlearn.py
46 lines (34 loc) · 1.63 KB
/
fruits_with_colors_KNNlearn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#Building to Model to predict fruits data with colors using SKlearn and KNN
# Author: Rishu Shrivastava ([email protected])
# Date : June 4, 2017
import numpy as np
import matplotlib.pyplot as mp
import pandas as pd
from sklearn.model_selection import train_test_split
from matplotlib import cm
from sklearn.neighbors import KNeighborsClassifier
#reading data
fruits = pd.read_table('./data/fruit_data_with_colors.txt')
print("Displaying sample rows of Flower data set")
print(fruits.head())
#create a mapping from fruit label value to fruit name to make results easier to interpret
print("Lookup fruit names to make it easier to interpret the prediction")
lookup_fruit_name = dict(zip(fruits.fruit_label.unique(), fruits.fruit_name.unique()))
print(lookup_fruit_name)
#plotting scatter matrix
X = fruits[['height', 'width', 'mass']]
y = fruits['fruit_label']
#creating a train and test data set. Split it in 75%/25%
print("Generating train and test dataset")
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
cmap = cm.get_cmap('gnuplot')
scatter = pd.scatter_matrix(X_train, c= y_train, marker = 'o', s=40, hist_kwds={'bins':15}, figsize=(9,9), cmap=cmap)
mp.show()
# Training the Dataset using KNN algorithm | neighbours=5
print("Training KNNeighbour Classifier")
knn = KNeighborsClassifier(n_neighbors = 5)
knn.fit(X_train, y_train)
print("The ACCURACY score = ",knn.score(X_test,y_test))
# first example: a small fruit with mass 20g, width 4.3 cm, height 5.5 cm
fruit_prediction = knn.predict([[20, 4.3, 5.5]])
print("PREDICTING fruit with mass 20g, width 4.3 cm, height 5.5 cm : ",lookup_fruit_name[fruit_prediction[0]])