zoukankan      html  css  js  c++  java
  • day06-决策树及随机森林

    
    
    # coding=utf-8
    from sklearn.feature_extraction import DictVectorizer
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.datasets import load_iris
    from sklearn.model_selection import train_test_split
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.model_selection import GridSearchCV
    
    def dectree():
        """
        决策树
        :return:
        """
    
        # 水仙花数
        iris = load_iris()
    
        # 分割数据
        x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,test_size=0.25)
    
        # 决策树
        dec = DecisionTreeClassifier()
    
        dec.fit(x_train,y_train)
    
        print(x_train)
    
        print("决策树预测的准确率为:",dec.score(x_test,y_test))
    
    
    
        # 随机森林
        rf = RandomForestClassifier()
    
        # 网格验证找出最好的模型
        gc = GridSearchCV(rf,param_grid={"n_estimators":[1,3,5,7,9],"max_depth":[3,5,7,9,15]},cv=2)
    
        # 训练
        gc.fit(x_train,y_train)
    
        # 结果
        print("经过网格验证选择后的随机森林预测准确率为:",gc.score(x_test,y_test))
        print("最佳的随机森林参数为:",gc.best_params_)
    
        return None
    
    if __name__ == '__main__':
        dectree()
    
    
    

    结果为:

    
    决策树预测的准确率为: 0.9473684210526315
    经过网格验证选择后的随机森林预测准确率为: 0.9736842105263158
    最佳的随机森林参数为: {'max_depth': 3, 'n_estimators': 3}
    
    
    

    决策树中使用算法来计算各个特征值的信息熵,判断哪个特征值对结果的影响最大,将这个特征值移到树节点的前边,然后依次将特征值作为节点组成决策树。
    而随机森林则是多个决策树,每个决策树有不同的结果,哪个结果多则预测的结果为那个。
    随机森林优点比较明显,可以用于大数据量的预测,预测准确率高,且不需要进行数据降维,唯一不好的地方则是参数不好调,即树的数量以及树的深度。

  • 相关阅读:
    第四季-专题20-SPI驱动程序设计
    第四季-专题21-USB驱动程序设计
    第四季-专题19-I2C驱动程序设计
    第四季-专题17-LCD驱动程序设计
    第四季-专题18-FLASH驱动程序设计
    第四季-专题16-触摸屏驱动程序设计
    第四季-专题15-网卡驱动程序设计
    第四季-专题14-串口驱动程序设计
    python提示AttributeError: 'NoneType' object has no attribute 'append'
    python的正则表达式
  • 原文地址:https://www.cnblogs.com/wuren-best/p/14278845.html
Copyright © 2011-2022 走看看