数据集采用的是kaggle比赛中公开的数据集——泰坦尼克号
对之前的机器学习方法分别进行了预测。
包括:逻辑回归0.7901234567901234、
神经网络0.7878787878787877、
KNN0.8125701459034792、
决策树0.8080808080808081、
随机森林0.7991021324354657、0.8181818181818182
Bagging0.8282828282828283、和随机森林做集成
Adaboost???????0.8181818181818182、和bagging做集成
Stacking0.8125701459034792
整体代码:
import pandastitanic = pandas.read_csv("titanic_train.csv")# 空余的age填充整体age的中值
titanic["Age"] = titanic["Age"].fillna(titanic["Age"].median())
print(titanic.describe())print(titanic["Sex"].unique())# 把male变成0,把female变成1
titanic.loc[titanic["Sex"] == "male", "Sex"] = 0
titanic.loc[titanic["Sex"] == "female", "Sex"] = 1print(titanic["Embarked"].unique())
# 数据填充
titanic["Embarked"] = titanic["Embarked"].fillna('S')
# 把类别变成数字
titanic.loc[titanic["Embarked"] == "S", "Embarked"] = 0
titanic.loc[titanic["Embarked"] == "C", "Embarked"] = 1
titanic.loc[titanic["Embarked"] == "Q", "Embarked"] = 2from sklearn.preprocessing import StandardScaler# 选定特征
predictors = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked"]
x_data = https://www.it610.com/article/titanic[predictors]
y_data = titanic["Survived"]# 数据标准化
scaler = StandardScaler()
x_data = https://www.it610.com/article/scaler.fit_transform(x_data)# 逻辑回归
from sklearn import model_selection
from sklearn.linear_model import LogisticRegression
# 逻辑回归模型
LR = LogisticRegression()
# 计算交叉验证的误差
scores = model_selection.cross_val_score(LR, x_data, y_data, cv=3)
# 求平均
print(scores.mean())
# 0.7901234567901234# 神经网络模型
from sklearn.neural_network import MLPClassifier
# 建模
mlp = MLPClassifier(hidden_layer_sizes=(20,10),max_iter=1000)
# 计算交叉验证的误差
scores = model_selection.cross_val_score(mlp, x_data, y_data, cv=3)
# 求平均
print(scores.mean())
# 0.7878787878787877# KNN模型
from sklearn import neighbors
knn = neighbors.KNeighborsClassifier(21)
# 计算交叉验证的误差
scores = model_selection.cross_val_score(knn, x_data, y_data, cv=3)
# 求平均
print(scores.mean())
# 0.8125701459034792# 决策树模型
from sklearn import tree
# 决策树模型
dtree = tree.DecisionTreeClassifier(max_depth=5, min_samples_split=4)
# 计算交叉验证的误差
scores = model_selection.cross_val_score(dtree, x_data, y_data, cv=3)
# 求平均
print(scores.mean())
# 0.8080808080808081# 随机森林模型
from sklearn.ensemble import RandomForestClassifier
RF1 = RandomForestClassifier(random_state=1, n_estimators=10, min_samples_split=2)
# 计算交叉验证的误差
scores = model_selection.cross_val_score(RF1, x_data, y_data, cv=3)
# 求平均
print(scores.mean())
# 0.7991021324354657RF2 = RandomForestClassifier(n_estimators=100, min_samples_split=4)
# 计算交叉验证的误差
scores = model_selection.cross_val_score(RF2, x_data, y_data, cv=3)
# 求平均
print(scores.mean())
# 0.8181818181818182# Bagging
from sklearn.ensemble import BaggingClassifier
bagging_clf = BaggingClassifier(RF2, n_estimators=20)
# 计算交叉验证的误差
scores = model_selection.cross_val_score(bagging_clf, x_data, y_data, cv=3)
# 求平均
print(scores.mean())
# 0.8282828282828283# AdaBoost模型
from sklearn.ensemble import AdaBoostClassifier
# AdaBoost模型
adaboost = AdaBoostClassifier(bagging_clf,n_estimators=10)
# 计算交叉验证的误差
scores = model_selection.cross_val_score(adaboost, x_data, y_data, cv=3)
# 求平均
print(scores.mean())
# 0.8181818181818182# Stacking
from sklearn.ensemble import VotingClassifier
from mlxtend.classifier import StackingClassifiersclf = StackingClassifier(classifiers=[bagging_clf, mlp, LR],
meta_classifier=LogisticRegression())sclf2 = VotingClassifier([('adaboost',adaboost), ('mlp',mlp), ('LR',LR),('knn',knn),('dtree',dtree)])# 计算交叉验证的误差
scores = model_selection.cross_val_score(sclf2, x_data, y_data, cv=3)
# 求平均
print(scores.mean())
# 0.8125701459034792
【机器学习|机器学习项目实战——11集成学习算法之泰坦尼克号船员获救预测】
推荐阅读
- 机器学习|【机器学习实战】决策树——构造决策树
- 机器学习|数学、机器学习、深度学习目录
- 日常|你的外卖为什么他来送——聊一聊外卖订单的生命周期
- 智能优化算法|智能优化算法(冠状病毒群体免疫优化算法 -附代码)
- 人工智能|简述神经网络的发展历史,神经网络的起源和发展
- 数学建模|数学建模(线性回归模型)
- pytorch|pytorch快速入门(五)——pytorch 生成对抗网络
- 机器学习|神经网络(七)优化与正则化
- 机器学习|神经网络(六)循环神经网络