import numpy as np
import matplotlib.pyplot as plt from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier,DecisionTreeRegressor def creat_data(n):
np.random.seed(0)
X = 5 * np.random.rand(n, 1)
y = np.sin(X).ravel()
noise_num=(int)(n/5)
# 每第5个样本,就在该样本的值上添加噪音
y[::5] += 3 * (0.5 - np.random.rand(noise_num))
return train_test_split(X, y,test_size=0.25,random_state=1) #决策树DecisionTreeRegressor模型
def test_DecisionTreeRegressor(*data):
X_train,X_test,y_train,y_test=data
regr = DecisionTreeRegressor()
regr.fit(X_train, y_train)
print("Training score:%f"%(regr.score(X_train,y_train)))
print("Testing score:%f"%(regr.score(X_test,y_test)))
#绘图
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
X = np.arange(0.0, 5.0, 0.01)[:, np.newaxis]
Y = regr.predict(X)
ax.scatter(X_train, y_train, label="train sample",c='g')
ax.scatter(X_test, y_test, label="test sample",c='r')
ax.plot(X, Y, label="predict_value", linewidth=2,alpha=0.5)
ax.set_xlabel("data")
ax.set_ylabel("target")
ax.set_title("Decision Tree Regression")
ax.legend(framealpha=0.5)
plt.show() # 产生用于回归问题的数据集
X_train,X_test,y_train,y_test=creat_data(100)
# 调用 test_DecisionTreeRegressor
test_DecisionTreeRegressor(X_train,X_test,y_train,y_test)

def test_DecisionTreeRegressor_splitter(*data):
'''
测试 DecisionTreeRegressor 预测性能随划分类型的影响
'''
X_train,X_test,y_train,y_test=data
splitters=['best','random']
for splitter in splitters:
regr = DecisionTreeRegressor(splitter=splitter)
regr.fit(X_train, y_train)
print("Splitter %s"%splitter)
print("Training score:%f"%(regr.score(X_train,y_train)))
print("Testing score:%f"%(regr.score(X_test,y_test))) # 调用 test_DecisionTreeRegressor_splitter
test_DecisionTreeRegressor_splitter(X_train,X_test,y_train,y_test)

def test_DecisionTreeRegressor_depth(*data,maxdepth):
'''
测试 DecisionTreeRegressor 预测性能随 max_depth 的影响
'''
X_train,X_test,y_train,y_test=data
depths=np.arange(1,maxdepth)
training_scores=[]
testing_scores=[]
for depth in depths:
regr = DecisionTreeRegressor(max_depth=depth)
regr.fit(X_train, y_train)
training_scores.append(regr.score(X_train,y_train))
testing_scores.append(regr.score(X_test,y_test))
# 绘图
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(depths,training_scores,label="traing score")
ax.plot(depths,testing_scores,label="testing score")
ax.set_xlabel("maxdepth")
ax.set_ylabel("score")
ax.set_title("Decision Tree Regression")
ax.legend(framealpha=0.5)
plt.show() # 调用 test_DecisionTreeRegressor_depth
test_DecisionTreeRegressor_depth(X_train,X_test,y_train,y_test,maxdepth=20)

吴裕雄 python 机器学习——回归决策树模型的更多相关文章

  1. 吴裕雄 python 机器学习——分类决策树模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_s ...

  2. 吴裕雄 python 机器学习——核化PCAKernelPCA模型

    # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt from sklearn import datas ...

  3. 吴裕雄 python 机器学习——KNN回归KNeighborsRegressor模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets from skle ...

  4. 吴裕雄 python 机器学习——集成学习梯度提升决策树GradientBoostingRegressor回归模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklear ...

  5. 吴裕雄 python 机器学习——集成学习随机森林RandomForestRegressor回归模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklear ...

  6. 吴裕雄 python 机器学习——集成学习AdaBoost算法回归模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklear ...

  7. 吴裕雄 python 机器学习——模型选择回归问题性能度量

    from sklearn.metrics import mean_absolute_error,mean_squared_error #模型选择回归问题性能度量mean_absolute_error模 ...

  8. 吴裕雄 python 机器学习——线性回归模型

    import numpy as np from sklearn import datasets,linear_model from sklearn.model_selection import tra ...

  9. 吴裕雄 python 机器学习——支持向量机非线性回归SVR模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

随机推荐

  1. delphi 多线程之System.TMonitor

    三天不写代码就手生! 把测试代码记录下来. unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, S ...

  2. <ROS> NodeHandle句柄

    作者:jack_20 原文:https://blog.csdn.net/jack_20/article/details/70746736 1.句柄可以让你通过构造函数指定命名空间 ros::NodeH ...

  3. APS技术中的多目标规划问题

    在进行APS(高级计划与排程)系统开发时,绝大多数情况下是需要考虑多目标的.但面对多目标问题进行规划求解时,我们往往极容易因处理方法不当,而影响输出结果,令结果与用户期望产生较大差别.事实上很多时候用 ...

  4. JAVA的DES加密解密在windows上测试一切正常,在linux上异常

    windows上加解密正常,linux上加密正常,解密时发生 如下异常,异常信息如下: [ERROR] 2018-10-15 09:30:35,998 method:com.iscas.ippc.co ...

  5. Centos7下GlusterFS分布式存储集群环境部署记录

    0)环境准备 GlusterFS至少需要两台服务器搭建,服务器配置最好相同,每个服务器两块磁盘,一块是用于安装系统,一块是用于GlusterFS. 192.168.10.239 GlusterFS-m ...

  6. centos7 系统优化脚本

    脚本一: #!/usr/bin/env bash #设置环境变量 export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sb ...

  7. InetSim配置使用

    参考网址: http://techanarchy.net/2013/08/installing-and-configuring-inetsim/ https://blog.csdn.net/isins ...

  8. MySQL5.7.17解压版安装

    首先将mysql解压,公司的mysql解压后自带my.ini文件,结构如下: 在my.ini文件中配置的data路径在my文件夹下,需要删掉,然后修改my.ini文件中basedir和datadir路 ...

  9. (转)SQLServer_十步优化SQL Server中的数据访问 三

    原文地址:http://tech.it168.com/a2009/1125/814/000000814758_all.shtml 第六步:应用高级索引 实施计算列并在这些列上创建索引 你可能曾经写过从 ...

  10. html5 video标签屏蔽右键视频另存为的js代码-HTML5教程

    点评:html5 video标签本身有下载功能但是在video区域内,点击右键可以将“视频另存为”下面是屏蔽右键视频另存为的js代码,有此需求的朋友不要错过   做HTML5的video标签,本身我们 ...