KT AIVLE/Python Syntax

머신러닝_지도_문법정리

bestone888 2024. 11. 3. 17:20
from sklearn.model_selection import train_test_split
x_train, x_val, y_train, y_val = train_test_split(x, y, test_size = 0.3, random_state = 1)
In [ ]:
# Linear Regression
from sklearn.linear_model import LinearRegression

model = LinearRegression()

# 회귀계수
model.coef_
model.intercept_
In [ ]:
# KNN
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neighbors import KNeighborsRegressor

model = KNeighborsClassifier(n_neighbors = n)
model = KNeighborsRegressor(n_neighbors = n)
In [ ]:
# 정규화
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler


# scaling
# 방법 1
scaler = MinMaxScaler()
x_train_s = scaler.fit_transform(x_train)
x_val_s = scaler.transform(x_val)

scaler = StandardScaler()
x_train_s = scaler.fit_transform(x_train)
x_val_s = scaler.transform(x_val)

# 방법 2
scaler = MinMaxScaler()
scaler.fit(x_train)
x_train_s = scaler.transform(x_train)
x_val_s = scaler.transform(x_val)
In [ ]:
# Decision Tree
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import DecisionTreeRegressor

model = DecisionTreeClassifier(max_depth = n, random_state = ?)
model = DecisionTreeRegressor(max_depth = n, random_state = ?)

# 변수 중요도
model.feature_importances_
In [ ]:
# Logistic Regression
from sklearn.linear_model import LogisticRegression

model = LogisticRegression(max_iter = ?)

# 회귀계수
model.coef_
model.intercept_
In [ ]:
# SVM (Supoort Vector Machine)
from sklearn.svm import SVC
from sklearn.svm import SVR

# scaling 필요

model = SVC(kernel = ?, C = , probability = True)
model = SVR(kernel = ?, C = , probability = True) 

# kernel: linear, poly, rbf(주로 비선형)
# probability: default는 False, True 일 때 model.predict_proba 사용 가능
In [ ]:
# cross_val_score
from sklearn.model_selection import cross_val_score
model = ?
cv_score = cross_val_score(model, x_train, y_train, cv = n, scoring = ?)

# 분류 모델 default: accuracy
# 회귀 모델 default: r2_score
In [ ]:
# random search
from sklearn.model_selection import RandomizedSearchCV

model_X = ?

model = RandomizedSearchCV(model_X,
                            param,
                            cv= 5,
                            n_iter = 20,    #random 하게 몇 번 실행할지
                            scoring = ?)
# scoring: 회귀에서는 default r2_score, 분류에서는 default accuracy

model.fit(x_train, y_train)

# 결과확인
model.cv_results_['mean_test_score']

# 학습 후 결과
model.best_estimator_
model.best_params_
model.best_score_
In [ ]:
# grid search
from sklearn.model_selection import GridSearchCV

model = GridSearchCV(model_X,
                    param,
                    cv = 5,
                    scoring = ?)

model.fit(x_train, y_train)

# 학습 후 결과
model.cv_results_['mean_test_score']
model.best_params_
model.best_score_
model.best_estimator_

# (decision tree) feature_importances_
model.best_estimator_.feature_importances_
In [ ]:
 
In [ ]:
# Voting
from sklearn.ensemble import VotingRegressor
from sklearn.ensemble import VotingClassifier

# estimators 예시
estimators = [('lr', model_lr), 
              ('knn', model_knn), 
              ('dt', model_dt), 
              ('rdf', model_rdf), 
              ('lgb', model_lgb)]

model = VotingRegressor(estimators=estimators, voting = 'hard')
model = VotingClassifier(estimators=estimators, voting = 'soft')

model.fit(x_train, y_val)
In [ ]:
# Stacking
# random forest
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestRegressor

model = RandomForestClassifier(max_depth = n, n_estimators = m, final_estimator = ?)
# final_estimator default: logistic regression

model = RandomForestClassifier(max_depth = n, n_estimators = m, final_estimator = ?)
# final_estimator default: linear regression
In [ ]:
# Boosting
# XGBoost
!pip install xgboost

from xgboost import XGBClassifier
from xgboost import XGBRegressor

model = XGBClassifier(max_depth = n, n_estimators = m, random_state = 1)

model.fit(x_train, y_train)

# 학습 후 결과
model.feature_importances_
In [ ]:
# Boosting
# LightGBM
!pip install lightgbm

from lightgbm import LGBMRegressor
from lighhgbm import LGBMClassifier

model = LGBMRegressor(max_depth = n, n_estimators = m, random_state = 1, verbose = -1)

model.fit(x_train, y_train)

# 학습 후 결과
model.feature_importances_
In [ ]:
# 평가 지표
accuracy_score(y_val, y_pred)
precision_score(y_val, y_pred)
recall_score(y_val, y_pred)
f1_score(y_val, y_pred)

'KT AIVLE > Python Syntax' 카테고리의 다른 글

딥러닝_문법정리  (0) 2025.01.22
lambda, filter, reduce  (0) 2024.10.06
데이터분석_문법정리  (0) 2024.10.06
데이터다듬기_문법정리  (0) 2024.09.29