givemebro
2020. 5. 8. 09:21
반응형
import numpy as np
from sklearn.datasets import load_iris
# data 준비
iris=load_iris()
x = iris.data[:,2]
y = iris.data[:,3]
# 초기화
w = np.random.randn()/10
b = np.random.randn()/10
# learning rate 설정
lr = 0.05
pred_y = x*w + b
mse = np.mean((y-pred_y)**2)
costs = [mse]
for i in range(3000):
pred_y = x*w + b
dw = lr*2/len(y)*np.sum((y-pred_y)*x)
db = lr*2/len(y)*np.sum(y-pred_y)
w_new = w + dw
b_new = b + db
mse_new = np.mean((y-(x*w_new + b_new))**2)
# 종료점
if mse_new>=mse: break
w = w_new
b = b_new
mse = mse_new
costs.append(mse)
print(i, w, b, mse)
881 0.4164191208275526 -0.3665139932459091 0.04228994631948473
plt.plot(costs[:100])

(array([0.41641913]), -0.3665140452167275)
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(x.reshape(-1,1), y)
model.coef_, model.intercept_반응형