반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

bro's coding

머신러닝.linear_model.LogisticRegression(Class=4)(iris) 본문

[AI]/python.sklearn

머신러닝.linear_model.LogisticRegression(Class=4)(iris)

givemebro 2020. 4. 13. 15:35
반응형

https://broscoding.tistory.com/114

 

머신러닝.iris data 불러오기

import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load_iris iris=load_iris() iris dir(iris) ['DESCR', 'data', 'feature_names', 'target', 'target_names'] iris.data.shape..

broscoding.tistory.com

https://broscoding.tistory.com/115

 

머신러닝.테스트데이터 뽑기

from sklearn.model_selection import train_test_split X_train,X_test,y_train,y_test=train_test_split(iris.data,iris.target,shuffle=False) # shuffle=False : 섞지 않겠다 # 기본 75% / 25% np.bincount(y_..

broscoding.tistory.com

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

iris=load_iris()

X_train,X_test,y_train,Y_test=train_test_split(iris.data,iris.target)

 

from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train,y_train)

 

score_train=model.score(X_train,y_train)
score_test=model.score(X_test,Y_test)
display(score_test,score_train)

# 0.9473684210526315
# 0.9732142857142857

 

display(model.coef_,model.intercept_)
# 값이 클수록 자기 자신과 가까워짐!
array([[ 0.3861533 ,  1.34189594, -2.11701033, -0.96016453], # 첫번째 꽃 를 구분하는 평면
       [ 0.42678801, -1.45440384,  0.36384584, -0.94339699], # 두번째 꽃 을 구분하는 평면
       [-1.59312057, -1.26910331,  2.26604178,  2.20306414]]) # 세번째 꽃 을 구분하는 평면
       
# 0.4*sl+1.4*sw-2.2*pl-pw
# 위 식에 속성값을 대입 했을 때, 양수이면 두번째 꽃

 

np.round(model.predict_proba(X_test),3)[:5],Y_test[:5]
array([[0.001, 0.315, 0.684],
        [0.818, 0.182, 0.   ],
        [0.001, 0.205, 0.794],
        [0.   , 0.411, 0.589],
        [0.022, 0.7  , 0.278]]),
        
array([2, 0, 2, 2, 1])

 

np.round(model.predict_proba(X_test),3)[Y_test!=pred_y],Y_test[Y_test!=pred_y]
array([[0.008, 0.283, 0.71 ],
        [0.042, 0.448, 0.509]]),
array([1, 1])

 

testS=[] # test score 보관
trainS=[] # trian score 보관
cs=[1*(10**i) for i in range(-5,5)] # c값 생성
for c in cs:
	# c 값에 따른 모델 설정
    model=LogisticRegression(C=c)
    # 모델 훈련
    model.fit(X_train,y_train)
    
    # score data 수집
    testS.append(model.score(X_test,Y_test))
    trainS.append(model.score(X_train,y_train))

# 시각화
plt.plot(testS,'r:o',label='pred_test')
plt.plot(trainS,'bo:',label='pred_train')
plt.xticks(range(len(cs)),cs) # x좌표 단위 세팅
plt.ylim(0,1)
plt.legend()
plt.xlabel('C')
plt.ylabel('Score')

반응형
Comments