-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathML Project.py
171 lines (106 loc) · 3.19 KB
/
ML Project.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
# coding: utf-8
# In[7]:
import sys
print('Python: {}'.format(sys.version))
import scipy
print("Scipy: {}".format(scipy.__version__))
import numpy
print("Numpy: {}".format(numpy.__version__))
import pandas
print("Pandas: {}".format(pandas.__version__))
import sklearn
print("Sklearn: {}".format(sklearn.__version__))
import matplotlib
print("Matplotlib: {}".format(matplotlib.__version__))
# In[14]:
import pandas
from pandas.plotting import scatter_matrix
from matplotlib import pyplot
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn import model_selection
from sklearn.ensemble import RandomForestClassifier
# In[15]:
url="https://raw.githubusercontent.com/Amit366/data/master/iris.csv"
dataset=pandas.read_csv(url)
# In[16]:
#dimensions
print(dataset.shape)
# In[17]:
#take a peek into the data
dataset.head(20)
# In[18]:
#sanatical summary
print(dataset.describe())
# In[19]:
#class distribution
print(dataset.groupby('variety').size())
# In[20]:
#univariate plots - box and whisker type
dataset.plot(kind='box',subplots=True,layout=(2,2),sharex=False,sharey=False)
pyplot.show()
# In[21]:
#histogram of the variables
dataset.hist()
pyplot.show()
# In[22]:
#multivariant plot
scatter_matrix(dataset)
pyplot.show()
# In[23]:
#creating validation dataset
#splitting dataset
array=dataset.values
x=array[:,0:4]
y=array[:,4]
x_train,x_validation,y_train,y_validation=train_test_split(x,y,test_size=0.2,random_state=1)
# In[31]:
#logistic Regression
#Linear Discriminant Analysis
#K-Nearest Neighbor
#Classification and Regression Trees
#Gaussian Naive Bayes
#Support Vector Machine
# Creating models
models=[]
models.append(('LR', LogisticRegression(solver='liblinear',multi_class='ovr')))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC(gamma='auto')))
# In[32]:
#Evaluating the models
results=[]
names=[]
for name,model in models:
kfold=StratifiedKFold(n_splits=10,random_state=1)
cv_results=cross_val_score(model,x_train,y_train,cv=kfold,scoring='accuracy')
results.append(cv_results)
names.append(name)
print('%s %f (%f)'% (name,cv_results.mean(),cv_results.std()))
# In[33]:
#compare our models
pyplot.boxplot(results,labels=names)
pyplot.title('Algorithm Comparision')
pyplot.show()
# In[38]:
#make a prediction
model=SVC(gamma='auto')
model.fit(x_train,y_train)
prediction=model.predict(x_validation)
# In[39]:
#evaluate our predictions
print(accuracy_score(y_validation,prediction))
print(confusion_matrix(y_validation,prediction))
print(classification_report(y_validation,prediction))