Titanic模型集成

本文通过绘制ROC曲线,比较了随机森林和XGBoost两种算法在预测任务上的性能。实验结果显示,XGBoost在预测准确性上表现更优。

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

1.使用随机森林做预测并绘制ROC曲线

X = train[['Pclass', 'Sex', 'SibSp', 'Parch', 'Cabin', 'Embarked',
       'title', 'isalone', 'Family', 'mother', 'person', 'ticket-same', 'age',
       'fare']]
Y = train['Survived']
#使用随机森林做预测
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
clf = RandomForestClassifier(n_estimators=10, max_depth=None, min_samples_split=2, random_state=0)
clf.fit(X, Y)
scores = cross_val_score(clf, X, Y, cv=5)
print('正确率:', np.mean(scores), scores)
train_x, test_x, train_label, test_label = train_test_split(X, Y, test_size=0.3, random_state= 0)
clf.fit(train_x, train_label)
probas_ = clf.predict_proba(test_x)
fpr, tpr, thresholds = roc_curve(test_label, probas_[:, 1])
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC (area = %0.2f)' % (roc_auc))
#画对角线
plt.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Luck')
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Roc-rf')
plt.legend(loc="lower right")
plt.show()

2.使用xgb做预测并绘制ROC曲线

from xgboost import XGBClassifier
clf = XGBClassifier()
clf.fit(X,Y)
scores = cross_val_score(clf, X, Y, cv=5)
print('正确率:', np.mean(scores), scores)
train_x, test_x, train_label, test_label = train_test_split(X, Y, test_size=0.3, random_state= 0)
clf.fit(train_x, train_label)
probas_ = clf.predict_proba(test_x)
fpr, tpr, thresholds = roc_curve(test_label, probas_[:, 1])
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC (area = %0.2f)' % (roc_auc))
#画对角线
plt.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Luck')
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Roc-xgb')
plt.legend(loc="lower right")
plt.show()

从ROC曲线来看xgb的效果要好一些。

参考资料https://blog.youkuaiyun.com/u010454729/article/details/45098305

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值