-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathdiff_ml_models.py
197 lines (167 loc) · 4.71 KB
/
diff_ml_models.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#%%
''' this is a implementation of the Linear regression model
from scratch without using any library '''
import numpy as np
X = 2* np.random.rand(100,1)
X
#%%
#the dependent equation i.e Y's value depends on Y
Y = 4 + 3 * X + np.random.randn(100, 1)
Y
#%%
#creates 100 X 1 shape matrix with value 1
np.ones((100,1))
#%%
X_b = np.c_[np.ones((100, 1)), X]
# add x0 = 1 to each instance
X_b
#%%
''' the normal equation'''
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(Y)
theta_best
#%%
X_new = np.array([[0], [2]])
X_new_b = np.c_[np.ones((2, 1)), X_new] # add x0 = 1 to each instance
y_predict = X_new_b.dot(theta_best)
y_predict
#%%
import matplotlib.pyplot as plt
plt.plot(X_new, y_predict, "r-")
plt.plot(X, Y, "g.")
plt.axis([0, 2, 0, 15])
plt.show()
#%%
#The equivalent implementation of the above Algorithm with library
from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X, Y)
lin_reg.intercept_, lin_reg.coef_
lin_reg.predict(X_new)
#%%
''' this is the implementation of the gradient descent algorithm
from scratch'''
eta = 0.1 # learning rate
n_iterations = 1000
m = 100
theta = np.random.randn(2,1)
theta
#%%
for iteration in range(n_iterations):
gradient = 2/m * X_b.T.dot(X_b.dot(theta) - Y)
theta = theta - eta * gradient
theta
#%%
'''
this is the implementation of the stochastic gradient
descent algorithm
'''
epochs = 50
t0 , t1 = (5,50)
def learning_rate(t):
return t0 / (t + t1)
for epoch in range(epochs):
for i in range(m):
random_index = np.random.randint(m)
x_i = X_b[random_index:(random_index + 1)]
y_i = Y[random_index:random_index+1]
gradients = 2 * x_i.T.dot(x_i.dot(theta) - y_i)
eta = learning_rate(epoch * m + i)
theta = theta - eta * gradients
theta
#%%
from sklearn.preprocessing import PolynomialFeatures
m = 100
X_p = 6 * np.random.rand(m, 1) - 3
Y_p = 0.5 * X_p**2 + X_p + 2 + np.random.randn(m, 1)
poly_features = PolynomialFeatures(degree=2 , include_bias=False)
X_poly = poly_features.fit_transform(X_p)
X_p[0]
X_poly[0]
#%%
lin_reg = LinearRegression()
lin_reg.fit(X_poly,Y_p)
print(lin_reg.intercept_ , lin_reg.coef_)
#%%
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
def plot_learning_curves(model,X,Y):
X_train,X_val,Y_train,Y_val = train_test_split(X,Y,test_size=0.2)
train_error,val_error = [],[]
for m in range(1,len(X_train)):
model.fit(X_train[:m],Y_train[:m])
Y_train_predict = model.predict(X_train[:m])
Y_val_predict = model.predict(X_val)
train_error.append(mean_squared_error(Y_train[:m],Y_train_predict))
val_error.append(mean_squared_error(Y_val,Y_val_predict))
plt.plot(np.sqrt(train_error), "r-+", linewidth=2, label="train")
plt.plot(np.sqrt(val_error), "b-", linewidth=3, label="val")
lin_reg = LinearRegression()
plot_learning_curves(lin_reg, X_p, Y_p)
#%%
from sklearn.pipeline import Pipeline
polynomial_regression = Pipeline((
("poly_features", PolynomialFeatures(degree=10, include_bias=False)),
("sgd_reg", LinearRegression()),
))
plot_learning_curves(polynomial_regression, X_p, Y_p)
#%%
'''
this is the implementation of ridge regression
'''
from sklearn.linear_model import Ridge
ridge_model = Ridge(alpha= 1 , solver="cholesky")
ridge_model.fit(X_p,Y_p)
ridge_model.predict([[1.5]])
#%%
from sklearn.linear_model import SGDRegressor
sgd_reg = SGDRegressor(penalty="l2")
sgd_reg.fit(X_p, Y_p.ravel())
sgd_reg.predict([[1.5]])
#%%
'''
this is a implementation of the Lasso Regression
'''
from sklearn.linear_model import Lasso
lasso_reg = Lasso(alpha=0.1)
lasso_reg.fit(X_p,Y_p)
lasso_reg.predict([[1.5]])
#%%
'''
this is a implementation of the Elastic net
'''
from sklearn.linear_model import ElasticNet
elastic_net = ElasticNet(alpha=0.1,l1_ratio=0.5)
elastic_net.fit(X_p,Y_p)
elastic_net.predict([[1.5]])
#%%
'''
this is the implementation of a simple Logistic regression
'''
from sklearn import datasets
iris = datasets.load_iris()
list(iris.keys())
#%%
X = iris["data"][:, 3:] # petal width
X
#%%
Y = (iris["target"] == 2).astype(np.int) # 1 if Iris-Virginica, else 0
Y
#%%
from sklearn.linear_model import LogisticRegression
log_reg = LogisticRegression()
log_reg.fit(X, Y)
#%%
X_new = np.linspace(0, 3, 1000).reshape(-1, 1)
y_proba = log_reg.predict_proba(X_new)
plt.plot(X_new, y_proba[:, 1], "g-", label="Iris-Virginica")
plt.plot(X_new, y_proba[:, 0], "b--", label="Not Iris-Virginica")
#%%
'''
this is the implementation of a softmax regression
'''
X = iris["data"][:, (2, 3)]
y = iris["target"]
softmax_reg = LogisticRegression(multi_class="multinomial",solver="lbfgs", C=10)
softmax_reg.fit(X, Y)
print(softmax_reg.predict([[5, 2]]))
print(softmax_reg.predict_proba([[5, 2]]))