sklearn中的决策树-分类树:泰坦尼克号生存预测

分类树实例:泰坦尼克号生存预测

代码分解
  • 需要导入的库

    python 复制代码
    """导入所需要的库"""
    import pandas as pd
    import numpy as np
    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
  • 导入数据集

    python 复制代码
    """导入数据集,探索数据"""
    data_train = pd.read_csv('./need/Taitanic_data/data.csv',index_col=0)
    data_test = pd.read_csv('./need/Taitanic_data/test.csv',index_col=0)
    
    data = pd.concat([data_train,data_test],axis=0)
    data.head()
    data.info()

    ​

  • 对数据集进行预处理

    python 复制代码
    """对数据集进行预处理"""
    #删除缺失值过多的列,和观察判断来说和预测的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()
  • 提取标签和特征矩阵,分测试集和训练集

    python 复制代码
    """提取标签和特征矩阵,分测试集和训练集"""
    X = data.iloc[:,data.columns != "Survived"]
    y = data.iloc[:,data.columns == "Survived"]
    Xtrain, Xtest, Ytrain, Ytest = train_test_split(X,y,test_size=0.3)
    #修正测试集和训练集的索引(或者直接reset_index(drop=True,inplace=True))
    for i in [Xtrain, Xtest, Ytrain, Ytest]:
        i.index = range(i.shape[0])
    #查看分好的训练集和测试集 
    Xtrain.head()
  • 导入模型,粗略跑一下查看结果

    python 复制代码
    """导入模型,粗略跑一下查看结果"""
    clf = DecisionTreeClassifier(random_state=25)
    clf = clf.fit(Xtrain, Ytrain)
    score_ = clf.score(Xtest, Ytest)
    print('单颗决策树精度',score_)
    score = cross_val_score(clf,X,y,cv=10).mean()
    print('10次交叉验证平均精度',score)
    
    """输出"""
    单颗决策树精度 0.8164794007490637
    10次交叉验证平均精度 0.7739274770173645
  • 在不同max_depth下观察模型的拟合状况

    python 复制代码
    """在不同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.figure(figsize=(12,8))
    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()
    #这里为什么使用"entropy"?因为我们注意到,在最大深度=3的时候,模型拟合不足,在训练集和测试集上的表现接 近,但却都不是非常理想,只能够达到83%左右,所以我们要使用entropy。
    
    
    """输出"""
    测试集交叉验证均值最大值(精度) 0.8177860061287026

    ​

  • 用网格搜索调整参数

    python 复制代码
    """用网格搜索调整参数"""
    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)
    print('最佳参数',GS.best_params_)
    print('最佳精度',GS.best_score_)
    
    
    """输出"""
    最佳参数 {'criterion': 'entropy'
              , 'max_depth': 9
              , 'min_impurity_decrease': 0.0
              , 'min_samples_leaf': 6
              , 'splitter': 'best'
             }
    最佳精度 0.815284178187404

    由此可见,网格搜索并非一定比自己调参好,因为网格搜索无法舍弃无用的参数,默认传入的所有参数必须得都选上。

所有代码
  • 所有代码

    python 复制代码
    """导入所需要的库"""
    import pandas as pd
    import numpy as np
    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_train = pd.read_csv('./need/Taitanic_data/data.csv',index_col=0)
    data_test = pd.read_csv('./need/Taitanic_data/test.csv',index_col=0)
    
    data = pd.concat([data_train,data_test],axis=0)
    data.head()
    data.info()
    
    
    """对数据集进行预处理"""
    #删除缺失值过多的列,和观察判断来说和预测的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 = data.iloc[:,data.columns != "Survived"]
    y = data.iloc[:,data.columns == "Survived"]
    Xtrain, Xtest, Ytrain, Ytest = train_test_split(X,y,test_size=0.3)
    #修正测试集和训练集的索引(或者直接reset_index(drop=True,inplace=True))
    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)
    print('单颗决策树精度',score_)
    score = cross_val_score(clf,X,y,cv=10).mean()
    print('10次交叉验证平均精度',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.figure(figsize=(12,8))
    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()
    #这里为什么使用"entropy"?因为我们注意到,在最大深度=3的时候,模型拟合不足,在训练集和测试集上的表现接 近,但却都不是非常理想,只能够达到83%左右,所以我们要使用entropy。
    """用网格搜索调整参数"""
    # gini系数最大为0.5最小为0、信息增益最大为1,最小为0
    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)
    print('最佳参数',GS.best_params_)
    print('最佳精度',GS.best_score_)
相关推荐
电科_银尘9 天前
【大模型算法与工程 05】分类、回归与排序指标:怎样评价一个模型
算法·分类·回归
小宋102110 天前
AI 爬虫把大学图书馆站点拖垮:按行为指纹给爬虫分类、按类发配额、用边缘缓存分层,以带宽成本结构和真实用户 P95 延迟定档
人工智能·爬虫·分类
BYSJMG13 天前
计算机毕业设计选题推荐:【基于深度学习的车辆损坏严重程度评估方法研究】ResNet50 图像分类
人工智能·深度学习·目标检测·机器学习·计算机视觉·分类·课程设计
2601_9679801115 天前
从决策树到随机森林:Python机器学习里的“树形家族“深度实战与原理拆解
决策树·随机森林·机器学习·scikit-learn·集成学习
.道阻且长.15 天前
AI 按数据模态分类:计算机视觉、NLP、语音处理,一篇理清
人工智能·计算机视觉·分类
yunhuibin16 天前
AI分类任务种类
人工智能·自然语言处理·分类
电科_银尘18 天前
【深度学习项目实战·15】小样本图像分类:迁移学习识别蚂蚁与蜜蜂
深度学习·分类·迁移学习
敢敢のwings18 天前
机器人如何通过触觉认识物体:从材质分类到接触状态判断
分类·机器人·材质
Blockbuater_drug19 天前
《经典机器学习实战》第 4 课:逻辑回归:预测类别(南瓜)
分类·逻辑回归·混淆矩阵·roc曲线·auc·召回率·精确率
AI源码19 天前
基于深度学习的日常服饰分类与推荐系统设计
人工智能·深度学习·分类