Life and Tech Talk

머신러닝 분류 모델 성능 비교
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score, roc_auc_score 

# 문제 정의: 붓꽃의 종류를 예측하는 문제 (Classifying)
iris = load_iris()
iris.keys()
print(iris['target_names'])

# 데이터 전처리 
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
scaler=StandardScaler()
X = scaler.fit_transform(iris['data'])

x_train, x_test, y_train, y_test = train_test_split(X, iris['target'], test_size = 0.2, stratify=iris['target'], random_state=0)


# 분류 모델 평가를 위한 함수 정의
def get_cm_eval(y_test, y_pred):
    confusion = confusion_matrix(y_test, y_pred) # confusion matrix x축은 예측값, y축은 실제 값, 함수 파라미터 순서와는 다름
    accuracy = accuracy_score(y_test, y_pred) 
    precision = precision_score(y_test, y_pred, average='weighted') 
    recall = recall_score(y_test, y_pred, average='weighted') 
    F1 = f1_score(y_test, y_pred, average='weighted') 
    # AUC = roc_auc_score(y_test, y_pred, multi_class='ovr') # 'ovo': Computes the AUC for each pair of classes (one-vs-one). 'ovr': Computes the AUC for each class against the rest (one-vs-rest).
    print('\n오차행렬\n', confusion)
    print('정확도: {:.3f}'.format(accuracy))
    print('정밀도: {:.3f}'.format(precision))
    print('재현율: {:.3f}'.format(recall))
    print('F1 score: {:.3f}'.format(F1))


# 모델별 성능 비교
print("\n** KNeighborsClassifier **")
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors=7).fit(x_train, y_train) # n_neighbors는 최적 값을 GridSearchCV를 이용하여 찾았음
answer = model.score(x_train, y_train), model.score(x_test, y_test)
get_cm_eval(y_test, model.predict(x_test))

print("\n** LogisticRegression **")
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(C=0.1, max_iter=1000).fit(x_train, y_train)  # max_iter는 train data의 갯수보다 커야 함, 규제강도 변경 C=1/Alpha
answer2 = model.score(x_train, y_train), model.score(x_test, y_test)
get_cm_eval(y_test, model.predict(x_test))

print("\n** DecisionTreeClassifier **")
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(max_depth=5, random_state=0).fit(x_train, y_train)
answer3 = model.score(x_train, y_train), model.score(x_test, y_test)
print("모델 Feature 중요도")
print(pd.Series(model.feature_importances_, index=iris['feature_names']).sort_values(ascending=False))
get_cm_eval(y_test, model.predict(x_test))

print("\n** RandomForestClassifier **")
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators = 500, n_jobs=-1).fit(x_train, y_train)
answer4 = model.score(x_train, y_train), model.score(x_test, y_test)
print("모델 Feature 중요도")
print(pd.Series(model.feature_importances_, index=iris['feature_names']).sort_values(ascending=False))
get_cm_eval(y_test, model.predict(x_test))

print("\n***** 4개 모델의 Train Score, Test Score 비교 (정확도) *****")
print("KNeighborsClassifier: {:.3f}, {:.3f}".format(answer[0], answer[1]))
print("LogisticRegression: {:.3f}, {:.3f}".format(answer2[0], answer2[1]))
print("DecisionTreeClassifier: {:.3f}, {:.3f}".format(answer3[0], answer3[1]))
print("RandomForestClassifier: {:.3f}, {:.3f}".format(answer4[0], answer4[1]))

올린날: 2024년 2월 3일
주제: 코드 관련 포스팅 클릭!!

* 올린이: 마이클

* VIEW: 37       0           위키홈     게시판     수정