赞
踩











# 导包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeRegressor
# 产生数据
x = np.linspace(1,5,100)
y = np.sin(x)
# 绘图
plt.figure(figsize=(10,5))
plt.scatter(x,y,c='r')
# 添加噪点
y[::5]+=np.random.randn(20)*0.1 # 一维数组的加法
# 绘图
plt.figure(figsize=(10,5))
plt.scatter(x,y,c='r')
# 转化输入数据维度
x = x.reshape(-1,1)
x.shape
# 预测
Dt = DecisionTreeRegressor(max_depth=7)
Dt.fit(x,y)
Dt.score(x,y)
# 生成测试数据预测
x_p = np.linspace(3.5,5,100)
x_p = x_p.reshape(-1,1)
x_p.shape
# 预测绘图
pre_y = Dt.predict(x)
plt.scatter(x,y,c='r')
plt.scatter(x_p,pre_y,c='b')



Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。