-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIris program.py
75 lines (48 loc) · 1.53 KB
/
Iris program.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
import pandas as pd
from sklearn import neighbors
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import tree
df = pd.read_csv('iris.csv')
print(df.head(10), '\n')
print(df.shape, '\n')
print(df.columns, '\n')
print(df['variety'].value_counts(), '\n')
# sepal.length , sepal.width plot
df.plot(kind='scatter', x='sepal.length', y='sepal.width')
plt.show()
df.plot(kind='scatter', x='petal.length', y='petal.width')
plt.show()
# petal.length, petal.width plot
sns.set_style('whitegrid');
sns.FacetGrid(df, hue='variety', height=4)\
.map(plt.scatter, 'sepal.length', 'sepal.width')\
.add_legend();
plt.show()
sns.barplot(x='petal.length', y='petal.width', data=df)
plt.show()
# define X and y
X = np.array(df.drop(['variety'], axis=1))
y = df['variety']
df.hist()
plt.show()
# Train Test model
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2)
# KNearest classifier
clf = neighbors.KNeighborsClassifier(n_neighbors=3)
# find patterns in data
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
print('KNN : ', accuracy)
# decision tree classifier
clf = tree.DecisionTreeClassifier()
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
print('Decision Tree : ', accuracy)
print(X[110], y[110])
example_measure=np.array([[5.9, 3.2, 3.5, .8]])
example_measure=example_measure.reshape(len(example_measure), -1)
prediction=clf.predict(example_measure)
print(prediction)