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. 北京U3D外包团队 UE4红军抗战案例 Unity3D红军抗战案例 UE4下载和安装虚幻4游戏引擎

    刚完整UE4红军抗战案例 Unity3D红军抗战案例,有在线演示(版权关系不方便发图),有UE4或Unity项目定制外包开发的欢迎联系我们 进入虚幻4的官方主页(https://www.unreale ...

  2. HTML与CSS:结构与表现

    在HTML和CSS里存在着部分重复的功能,例如两者都可以设定一段文字的字体属性.既然如此,为啥还要CSS呢(至少,为啥CSS里存在着和HTML标签属性重复的东西呢)? 这是因为,HTML和CSS的用途 ...

  3. 树莓派 nfs server安装

    安装服务 sudo  apt-get install  portmap sudo  apt-get install  nfs-kernel-server 配置: sudo nano /etc/expo ...

  4. [ZZ]MTSC 2017 Mobile Testing@Google 演讲的感受

    原文地址: https://testerhome.com/topics/9364 Mobile Testing@Google 其实在开始听谷歌的张南和潘岩开始演讲前,了解下 Google Test C ...

  5. JavaScript问题——在浏览器中的offsetLeft/offsetWidth等属性是什么?

    原文链接http://www.cnblogs.com/xiaohuochai/p/5828369.html https://blog.csdn.net/u012532033/article/detai ...

  6. 2.NB-IoT及通信协议

    NB-IoT 1.什么是NB-IoT? NB-IoT全称窄带物联网(Narrow Band IOT),构建于蜂窝网络,只消耗大约180KHz的带宽,可直接部署于GSM网络.UMTS网络或LTE网络,以 ...

  7. 60秒的快速巡检Linux服务器性能

    uptime dmesg | tail vmstat 1 mpstat -P ALL 1 pidstat 1 iostat -xz 1 free -m sar -n DEV 1 sar -n TCP, ...

  8. Condtion type Z123 is mandatory!

    user exit: MV45AFZZ->userexit_save_document_prepare ZI_EXIT_SD_CNEMS_SAVE_PREPARE ***Start of ins ...

  9. golang初识4 - Go 并发

    Go的CSP并发模型实现:M, P, G Go实现了两种并发形式.第一种是大家普遍认知的:多线程共享内存.其实就是Java或者C++等语言中的多线程开发.另外一种是Go语言特有的,也是Go语言推荐的: ...

  10. TestNG+Java+Selenium+Maven 代码实例

    搭环境时注意不要忘记testng的jar包,selenium的jar包 package com.guge.test; import org.testng.annotations.Test;import ...