- 🌈所属专栏:【机械学习】
- ✨作者主页: Mr.Zwq
- ✔️个人简介:一个正在努力学技术的Python领域创作者,擅长爬虫,逆向,全栈方向,专注基础和实战分享,欢迎咨询!
您的点赞、关注、收藏、评论,是对我最大的激励和支持!!!🤩🥰😍
目录
安装
数据
使用
MLP模型
逻辑回归模型
随机森林模型
SGDClassifier模型
XGBoost模型
网格搜索优化XGBoost模型
总结
安装
pip install scikit-learn
数据
X,y即为所需要进行回归处理的数据。
操作:拆分为训练集和测试集
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, random_state=12)
使用
MLP模型
# 训练MLP模型
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import roc_auc_score
mlp_model = MLPClassifier(hidden_layer_sizes=(64,32), activation='relu', solver='adam',max_iter=200, random_state=12)
mlp_model.fit(X_train,y_train)
y_pred_mlp = mlp_model.predict_proba(X_test)[:,1]
# auc分数越接近1表示模型性能越好
auc_mlp = roc_auc_score(y_test, y_pred_mlp)
print(f'MLP Model AUC:{auc_mlp:.4f}')
逻辑回归模型
# 逻辑回归模型
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
lrmodel=LogisticRegression(solver='liblinear')
lrmodel.fit(X_train,y_train)
y_lrpred=lrmodel.predict_proba(X_test)[:,1]
auc = roc_auc_score(y_test, y_lrpred)
print(f'LogisticRegression Model AUC:{auc:.4f}')
随机森林模型
# 随机森林模型
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_auc_score
rfmodel=RandomForestClassifier()
rfmodel.fit(X_train,y_train)
y_rfpred=rfmodel.predict_proba(X_test)[:,1]
auc = roc_auc_score(y_test, y_rfpred)
print(f'RandomForestClassifier Model AUC:{auc:.4f}')
SGDClassifier模型
# SGDClassifier模型
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import matthews_corrcoef,make_scorer,roc_auc_score
param_grid=[{
'loss':['hinge'],
'alpha':[10**k for k in range(-3,4)],
'max_iter':[1000],
'tol':[1e-3],
'random_state':[random_state],
'class_weight':[None,'balanced'],
'warm_start':[True]},
{'loss':['log'],
'penalty':['l2','l1'],
'alpha':[10**k for k in range(-3,4)],
'max_iter':[1000],
'tol':[1e-3],
'random_state':[random_state],
'class_weight':[None,'balanced'],
'warm_start':[True]}]
grid=GridSearchCV(estimator=SGDClassifier(),param_grid=param_grid,scoring=make_scorer(matthews_corrcoef),n_jobs=1,pre_dispatch=1,verbose=1,return_train_score=True)
grid.fit(X_train,y_train)
y_SGDCpred=grid.predict(X_test)
auc = roc_auc_score(y_test, y_SGDCpred)
print(f'SGDClassifier Model AUC:{auc:.4f}')
XGBoost模型
安装:pip install xgboost
import xgboost as xgb
from sklearn.metrics import roc_auc_score
# 训练XGBoost模型
xgb_model = xgb.XGBClassifier(use_label_encoder=False, eval_metric='logloss')
xgb_model.fit(X_train, y_train)
y_pred_xgb = xgb_model.predict_proba(X_test)[:,1]
auc_xgb = roc_auc_score(y_test, y_pred_xgb)
print(f'XGBoost Model AUC:{auc_xgb:.4f}')
网格搜索优化XGBoost模型
# 网格搜索优化XGBoost模型
import xgboost as xgb
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import GridSearchCV
param_grid = {
'n_estimators':[50,100,200],
'max_depth':[3,4,5],
'learning_rate':[0.01,0.1,0.2]
}
xgb_model = xgb.XGBClassifier(use_label_encoder=False, eval_metric='logloss')
grid_search = GridSearchCV(estimator=xgb_model, param_grid=param_grid, scoring='roc_auc',cv=3)
grid_search.fit(X_train, y_train)
best_xgb_model = grid_search.best_estimator_
y_pred_best_xgb = best_xgb_model.predict_proba(X_test)[:,1]
auc_best_xgb = roc_auc_score(y_test, y_pred_best_xgb)
print(f'Optimized XGBoost Model AUC:{auc_best_xgb:.4f}')
总结
感谢观看,原创不易,如果觉得有帮助,请给文章点个赞吧,让更多的人看到。🌹🌹🌹
👍🏻也欢迎你,关注我。👍🏻
如有疑问,可在评论区留言哦~