[AI]/python.sklearn
sklearn.linearRegression. basic
givemebro
2020. 4. 9. 17:59
반응형
X=iris.data[:,:3]
# 만약 X=iris.data[:,3] 이렇게 넣으면 차원이 맞지 않는다.
# ex) error : x=[1,2,3] ->
# sol1 ) X=[[1],[2],[3]]
# sol2 ) X=iris.data[:,[2]]
# sol3 ) X=iris.data[:,2].reshape(-1,1)
# because : X는 2차원 형태여야 한다.
y=iris.data[:,3]
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y)
from sklearn.linear_model import LinearRegression
model=LinearRegression()
model.fit(X_train,y_train)
pred_y=model.predict(X_test)
model.score(X_test,y_test)
import matplotlib.pyplot as plt
plt.scatter(pred_y,y_test,c=y_test)
plt.plot([0,3],[0,3],'r:')
plt.xlabel('pred_y')
plt.ylabel('test_y')
# 원본에 대한 비율
import matplotlib.pyplot as plt
plt.scatter(pred_y,pred_y/y_test,c=y_test)
plt.xlabel('pred_y')
plt.ylabel('test_y')
plt.hlines(1,0,pred_y.max(),linestyle=':')
index=np.argsort(y_test)
plt.plot(y_test[index],'bo:',alpha=0.3)
plt.plot(pred_y[index],'ro:',alpha=0.3)
plt.legend(['test_y','pred_y'])
model.coef_ # u= ax+by+cz > (a,b,c) 와 비슷한 개념(가중치, 기울기)
# array([-0.23392987, 0.24854725, 0.53728789])
model.intercept_# u= ax+by+cz > (u) 와 비슷한 개념(절편)
# -0.2123844501296439
반응형