回归树练习,泰坦尼克号幸存者的预测
数据集下载地址
https://download.csdn.net/download/AnalogElectronic/89846327
包含测试集合代码资源
https://download.csdn.net/download/AnalogElectronic/89892984
我们来看看train.csv文件,它包含了891个样本,每个样本代表一个乘客。这些样本的数据包括乘客的年龄(Age)、船票等级(Pclass)、性别(Sex)、登船港口(Embarked)、票价(Fare)等基本信息,以及最重要的生存状态(Survived)。这些特征提供了对乘客生存可能性的洞察,比如男性与女性的生存率差异、船票等级与生存机会的关系等。
##回归树练习,泰坦尼克号幸存者的预测
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_val_score
import matplotlib.pyplot as plt
data = pd.read_csv(r"I:\hadoop note\titanic_train.csv",index_col= 0)
data.head()
#删除缺失值过多的列,和观察判断来说和预测的y没有关系的列
data.drop(["Cabin","Name","Ticket"],inplace=True,axis=1)
#处理缺失值,对缺失值较多的列进行填补,有一些特征只确实一两个值,可以采取直接删除记录的方法
data["Age"] = data["Age"].fillna(data["Age"].mean())
data = data.dropna()
#将分类变量转换为数值型变量
#将二分类变量转换为数值型变量
#astype能够将一个pandas对象转换为某种类型,和apply(int(x))不同,astype可以将文本类转换为数字,用这个方式可以很便捷地将二分类特征转换为0~1
data["Sex"] = (data["Sex"]== "male").astype("int")
#将三分类变量转换为数值型变量
labels = data["Embarked"].unique().tolist()
data["Embarked"] = data["Embarked"].apply(lambda x: labels.index(x))
#查看处理后的数据集
data.head()
##提取X和Y,拆分训练集和测试集
X = data.iloc[:,data.columns != "Survived"]
y = data.iloc[:,data.columns == "Survived"]
from sklearn.model_selection import train_test_split
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X,y,test_size=0.3)
#修正测试集和训练集的索引
for i in [Xtrain, Xtest, Ytrain, Ytest]:
i.index = range(i.shape[0])
#查看分好的训练集和测试集
Xtrain.head()
clf = DecisionTreeClassifier(random_state=25)
clf = clf.fit(Xtrain, Ytrain)
score_ = clf.score(Xtest, Ytest)
score_
##循环获取适合的max_depth
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))
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()
0.8177860061287026
##交叉验证和网格搜索
import numpy as np
gini_thresholds = np.linspace(0,0.5,20)
parameters = {'splitter':('best','random'),
'criterion':("gini","entropy"),
"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.fit(Xtrain,Ytrain)
GS.best_params_
GS.best_score_
0.819969278033794
采用最佳参数构建模型
clf = DecisionTreeClassifier(random_state=25,max_depth=7 ,criterion="entropy" ,min_samples_leaf=6,splitter="best")
clf = clf.fit(Xtrain, Ytrain)
score_ = clf.score(Xtest, Ytest)
score_
0.8426966292134831
实战模型预测与结果提交
# 实战模型预测与结果提交
data = pd.read_csv('./test.csv')
data.head()
data.drop(["Cabin","Name","Ticket"],inplace=True,axis=1)
#处理缺失值,对缺失值较多的列进行填补,有一些特征只确实一两个值,可以采取直接删除记录的方法
data["Age"] = data["Age"].fillna(data["Age"].mean())
data = data.dropna()
#将分类变量转换为数值型变量
#将二分类变量转换为数值型变量
#astype能够将一个pandas对象转换为某种类型,和apply(int(x))不同,astype可以将文本类转换为数字,用这个方式可以很便捷地将二分类特征转换为0~1
data["Sex"] = (data["Sex"]== "male").astype("int")
#将三分类变量转换为数值型变量
labels = data["Embarked"].unique().tolist()
data["Embarked"] = data["Embarked"].apply(lambda x: labels.index(x))
#查看处理后的数据集
data.head()
df = pd.DataFrame()
df['PassengerId'] = data['PassengerId']
data.drop(["PassengerId"],inplace=True,axis=1)
_y = model.predict(data)
_y = _y.astype(int)
# 导出预测结果
df['Survived'] = _y
df.to_csv('./test_predict.csv', index=False)
模型训练, 采用逻辑回归
#模型训练, 采用逻辑回归
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X,y)
y_ = model.predict(X)
from sklearn.metrics import auc,roc_auc_score,roc_curve,recall_score,accuracy_score,classification_report,confusion_matrix
print('LogisticRegression准确率是:',accuracy_score(y,y_))
cm = confusion_matrix(y,y_) #获得混淆矩阵
recall = cm[1,1]/(cm[1,1]+cm[1,0])
print('LogisticRegression召回率率是:',recall)
proba_ = model.predict_proba(X)[:,1] #表示获取类别1的样本阳性,行用卡盗刷
fpr,tpr,thresholds = roc_curve(y,proba_)
roc_auc = auc(fpr,tpr) #曲线下的面积
import matplotlib.pyplot as plt
plt.title=('Receiver Operating Characteristic')
plt.plot(fpr,tpr,'b',label='AUC = %0.5f'% roc_auc)
plt.legend(loc='lower right')
plt.plot([0,1],[0,1],'r--')
plt.xlim([-0.1,1.0])
plt.ylim([-0.1,1.0])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
混淆矩阵
from sklearn.model_selection import GridSearchCV, train_test_split
#交叉验证, 筛选合适的参数
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.2)
y_pred = model.predict(X_test)
print('准确率: ',accuracy_score(y_test,y_pred))
print(classification_report(y_test,y_pred))
def plot_confusion_matrix(cm,classes,title, cmap=plt.cm.Blues):
plt.imshow(cm,interpolation='nearest', cmap=cmap)
# plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=0)
plt.yticks(tick_marks, classes)
thresh = cm.max()/2.
for i, j in itertools.product(range(cm.shape[0]),range(cm.shape[1])):
plt.text(j, i , cm[i, j],
horizontalalignment="center",
color="white" if cm[i,j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
import itertools
cnf_matrix = confusion_matrix(y_test,y_pred) #获得混淆矩阵
recall1 = cnf_matrix[1,1]/(cnf_matrix[1,1]+cnf_matrix[1,0])
print('LogisticRegression召回率率是:',recall1)
#绘制模型优化后的混淆矩阵
class_names=[0,1]
plt.figure()
plot_confusion_matrix(cnf_matrix
, classes=class_names
, title = 'Confusion matrix'
)