机器学习之使用sklearn构造决策树模型

一、任务基础

导入所需要的库

import matplotlib.pyplot as plt
import pandas as pd

%matplotlib inline

加载sklearn内置数据集 ,查看数据描述

from sklearn.datasets.california_housing import fetch_california_housing
housing = fetch_california_housing()
print(housing.DESCR)

 数据集包含房价价格以及影响房价的一些因素

.. _california_housing_dataset:

California Housing dataset
--------------------------

**Data Set Characteristics:**

    :Number of Instances: 20640

    :Number of Attributes: 8 numeric, predictive attributes and the target

    :Attribute Information:
        - MedInc        median income in block
        - HouseAge      median house age in block
        - AveRooms      average number of rooms
        - AveBedrms     average number of bedrooms
        - Population    block population
        - AveOccup      average house occupancy
        - Latitude      house block latitude
        - Longitude     house block longitude

    :Missing Attribute Values: None

This dataset was obtained from the StatLib repository.
http://lib.stat.cmu.edu/datasets/

The target variable is the median house value for California districts.

This dataset was derived from the 1990 U.S. census, using one row per census
block group. A block group is the smallest geographical unit for which the U.S.
Census Bureau publishes sample data (a block group typically has a population
of 600 to 3,000 people).

It can be downloaded/loaded using the
:func:`sklearn.datasets.fetch_california_housing` function.

.. topic:: References

    - Pace, R. Kelley and Ronald Barry, Sparse Spatial Autoregressions,
      Statistics and Probability Letters, 33 (1997) 291-297

查看数据集维度

housing.data.shape  
(20640, 8)

查看第一条数据

housing.data[0]
array([   8.3252    ,   41.        ,    6.98412698,    1.02380952,
        322.        ,    2.55555556,   37.88      , -122.23      ])

二、构造决策树模型

决策树模型参数:

(1)criterion gini or entropy   基尼系数或者熵
(2)splitter best or random 前者是在所有特征中找最好的切分点 后者是在部分特征中(数据量大的时候)
(3)max_features: None(所有),log2,sqrt,N。特征小于50的时候一般使用所有的特征
(4)max_depth 数据少或者特征少的时候可以不管这个值,如果模型样本量多,特征也多的情况下,可以尝试限制下这个决策树的深度。可以尝试遍历max_depth找出最佳。(最常用参数之一)
(5)min_samples_split 如果某节点的样本数少于min_samples_split,则不会继续再尝试选择最优特征来进行划分如果样本量不大,不需要管这个值。如果样本量数量级非常大,则推荐增大这个值。(最常用参数之一)
(6)min_samples_leaf 这个值限制了叶子节点最少的样本数,如果某叶子节点数目小于样本数,则会和兄弟节点一起被剪枝,如果样本量不大,不需要管这个值,大些如10W可是尝试下
(7)min_weight_fraction_leaf 这个值限制了叶子节点所有样本权重和的最小值,如果小于这个值,则会和兄弟节点一起被剪枝默认是0,就是不考虑权重问题。一般来说,如果我们有较多样本有缺失值,或者分类树样本的分布类别偏差很大,就会引入样本权重,这时我们就要注意这个值了。
(8)max_leaf_nodes 通过限制最大叶子节点数,可以防止过拟合,默认是"None”,即不限制最大的叶子节点数。如果加了限制,算法会建立在最大叶子节点数内最优的决策树。如果特征不多,可以不考虑这个值,但是如果特征分成多的话,可以加以限制具体的值可以通过交叉验证得到。
(9)class_weight 指定样本各类别的的权重,主要是为了防止训练集某些类别的样本过多导致训练的决策树过于偏向这些类别。这里可以自己指定各个样本的权重如果使用“balanced”,则算法会自己计算权重,样本量少的类别所对应的样本权重会高。
(10)min_impurity_split 这个值限制了决策树的增长,如果某节点的不纯度(基尼系数,信息增益,均方差,绝对差)小于这个阈值则该节点不再生成子节点。即为叶子节点 。
(11)n_estimators:要建立树的个数

这些参数都是用来剪枝决策树,防止决策树太过庞大或者出现过拟合的现象

这里只选择了Longitude(经度)和Latitude(纬度)两个特征来构造决策树模型

