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

bro's coding

AICE.ML 본문

[CERTIFICATION]/AICE

AICE.ML

givemebro 2023. 7. 26. 02:29
반응형
Tranin, Test 데이터셋 분할Permalink
입력(X)과 레이블 (y) 나누기

 = df1.drop('Churn', axis=1).values
y = df1['Churn'].values

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y, random_state=42)
모델 개발Permalink
모델별 바차트 그려주고 성능 확인을 위한 함수
# 모델별로 Recall 점수 저장
# 모델 Recall 점수 순서대로 바차트를 그려 모델별로 성능 확인 가능

from sklearn.metrics import accuracy_score

my_predictions = {}

colors = ['r', 'c', 'm', 'y', 'k', 'khaki', 'teal', 'orchid', 'sandybrown',
          'greenyellow', 'dodgerblue', 'deepskyblue', 'rosybrown', 'firebrick',
          'deeppink', 'crimson', 'salmon', 'darkred', 'olivedrab', 'olive', 
          'forestgreen', 'royalblue', 'indigo', 'navy', 'mediumpurple', 'chocolate',
          'gold', 'darkorange', 'seagreen', 'turquoise', 'steelblue', 'slategray', 
          'peru', 'midnightblue', 'slateblue', 'dimgray', 'cadetblue', 'tomato'
         ]

# 모델명, 예측값, 실제값을 주면 위의 plot_predictions 함수 호출하여 Scatter 그래프 그리며
# 모델별 MSE값을 Bar chart로 그려줌
def recall_eval(name_, pred, actual):
    global predictions
    global colors

    plt.figure(figsize=(12, 9))

    #acc = accuracy_score(actual, pred)
    acc = recall_score(actual, pred)
    my_predictions[name_] = acc * 100

    y_value = sorted(my_predictions.items(), key=lambda x: x[1], reverse=True)
    
    df = pd.DataFrame(y_value, columns=['model', 'recall'])
    print(df)
   
    length = len(df)
    
    plt.figure(figsize=(10, length))
    ax = plt.subplot()
    ax.set_yticks(np.arange(len(df)))
    ax.set_yticklabels(df['model'], fontsize=15)
    bars = ax.barh(np.arange(len(df)), df['recall'])
    
    for i, v in enumerate(df['recall']):
        idx = np.random.choice(len(colors))
        bars[i].set_color(colors[idx])
        ax.text(v + 2, i, str(round(v, 3)), color='k', fontsize=15, fontweight='bold')
        
    plt.title('recall', fontsize=18)
    plt.xlim(0, 100)
    
    plt.show()
로지스틱 회귀Permalink
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix 
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import classification_report

# 로지스틱 회귀로 학습

lg = LogisticRegression()
lg.fit(X_train, y_train)

# 분류기 성능 평가
lg.score(X_test, y_test)

lg_pred = lg.predict(X_test)

# 오차 행렬
# TN FP
# FN TP

confusion_matrix(y_test, lg_pred)

# 정확도
accuracy_score(y_test, lg_pred)

# 정밀도
precision_score(y_test, lg_pred)

# 재현율
recall_score(y_test, lg_pred)

# 정밀도 + 재현율
f1_score(y_test, lg_pred)

# 모델별 바차트
recall_eval('LogisticRegression', lg_pred, y_test)
KNN(K-Nearest Neighbor)Permalink
from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)

# 모델별 바차트

knn_pred = knn.predict(X_test)
recall_eval('K-Nearest Neighbor', knn_pred, y_test)
결정트리(DecisionTree)Permalink
from sklearn.tree import DecisionTreeClassifier

dt = DecisionTreeClassifier(max_depth=10, random_state=42)
dt.fit(X_train, y_train)

# 모델별 바차트
dt_pred = dt.predict(X_test)
recall_eval('DecisionTree', dt_pred, y_test)
랜덤포레스트(RandomForest)Permalink
from sklearn.ensemble import RandomForestClassifier

rfc = RandomForestClassifier(n_estimators=3, random_state=42)
rfc.fit(X_train, y_train)

# 모델별 바차트
rfc_pred = rfc.predict(X_test)
recall_eval('RandomForest Ensemble', rfc_pred, y_test)
XGBoostPermalink
설치 : !pip install xgboost

from xgboost import XGBClassifier

xgb = XGBClassifier(n_estimators=3, random_state=42)
xgb.fit(X_train, y_train)

xgb_pred = xgb.predict(X_test)
recall_eval('XGBoost', xgb_pred, y_test)
Light GBMPermalink
설치 = !pip install lightgbm

from lightgbm import LGBMClassifier

lgbm = LGBMClassifier(n_estimators=3, random_state=42)  
lgbm.fit(X_train, y_train)

lgbm_pred = lgbm.predict(X_test)
recall_eval('LGBM', lgbm_pred, y_test)
반응형

'[CERTIFICATION] > AICE' 카테고리의 다른 글

AICE.REF  (0) 2023.07.26
AICE.DNN  (0) 2023.07.26
AICE.NORMALIZITION  (0) 2023.07.26
AICE.DATA.PREPROCESSING  (0) 2023.07.26
AICE.DATA.VISUALIZITION  (0) 2023.07.26
AICE.DATA.PREPROCESSING  (0) 2023.07.26
Comments