Titanic 模型融合

本文介绍并展示了两种模型融合技术——投票法和Stacking,在Titanic生存预测任务上的应用。通过使用随机森林、XGBoost和决策树等算法,对比了单一模型与融合模型的性能差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.投票法

#模型融合
X = train[['Pclass', 'Sex', 'SibSp', 'Parch', 'Cabin', 'Embarked',
       'title', 'isalone', 'Family', 'mother', 'person', 'ticket-same', 'age',
       'fare']]
Y = train['Survived']
#投票法
from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.metrics import *
import matplotlib.pyplot as plt
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=2018)
from sklearn.ensemble import VotingClassifier, RandomForestClassifier
from xgboost import XGBClassifier
from sklearn import tree
rfc = RandomForestClassifier(n_estimators=10, criterion='entropy')
xgb = XGBClassifier()
dtc = tree.DecisionTreeClassifier()
clf_vc = VotingClassifier(estimators=[('rfc', rfc),('xgb', xgb),('dtc', dtc)])
clf_vc.fit(X_train, Y_train)
print(clf_vc.score(X_test, Y_test))

2.stacking融合

#stacking融合
from mlxtend.classifier import StackingCVClassifier
sclf = StackingCVClassifier(classifiers=[rfc, xgb], meta_classifier=dtc, use_probas=True)
sclf.fit(X_train.values, Y_train.values)
print(sclf.score(X_test.values, Y_test.values))

好的,我将为您提供一个大致的R语言代码流程,供您参考。 1. 数据探索与清洗 ```R # 导入数据 titanic <- read.csv("train.csv") # 查看数据基本信息 str(titanic) # 查看数据摘要信息 summary(titanic) # 查看数据前几行 head(titanic) # 查看数据后几行 tail(titanic) # 查看每列的缺失值情况 sapply(titanic, function(x) sum(is.na(x))) # 删除无用的列 titanic$PassengerId <- NULL titanic$Name <- NULL titanic$Ticket <- NULL titanic$Cabin <- NULL ``` 2. 特征工程 ```R # 对数据进行特征工程 # 将性别转化为二元变量 titanic$Sex <- as.integer(titanic$Sex == "female") # 将登船港口转化为三元变量 titanic$Embarked[titanic$Embarked == "S"] <- 1 titanic$Embarked[titanic$Embarked == "C"] <- 2 titanic$Embarked[titanic$Embarked == "Q"] <- 3 titanic$Embarked[is.na(titanic$Embarked)] <- 0 # 将年龄缺失值填充为中位数 titanic$Age[is.na(titanic$Age)] <- median(titanic$Age, na.rm = TRUE) # 将船票价格分为四个等级 titanic$FareLevel <- cut(titanic$Fare, breaks = c(0, 10, 50, 100, Inf), labels = c(1, 2, 3, 4)) # 删除处理后的列 titanic$Fare <- NULL ``` 3. 数据建模与评估 ```R # 划分训练集和测试集 set.seed(123) train_index <- sample(1:nrow(titanic), 0.7 * nrow(titanic)) train_data <- titanic[train_index, ] test_data <- titanic[-train_index, ] # 使用随机森林进行建模和预测 library(randomForest) model <- randomForest(as.factor(Survived) ~ ., data = train_data) pred <- predict(model, newdata = test_data) # 计算模型评估指标 library(caret) confusionMatrix(pred, as.factor(test_data$Survived)) ``` 4. 模型融合与评估 ```R # 使用GBM进行模型融合 library(gbm) gbm_model <- gbm(as.factor(Survived) ~ ., data = train_data, n.trees = 500, interaction.depth = 3, shrinkage = 0.01, verbose = FALSE) # 使用融合模型进行预测 gbm_pred <- predict.gbm(gbm_model, newdata = test_data, n.trees = 500) # 对两个模型进行加权融合 final_pred <- (pred + gbm_pred) / 2 # 计算融合模型的评估指标 confusionMatrix(as.factor(ifelse(final_pred > 0.5, 1, 0)), as.factor(test_data$Survived)) ``` 5. 数据预测 ```R # 对测试集进行预测,并保存结果为csv文件 test_data$FareLevel <- cut(test_data$Fare, breaks = c(0, 10, 50, 100, Inf), labels = c(1, 2, 3, 4)) test_data$Fare <- NULL test_data$Sex <- as.integer(test_data$Sex == "female") test_data$Embarked[test_data$Embarked == "S"] <- 1 test_data$Embarked[test_data$Embarked == "C"] <- 2 test_data$Embarked[test_data$Embarked == "Q"] <- 3 test_data$Embarked[is.na(test_data$Embarked)] <- 0 test_pred <- predict(model, newdata = test_data) test_output <- data.frame(PassengerId = (892:1309), Survived = test_pred) write.csv(test_output, file = "titanic_pred.csv", row.names = FALSE) ``` 以上就是一个大致的R语言代码流程,具体实现可能需要根据数据的具体情况进行调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值