泰坦尼克号生存者数据的预测练习
使用决策回归树建立模型
代码中有三次关于模型的生成,意在不断优化参数来提高预测值
第三次模型的建立使用网格搜索对超参数进行最优值选取,时间会较长,且效果不一定有第二次模型建立的好,仅供参考。
数据集
链接:百度网盘 请输入提取码
提取码: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