1. 需求分析泰坦尼克号顾客生存预测本案例用GBDT实现。2. 数据说明数据来源https://www.kaggle.com/c/titanic字段含义PassengerId乘客 IDSurvived目标标签0 死亡 / 1 存活Pclass舱位1 一等 / 2 二等 / 3 三等Name姓名Sex性别 male/femaleAge年龄大量缺失SibSp船上兄弟姐妹 / 配偶数Parch船上父母 / 子女数Ticket船票编号Fare票价Cabin船舱号缺失极多Embarked登船港 C/Q/S3. 建模import pandas as pd from matplotlib import pyplot as plt from sklearn.ensemble import GradientBoostingClassifier from sklearn.metrics import classification_report, auc, roc_curve from sklearn.model_selection import train_test_split, GridSearchCV from sklearn.tree import DecisionTreeClassifier import joblib3.1 加载数据# 1. 获取数据 train pd.read_csv(./data/titanic_train.csv, sep,) train.head()3.2 数据预处理提取特征与标签缺失值、异常值特征编码划分数据集# 2. 数据预处理 # 2.1 提取特征和标签 x train.drop([PassengerId,Cabin,Name,Ticket,Survived], axis1) y train[Survived] y.value_counts() # 2.2 缺失值处理 x[Age] x[Age].fillna(x[Age].mean()) # 2.3 分类字段编码 # 查看分类字段分布 # str_col x_train.select_dtypes(includestr).columns.tolist() # for col in str_col: # print(x_train[col].value_counts()) # print(-*50) # 创建one-hot编码分类少的字段可以用 x pd.get_dummies(x) # 2.4 划分数据集 x_train, x_test, y_train, y_test train_test_split(x, y, test_size0.2, random_state666)3.3 特征工程不用3.4 模型训练3.4.1 场景1单一决策树# 3. 模型训练 # 3.1 场景1单一cart树 estimator1 DecisionTreeClassifier(random_state666) estimator1.fit(x_train, y_train) y_pre1 estimator1.predict(x_test) print(单一决策树分类报告:\n, classification_report(y_test, y_pre1))3.4.2 场景2GBDT默认参数# 3.2 场景2集成学习GBDT默认参数 estimator2 GradientBoostingClassifier(random_state666) estimator2.fit(x_train, y_train) y_pre2 estimator2.predict(x_test) print(GBDT默认参数分类报告:\n, classification_report(y_test, y_pre2))3.4.3 场景3GBDT网格搜索交叉验证# 3.3 场景3集成学习GBDT网格搜索交叉验证 estimator3 GradientBoostingClassifier(random_state666) param_grid { n_estimators: [50,100,150], max_depth: [1,3,5], learning_rate: [0.001,0.005,0.01,0.05,0.1,0.5,1] } gs_estimator GridSearchCV(estimator3, param_grid, cv3) gs_estimator.fit(x_train, y_train) y_pre3 gs_estimator.predict(x_test) print(GBDT网格搜索交叉验证分类报告:\n, classification_report(y_test, y_pre3)) print(GBDT网格搜索交叉验证最佳准确率:, gs_estimator.best_score_) print(GBDT网格搜索交叉验证最佳参数:, gs_estimator.best_params_)3.5 模型评估# 4. 最优模型评估 estimator GradientBoostingClassifier(learning_rate0.05, max_depth5, n_estimators150, random_state666) estimator.fit(x_train, y_train) y_pre estimator.predict(x_test) print(最优GBDT分类报告:\n, classification_report(y_test, y_pre))# 可视化 fpr, tpr, thresholds roc_curve(y_test, estimator.predict_proba(x_test)[:,1]) auc_value auc(fpr, tpr) plt.figure(figsize(3,3)) plt.plot(fpr, tpr, labelAUC %.2f % auc_value) plt.plot([0,1], [0,1], r--) plt.xlabel(False Positive Rate) plt.ylabel(True Positive Rate) plt.legend() plt.show()3.6 模型保存# 5. 模型保存 joblib.dump(estimator, ./model/gbdt_titanic_model.pkl)3.7 模型预测# 7. 模型预测 # 模型加载 estimator joblib.load(./model/gbdt_titanic_model.pkl) # 读取要预测的数据 test pd.read_csv(./data/titanic_test.csv, sep,) # 提取特征 x_test_out test.drop([PassengerId,Cabin,Name,Ticket], axis1) # 缺失值处理填充值要与训练集一致 x_test_out [Age] x_test_out [Age].fillna(x[Age].mean()) x_test_out [Fare] x_test_out [Fare].fillna(x[Fare].median()) # 特征编码 x_test_out pd.get_dummies(x_test_out) # 预测 y_pre_out estimator.predict(x_test_out) test[Survived] y_pre_out test.head()