from sklearn import tree # 导入指定模块
dtr = tree.DecisionTreeRegressor(max_depth=2) # 决策分类
dtr.fit(housing.data[:, [6, 7]], housing.target) # x,y值

 可以看出有些参数只需要保持默认即可

DecisionTreeRegressor(criterion='mse', max_depth=2, max_features=None,
                      max_leaf_nodes=None, min_impurity_decrease=0.0,
                      min_impurity_split=None, min_samples_leaf=1,
                      min_samples_split=2, min_weight_fraction_leaf=0.0,
                      presort=False, random_state=None, splitter='best')

决策树的好处之一就在于通过可视化显示可以直观的看到构造出来的决策树模型

# 要可视化显示,首先需要安装 graphviz   
# https://graphviz.gitlab.io/_pages/Download/Download_windows.html 下载可视化软件
# pip install graphviz

# 设置临时环境遍历
import os
os.environ["PATH"] += os.pathsep + 'D:/program files (x86)/Graphviz2.38/bin/'  #注意修改你的路径

dot_data = tree.export_graphviz(dtr, # 注意这个参数为决策树对象名称
                                out_file=None,
                                feature_names=housing.feature_names[6:8], # 还需要指定特征名
                                filled=True,
                                impurity=False,
                                rounded=True)

显示决策树模型

# pip install pydotplus
import pydotplus

graph = pydotplus.graph_from_dot_data(dot_data)
graph.get_nodes()[7].set_fillcolor("#FFF2DD")
from IPython.display import Image
Image(graph.create_png())

  

将数据集划分为训练集测试集

from sklearn.model_selection import train_test_split
data_train, data_test, target_train, target_test = train_test_split(
    housing.data, housing.target, test_size=0.1, random_state=42)
dtr = tree.DecisionTreeRegressor(random_state=42)
dtr.fit(data_train, target_train)

dtr.score(data_test, target_test)

得到精度值

0.637355881715626

可以看到,精度值不太高的样子。

三、随机森林模型

导入集成算法里面的随机森林模型库,在这个实例里面只是简单的使用下随机森林模型。

from sklearn.ensemble import RandomForestRegressor # Regressor 回归
# random_state就是为了保证程序每次运行都分割一样的训练集和测试集
rfr = RandomForestRegressor( random_state = 42) 
rfr.fit(data_train, target_train)
rfr.score(data_test, target_test)

可以看到随机森林模型精度要好点 

0.7910601348350835

GridSearchCV,它存在的意义就是自动调参,只要把参数输进去,就能给出最优化的结果和参数。但是这个方法适合于小数据集,一旦数据的量级上去了,很难得出结果。相当于循环遍历给出的所有的参数来得到最优的结果,十分的耗时。

# from sklearn.grid_search import GridSearchCV
# sklearn.grid_search模块在0.18版本中被弃用,它所支持的类转移到model_selection模块中。还要注意,
# 新的CV迭代器的接口与这个模块的接口不同。sklearn.grid_search将在0.20中被删除。
from sklearn.model_selection import GridSearchCV
tree_param_grid = {'min_samples_split':list((3,6,9)),'n_estimators':list((10,50,100))}

# cv 交叉验证(Cross-validation)的简写  代表进行几次交叉验证
grid = GridSearchCV(RandomForestRegressor(),param_grid=tree_param_grid,cv=5)
grid.fit(data_train,target_train)
# grid_scores_在sklearn0.20版本中已被删除,取而代之的是cv_results_。
grid.cv_results_, grid.best_params_, grid.best_score_

可以看到最优化的参数值为'min_samples_split': 3, 'n_estimators': 100,得到精度为0.80多一点(在输出结果最后一行)

