머신러닝

juyeon·2022년 10월 27일
0

데이터 다루기

목록 보기
34/35
post-custom-banner
  • 여기서는 머신러닝 극히 일부만, 간단하게.
  • 자세한거는 별도로 글 써야지

모델링: 기본 형태

  • 로지스틱 회귀를 예로 들면
model = LogisticRegression()
model.fit(x_train, y_train)
pred = model.predict(x_test)

회귀

모델들

from sklearn.linear_model import LinearRegression # 선형회귀
from sklearn.tree import DecisionTreeRegressor # 결정트리
from sklearn.ensemble import RandomForestRegressor # 랜덤포레스트
from sklearn.neighbors import KNeighborsRegressor # KNN

!pip install xgboost
from xgboost import XGBRegressor # xgboost

평가

import

from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error, mean_absolute_percentage_error

출력 해보자..

print(mean_squared_error(y_test, pred)) # MSE
print(mean_squared_error(y_test, pred, squared = False)) # RMSE
print(mean_absolute_error(y_test, pred)) # MAE(평균오차)

# MAPE(평균오차율): 원래 개념은 100(%)을 곱해야한다. 그렇지만 sklearn 함수는 100 안 곱한 채로 결과를 준다.
print(mean_absolute_percentage_error(y_test, pred))
## 1 - MAPE : 회귀모델의 정확도

분류

모델들

from sklearn.linear_model import LogisticRegression # 로지스틱 회귀
from sklearn.tree import DecisionTreeClassifier # 결정트리
from sklearn.ensemble import RandomForestClassifier # 랜덤포레스트
from sklearn.neighbors import KNeighborsClassifier # KNN

!pip install xgboost
from xgboost import XGBClassifier # xgboost

평가

import

from sklearn.metrics import confusion_matrix
from sklearn.metircs import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import classification_report

출력 해보자..

print(confusion_matrix(y_test, pred))
print(accuracy_score(y_test, pred))
print(precision_score(y_test, pred))
print(recall_score(y_test, pred))
print(f1_score(y_test, pred))

Feature importance 그래프

feature_importance_series = pd.Series(model.feature_importance_, index=df.drop(columns=[target]).columns)
feature_importance_top10= feature_importance_series.sort_values(ascending=False)[:10]

plt.fiure(figsize=(10, 10))
sns.barplot(x=feature_importance_top10, y=feature_importance_top10.index)
plt.title('Top 10 Feature Importance')
plt.show()
profile
내 인생의 주연
post-custom-banner

0개의 댓글