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. Android OkHttp Get请求方式

    1.导入okhttp-2.7.5.jar和okio-1.11.0.jar 2.Get请求 public void getDataByGet(){ OkHttpClient client = new O ...

  2. squid http,https, 代理,默认端口3128

    squid http,https, 代理,默认端口3128 https 代理时出现 403,是因为squid默认允许 192.168.0.0 网段代理 在配置文件中,““acl localnet sr ...

  3. 嵌入式V3s交叉编译 tslib和QT4.8.7,并使用Qt Creator编译项目

    本文主参考:http://zero.lichee.pro/%E5%BA%94%E7%94%A8/QT_index.html 环境 Ubuntu16 64位 arm-linux-gnueabihf ve ...

  4. 华为PAY公交卡建议开卡免费!

    华为PAY公交卡,大家都知道是华为与当地交通卡通公司合作的,开卡费大概15-29元,最低充值10-30元. 估计大部分开卡费是给了当地交通卡公司,华为也应该有收入分成.这个虚拟公交卡,不同于实体公交卡 ...

  5. uWSGI的配置与发布

    参考:   https://www.cnblogs.com/pyyu/p/9481344.html 一.什么是wsgi, uwsgi, uWSGI wsgi 全称web server gateway ...

  6. layer弹出层父子页面交互(子页面form表单提交)

    例如:父页面中有数据需要修改,但不需要跳转到下一个页面进行处理 例图:

  7. Nestjs学习进度-180420

    # 一.已完成:# 1.将NPM降级到4.6.1,保证Nest环境正常: 2.成功运行了示例程序:https://docs.nestjs.cn/4.6/firststeps 3.粗略看了下文档. # ...

  8. 对象名 'dbo.__MigrationHistory' 无效 错误解决

    // 在数据库上下文的构造方法里 public GewPeAppContext() : base(ConnectionStrings.GewPeAppConnectionString) { // 添加 ...

  9. Intellij IDEA超好用的快捷键

    1.首推 Ctrl+Shift+Enter,自动跳出括号,在行尾添加分号,并自动回车. 2.Ctrl+Shift+J,将多行代码折叠成一行. 当你遇到的时候,这个快捷键非常实用.比如你在写painle ...

  10. Redis深入学习笔记(三)RDB及AOF流程

    RDB是Redis持久化数据的一种方式,是执行时间点的Redis内存快照,redis数据还原时加载rdb文件,Redis的主从数据同步也是基于RDB实现的. RDB流程: 1)执行bgsave命令,R ...