({'mean_fit_time': array([0.91196742, 4.46895003, 8.89996696, 0.90845881, 4.01207662,
         9.11067271, 0.84911356, 4.16957936, 8.08404155]),
  'std_fit_time': array([0.04628971, 0.19323399, 0.36771072, 0.07048984, 0.05280237,
         0.55379083, 0.0599862 , 0.19719896, 0.34949627]),
  'mean_score_time': array([0.00918159, 0.0467237 , 0.08795581, 0.00958099, 0.03958073,
         0.08624392, 0.01018567, 0.03616033, 0.06846623]),
  'std_score_time': array([0.00367907, 0.00559777, 0.00399863, 0.00047935, 0.00082726,
         0.0135891 , 0.0003934 , 0.0052837 , 0.00697507]),
  'param_min_samples_split': masked_array(data=[3, 3, 3, 6, 6, 6, 9, 9, 9],
               mask=[False, False, False, False, False, False, False, False,
                     False],
         fill_value='?',
              dtype=object),
  'param_n_estimators': masked_array(data=[10, 50, 100, 10, 50, 100, 10, 50, 100],
               mask=[False, False, False, False, False, False, False, False,
                     False],
         fill_value='?',
              dtype=object),
  'params': [{'min_samples_split': 3, 'n_estimators': 10},
   {'min_samples_split': 3, 'n_estimators': 50},
   {'min_samples_split': 3, 'n_estimators': 100},
   {'min_samples_split': 6, 'n_estimators': 10},
   {'min_samples_split': 6, 'n_estimators': 50},
   {'min_samples_split': 6, 'n_estimators': 100},
   {'min_samples_split': 9, 'n_estimators': 10},
   {'min_samples_split': 9, 'n_estimators': 50},
   {'min_samples_split': 9, 'n_estimators': 100}],
  'split0_test_score': array([0.79254741, 0.80793267, 0.81163631, 0.78859073, 0.81211894,
         0.81222231, 0.79241065, 0.80784586, 0.80958409]),
  'split1_test_score': array([0.77856084, 0.80047265, 0.80266101, 0.78474831, 0.79898533,
         0.80203702, 0.77912397, 0.79714354, 0.80029259]),
  'split2_test_score': array([0.78105784, 0.80063538, 0.8052804 , 0.78584898, 0.8036029 ,
         0.80240046, 0.78148243, 0.79955117, 0.80072995]),
  'split3_test_score': array([0.79582001, 0.80687008, 0.8100583 , 0.79947207, 0.80958334,
         0.80851996, 0.78633104, 0.80797192, 0.81129754]),
  'split4_test_score': array([0.79103059, 0.8071791 , 0.81016989, 0.78011578, 0.80719335,
         0.81117408, 0.79282783, 0.8064226 , 0.8085679 ]),
  'mean_test_score': array([0.78780359, 0.80461815, 0.80796138, 0.78775522, 0.80629709,
         0.80727103, 0.7864355 , 0.80378723, 0.8060946 ]),
  'std_test_score': array([0.00675437, 0.00333656, 0.00340769, 0.00646542, 0.00460913,
         0.00429948, 0.00556   , 0.00453896, 0.00464337]),
  'rank_test_score': array([7, 5, 1, 8, 3, 2, 9, 6, 4])},
 {'min_samples_split': 3, 'n_estimators': 100},
 0.8079613788142571)

设置上面得到的最优化的参数,构造随机森林模型

rfr = RandomForestRegressor( min_samples_split=3,n_estimators = 100,random_state = 42)
rfr.fit(data_train, target_train)
rfr.score(data_test, target_test)

可以看出精度相比较上面有了提升。 

0.8088623476993486

四、总结

通过这次案例学习了决策树模型里面的一些参数对于决策树的意义,以及怎么样使用可视化库来比较方便的展示构造出来的决策树模型,最后学习到了如何使用模型调参利器GridSearchCV来得到一般机器学习模型里面最优的参数,达到提升模型精度的目的。 

  

