Datawhale动手数据分析第三章——模型搭建和评估
学习资料:https://github.com/datawhalechina/hands-on-data-analysis
第三章 模型搭建和评估 经过前面的两章知识点的学习,完成了对数据的基本了解,数据清洗,特征工程,数据可视化。这一章是使用数据,运用我们的数据以及结合业务来得到某些我们需要知道的结果。简单说就是:选择模型 → 输入数据 → 得到输出结果 → 评价模型 → 调整模型 。
数据处理与载入库 1 2 3 4 5 6 7 8 import pandas as pdimport numpy as npfrom sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LogisticRegressionfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.metrics import accuracy_score, classification_reportdata = pd.read_csv('clear_data.csv' )
数据切分 1 2 3 4 5 6 X = data.drop('Survived' , axis=1 ) y = data['Survived' ] X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2 , random_state=42 )
参数说明:
test_size :测试集占比,默认 0.25
random_state :随机种子,保证结果可复现
stratify :按比例分层抽样
模型搭建 逻辑回归 1 2 3 lr = LogisticRegression(max_iter=1000 ) lr.fit(X_train, y_train) y_pred_lr = lr.predict(X_test)
随机森林 1 2 3 rf = RandomForestClassifier(n_estimators=100 , random_state=42 ) rf.fit(X_train, y_train) y_pred_rf = rf.predict(X_test)
模型评估 1 2 3 4 print ("Logistic Regression Accuracy:" , accuracy_score(y_test, y_pred_lr))print ("Random Forest Accuracy:" , accuracy_score(y_test, y_pred_rf))print ("\nClassification Report (Random Forest):" )print (classification_report(y_test, y_pred_rf))
常用评估指标 :
指标
说明
Accuracy
准确率:预测正确的样本占比
Precision
精确率:预测为正的样本中真正为正的比例
Recall
召回率:真正为正的样本中被正确预测的比例
F1-Score
精确率和召回率的调和平均数
模型调参 交叉验证 1 2 3 4 from sklearn.model_selection import cross_val_scorescores = cross_val_score(rf, X, y, cv=5 ) print (f"5-fold CV accuracy: {scores.mean():.4 f} (+/- {scores.std() * 2 :.4 f} )" )
网格搜索 1 2 3 4 5 6 7 8 9 10 11 12 13 from sklearn.model_selection import GridSearchCVparam_grid = { 'n_estimators' : [50 , 100 , 200 ], 'max_depth' : [None , 10 , 20 ], 'min_samples_split' : [2 , 5 , 10 ] } grid_search = GridSearchCV(RandomForestClassifier(random_state=42 ), param_grid, cv=5 , scoring='accuracy' ) grid_search.fit(X_train, y_train) print (f"Best params: {grid_search.best_params_} " )print (f"Best score: {grid_search.best_score_:.4 f} " )