泰坦尼克号生存者数据的预测

泰坦尼克号生存者数据的预测练习

使用决策回归树建立模型
代码中有三次关于模型的生成,意在不断优化参数来提高预测值

第三次模型的建立使用网格搜索对超参数进行最优值选取,时间会较长,且效果不一定有第二次模型建立的好,仅供参考。

数据集

链接:百度网盘 请输入提取码

提取码:6223

python 复制代码
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
from sklearn.model_selection import GridSearchCV, cross_val_score
from sklearn.model_selection import train_test_split
import numpy as np

data = pd.read_csv("./data/mytrain.csv")
#print(data)
# 查看表头
print(data.info())
# 显示前5行数据 查看表的结构
#print(data.head(5))
print(data.head())

# 筛选特征 inplace=True表示将修改后的表覆盖原表,默认false不覆盖
# axis=1删除列
# data.drop(['Cabin','Name','Ticket'], inplace=True, axis=1)
# 同下
data = data.drop(['Cabin','Name','Ticket', 'Unnamed: 12'], inplace=False, axis=1)
# print(data2)

# data2.to_csv("./data/train.csv", header=True)

# 处理缺省值 用平均去填补
data['Age'] = data['Age'].fillna(data['Age'].mean())

# 删除有缺省值的行,括号默认为1
data = data.dropna()
# data = data.dropna(1)

# 转换多分类
labels = data["Embarked"].unique().tolist()
data["Embarked"] = data['Embarked'].apply(lambda x: labels.index(x))
print("-------------")
print(data['Embarked'])
print("-------------")
# 转换二分类
data['Sex'] = (data['Sex'] == 'male').astype("int")
# data.loc[:,'Sex'] = (data['Sex'] == 'male').astype("int")
# data.iloc[:,3]
print(data.iloc[:, 3])
print(data.head())

x = data.iloc[:, data.columns != 'Survived']
print(data.columns != 'Survived')
y = data.iloc[:, data.columns == 'Survived']
# print(y)

Xtrain, Xtest, Ytrain, Ytest = train_test_split(x, y, test_size=0.3)
# print(Xtrain)

# 将乱序的索引表恢复
# 纠正索引避免混乱
# Xtrain.index = range(Xtrain.shape[0])
# print(Xtrain)
for i in [Xtrain, Xtest, Ytrain, Ytest]:
    i.index = range(i.shape[0])

# 建立模型1
# clf = DecisionTreeClassifier(random_state=25)
# clf = clf.fit(Xtrain, Ytrain)
# score = clf.score(Xtest, Ytest)
# print(score)  # 0.749
#
# # 使用交叉验证
# score = cross_val_score(clf, x, y, cv=10).mean()
# print(score)  # 0.746

# 建立模型2
# 上面模型不好,需要调参
# tr = []
# te = []
# for i in range(10):
#     clf = DecisionTreeClassifier(random_state=25
#                                  , max_depth=i+1
#                                  , criterion='entropy'
#                                  )
#     clf = clf.fit(Xtrain, Ytrain)
#     score_tr = clf.score(Xtrain, Ytrain)
#     score_te = cross_val_score(clf, x, y, cv=10).mean()
#     tr.append(score_tr)
#     te.append(score_te)
# print(max(te))  # 0.8166624106230849
# plt.plot(range(1, 11), tr, color='red', label='train')
# plt.plot(range(1, 11), te, color='blue', label='test')
# plt.xticks(range(1, 11))
# plt.legend()
# plt.show()

# 建立模型3
# 使用网格搜索调整多个超参数,枚举技术,计算量大

# 导入有顺序的随机50个0到0.5的数字
gini_thresholds = np.linspace(0, 0.5, 50)
# entropy_thresholds = np.linspace(0, 1, 50)
# print(x)
# arange导入的不是随机的,按照步长的大小设定
# np.arange(0, 0.5, 0.01)

# 希望的网格搜索的参数和参数的取值范围
parameters = {"criterion":("gini", "entropy")
              , "splitter":("best", "random")
              , "max_depth":[*range(1, 10)]
              , "min_samples_leaf":[*range(1, 50, 5)]
              , "min_impurity_decrease":[*np.linspace(0, 0.5, 20)]
              }
clf = DecisionTreeClassifier(random_state=25)
GS = GridSearchCV(clf, parameters, cv=10)
GS = GS.fit(Xtrain, Ytrain)
print(GS.best_params_)
print(GS.best_score_)
# {'criterion': 'entropy', 'max_depth': 6, 'min_impurity_decrease': 0.0, 'min_samples_leaf': 1, 'splitter': 'random'}
# 0.839452124935996
相关推荐
SNWCC20 小时前
autodl_M000_pytorch
人工智能·pytorch·python
Swift社区20 小时前
ArkUI 如何设计 AI 原生 UI?
人工智能·ui
Lw中20 小时前
RAG用户查询太模糊?
人工智能·大模型应用开发·rag查询
deephub21 小时前
多智能体系统的三种编排模式:Supervisor、Pipeline 与 Swarm
人工智能·python·大语言模型·agent
Lw中21 小时前
提示词效果不稳定?
人工智能·rag·大模型应用基础
globaldomain21 小时前
安全研究发现OpenClaw AI代理“极易受劫持”
人工智能·安全·openclaw·龙虾
墨102421 小时前
与 AI 并肩成长:从个人知识库到每日新闻系统的实践记录
人工智能·ai·ai编程·openclaw
m0_7381207221 小时前
渗透测试——pyexpvm靶机详细提权过程(MSF框架,Hydra数据库爆破,SUDO提权)
服务器·网络·数据库·python·sql·web安全
翱翔的苍鹰21 小时前
LangChain是一个主流的大语言模型(LLM)应用开发框架,核心功能是连接大模型与外部资源/工具。
网络·人工智能·python·深度学习·语言模型
坚持学习前端日记21 小时前
AI 产品开发经验
前端·javascript·人工智能·visual studio