转载于:https://www.cnblogs.com/xiaoyh/p/11266777.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,那我可以为您提供一些使用Python、Tensorflow、sklearn、numpy、pytorch和matplotlib等工具库进行机器学习项目开发时所需的算法实现示例和建议。 1. 最小二乘法:最小二乘法是一种常见的线性回归算法,可以用来拟合数据集并预测新的数据点。在Python中,可以使用NumPy库实现最小二乘法。例如: ```python import numpy as np # 构造数据集 x = np.arange(0, 10, 0.1) y = 2 * x + 1 + np.random.randn(len(x)) # 最小二乘法拟合 A = np.vstack([x, np.ones(len(x))]).T w = np.linalg.lstsq(A, y, rcond=None)[0] # 输出拟合结果 print('斜率:', w[0]) print('截距:', w[1]) ``` 2. 梯度下降法:梯度下降法是一种常见的优化算法,可以用于训练神经网络等模型的参数。在Python中,可以使用TensorFlow库实现梯度下降法。例如: ```python import tensorflow as tf import numpy as np # 构造数据集 x = np.random.randn(100) y = 3 * x + 2 + np.random.randn(100) * 0.5 # 定义模型参数 w = tf.Variable(0.0) b = tf.Variable(0.0) # 定义损失函数和优化器 loss = tf.reduce_mean(tf.square(y - w * x - b)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss) # 训练模型 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(100): sess.run(optimizer) if i % 10 == 0: print('第 %d 次训练,损失值为:%.4f,斜率为:%.4f,截距为:%.4f' % (i, sess.run(loss), sess.run(w), sess.run(b))) ``` 3. 分类:分类是机器学习中的一个重要任务,可以用于识别图像、语音等数据。在Python中,可以使用Scikit-learn库实现分类算法。例如: ```python from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score # 加载数据集 iris = load_iris() # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=0) # 定义模型并训练 clf = LogisticRegression() clf.fit(X_train, y_train) # 预测并评估模型 y_pred = clf.predict(X_test) print('准确率为:%.4f' % accuracy_score(y_test, y_pred)) ``` 4. 聚类:聚类是一种无监督学习算法,可以将数据集中的样本分为若干个类别。在Python中,可以使用Scikit-learn库实现聚类算法。例如: ```python from sklearn.datasets import make_blobs from sklearn.cluster import KMeans import matplotlib.pyplot as plt # 生成数据集 X, y = make_blobs(n_samples=100, centers=3, random_state=0) # 定义模型并训练 clf = KMeans(n_clusters=3) clf.fit(X) # 可视化结果 plt.scatter(X[:, 0], X[:, 1], c=clf.labels_) plt.scatter(clf.cluster_centers_[:, 0], clf.cluster_centers_[:, 1], marker='x', color='r') plt.show() ``` 5. 支持向量机:支持向量机是一种常见的分类算法,可以用于处理线性和非线性分类问题。在Python中,可以使用Scikit-learn库实现支持向量机算法。例如: ```python from sklearn.datasets import make_classification from sklearn.svm import SVC from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score # 生成样本数据 X, y = make_classification(n_samples=100, n_features=2, n_informative=2, n_redundant=0, random_state=0) # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) # 定义模型并训练 clf = SVC(kernel='rbf') clf.fit(X_train, y_train) # 预测并评估模型 y_pred = clf.predict(X_test) print('准确率为:%.4f' % accuracy_score(y_test, y_pred)) ``` 6. 决策树决策树是一种常见的分类和回归算法,可以用于处理离散型和连续型的数据。在Python中,可以使用Scikit-learn库实现决策树算法。例如: ```python from sklearn.datasets import load_iris from sklearn.tree import DecisionTreeClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score # 加载数据集 iris = load_iris() # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=0) # 定义模型并训练 clf = DecisionTreeClassifier() clf.fit(X_train, y_train) # 预测并评估模型 y_pred = clf.predict(X_test) print('准确率为:%.4f' % accuracy_score(y_test, y_pred)) ``` 7. 神经网络:神经网络是一种常见的深度学习算法,可以用于处理图像、语音等复杂的数据。在Python中,可以使用TensorFlow或PyTorch库实现神经网络算法。例如: ```python import tensorflow as tf import numpy as np # 构造数据集 X = np.random.randn(100, 10) y = np.random.randint(0, 2, size=(100, 1)) # 定义模型参数 inputs = tf.keras.layers.Input(shape=(10,)) x = tf.keras.layers.Dense(64, activation='relu')(inputs) x = tf.keras.layers.Dense(32, activation='relu')(x) outputs = tf.keras.layers.Dense(1, activation='sigmoid')(x) # 定义模型并训练 model = tf.keras.models.Model(inputs=inputs, outputs=outputs) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) model.fit(X, y, epochs=10, batch_size=32) # 预测并评估模型 X_test = np.random.randn(10, 10) y_pred = model.predict(X_test) print(y_pred) ``` 以上是一些使用Python、Tensorflow、sklearn、numpy、pytorch和matplotlib等工具库进行机器学习项目开发时所需的算法实现示例和建议,希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值