-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathAI_Workshop.py
140 lines (74 loc) · 1.68 KB
/
AI_Workshop.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
# coding: utf-8
# In[ ]:
# In[ ]:
# In[1]:
import pandas as pd
data=pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv',index_col=0)
# In[12]:
X=data[["TV","radio","newspaper"]].as_matrix()
Y=data[["sales"]].as_matrix()
X.shape
# In[13]:
X_train=X[:150,:]
X_test=X[150:,:]
Y_train=Y[:150,:]
Y_test=Y[150:,:]
# In[19]:
#Regression
# In[26]:
import seaborn as sns
get_ipython().magic('matplotlib inline')
sns.pairplot(data,x_vars=["TV","radio","newspaper"],y_vars=["sales"],kind='reg',size=7)
# In[27]:
#Sci-Kit learn
from sklearn.linear_model import LinearRegression
# In[28]:
linreg=LinearRegression()
# In[29]:
#.fit---Training using the dataset including features & Labels part
#.predict----predict using features
linreg.fit(X_train,Y_train)
# In[30]:
Y_pred=linreg.predict(X_test)
# In[36]:
import numpy as np
m=np.concatenate((Y_test,Y_pred),axis=1)
m
# In[45]:
#Evaluation Matices
#MAE-Mean Absolute error
#MSE-Mean Squared Error
#RMSE-Root Mean Squared Error
from sklearn import metrics
np.sqrt(metrics.mean_squared_error(Y_test,Y_pred))
# In[54]:
from sklearn.datasets import load_iris
iris=load_iris()
X=iris.data
Y=iris.target
# In[86]:
y=Y.reshape(150,1)
data=np.concatenate((X,y),axis=1)
data
from random import shuffle
shuffle(data)
# In[91]:
data
f=data[:,:4]
l=data[:,4:]
f_train=f[:100,:]
f_test=f[100:,:]
l_train=l[:100,:]
l_test=l[100:,:]
# In[ ]:
# In[96]:
from sklearn.neighbors import KNeighborsClassifier
knn=KNeighborsClassifier(n_neighbors=7)
knn.fit(f_train,l_train)
y_pred=knn.predict(f_test)
count=0
for i in range(0,50):
if (y_pred[i]==l_test[i]):
count=count+1
(count/50)*100
# In[ ]: