반응형
머신러닝
1-1) 로지스틱 (회귀,분류) Logistic Regression
C=규제강도, max_iter = 반복횟수
# import
from sklearn.linear_model import LogisticRegression
# 모델 생성
lg = LogisticRegression(C=1.0, max_iter=1000)
# 모델 학습
lg.fit(X_train, y_train)
# 모델 평가
lg.score(X_test, y_test)
1-2) 의사결정 나무 (회귀.분류)
# import
from sklearn.tree import DecisionTreeClassifier # 분류
# 모델 생성
dt = DecisionTreeClassifier(max_depth=5, random_state=2023)
# 모델 학습
dt.fit(X_train, y_train)
# 모델 평가
dt.score(X_test, y_test)
# import
from sklearn.tree import DecisionTreeRegressor # 회귀
# 모델 생성
dt = DecisionTreeRegressor(max_depth=5, random_state=2023)
# 모델 학습
dt.fit(X_train, y_train)
# 모델 평가
dt.score(X_test, y_test)
1-3) 랜덤 포레스트 (회귀,분류)
n_estimators: 사용하는 트리 개수
random_state: 랜덤 시드
# import
from sklearn.ensemble import RandomForestClassifier # 분류
# 모델 생성
rf = RandomForestClassifier(n_estimators=100, random_state=2023)
# 모델 학습
rf.fit(X_train, y_train)
# 모델 평가
rf.score(X_test, y_test)
# import
from sklearn.ensemble import RandomForestRegressor # 회귀
# 모델 생성
rf = RandomForestRegressor(n_estimators=100, random_state=2023)
# 모델 학습
rf.fit(X_train, y_train)
# 모델 평가
rf.score(X_test, y_test)
1-4) XGBoost
n_estimators: 사용하는 트리 개수
# install
!pip install xgboost
# import
from xgboost import XGBClassifier # 분류
# 모델 생성
xgb = XGBClassifier(n_estimators=100)
# 모델 학습
xgb.fit(X_train, y_train)
# 모델 평가
xgb.score(X_test, y_test)
# install
!pip install xgboost
# import
from xgboost import XGBRegressor # 회귀
# 모델 생성
xgb = XGBRegressor (n_estimators=100)
# 모델 학습
xgb.fit(X_train, y_train)
# 모델 평가
xgb.score(X_test, y_test)
1-5) Light GBM
# install
!pip install lightgbm
# import
from lightgbm import LGBMClassifier # 분류
# 모델 생성
lgbm = LGBMClassifier(n_estimators=100)
# 모델 학습
lgbm.fit(X_train, y_train)
# 모델 평가
lgbm.score(X_test, y_test)
# install
!pip install lightgbm
# import
from lightgbm import LGBMegressor # 회귀
# 모델 생성
lgbm = LGBMegressor (n_estimators=100)
# 모델 학습
lgbm.fit(X_train, y_train)
# 모델 평가
lgbm.score(X_test, y_test)
머신러닝 모델평가 (MAE) : 오차(error)라서 낮을수록 좋다.
from sklearn.metrics import mean_absolute_error
y_pred = model.predict(X_test)
mae = mean_absolute_error(y_valid,y_pred)
머신러닝 모델평가 (Accuracy) : 정확도가 높을수록 좋다.
from sklearn.metrics import accuracy_score
acc_s = accuracy_score(y_valid, y_pred)
머신러닝 모델평가 정밀도( Precision), 재현율(recall)
#정밀도(precision)
from sklearn.metrics import precision_score
precision_score(y_valid, y_pred)
#재현율(recall)
from sklearn.metrics import recall_score
recall_score(y_valid, y_pred)
랜덤포레스트 피쳐중요도
#ft_im변수에 변수 중요도 저장
ft_im = rf.feature_importances_
#series전환 및 정렬
ft_series = pd.Series(ft_im, index = X.columns)
ft_top5 = ft_series.sort_values(ascending=False)[:5]
#시각화
import seaborn as sb
sb.barplot(x=ft_top5,y=ft_top5.index)
Confusion Matrix 시각화하기
TN FP
FN TP
from sklearn.metrics import confusion_matrix # 혼동행렬
# 오차행렬
# TN FP
# FN TP
y_pred = model.predict(X_test) # 모델 예측값 저장
cf_matrix = confusion_matrix(y_test, y_pred)
print(cf_matrix) # 혼동 행렬
# 히트맵으로 시각화
sns.heatmap(cf_matrix, # 혼동 행렬
annot=True, # 주석
fmt='d') # 주석 포맷
plt.show()
728x90
'IT 자격증 > AICE Associate' 카테고리의 다른 글
AICE Associate - 데이터 전처리2 (기타 등등) (0) | 2024.06.19 |
---|---|
AICE Associate - 딥러닝 (0) | 2024.06.19 |
AICE Associate - 데이터 전처리 (0) | 2024.06.19 |
AICE Associate - 데이터 시각화 (0) | 2024.06.19 |
AICE Associate - 탐색적 데이터 분석 (0) | 2024.06.19 |
댓글