医疗设计网站,免费发布便民信息平台,东莞模板网站制作哪家好,吉林省建设工程安管人员管理系统用于回归的决策树与用于分类的决策树类似#xff0c;在DecisionTreeRegressor中实现。DecisionTreeRegressor不能外推#xff0c;也不能在训练数据范围之外的数据进行预测。
利用计算机内存历史及格的数据进行实验#xff0c;数据展示#xff1a;
import pandas as pd
im…用于回归的决策树与用于分类的决策树类似在DecisionTreeRegressor中实现。DecisionTreeRegressor不能外推也不能在训练数据范围之外的数据进行预测。
利用计算机内存历史及格的数据进行实验数据展示
import pandas as pd
import matplotlib.pyplot as pltplt.rcParams[font.sans-serif] [SimHei]ram_pricepd.read_csv(ram_price.csv)
plt.semilogy(ram_price.date,ram_price.price)
plt.xlabel(年份)
plt.ylabel(价格)
plt.show() 利用2000年前的历史数据来预测2000年之后的价格只用日期作为特征对比决策树、线性模型的预测结果
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LinearRegressionplt.rcParams[font.sans-serif] [SimHei]
ram_pricepd.read_csv(ram_price.csv)
#plt.semilogy(ram_price.data,ram_price.price)
data_trainram_price[ram_price.date2000]
data_testram_price[ram_price.date2000]X_trainnp.array(data_train)
#X_traindata_train.date[:, np.newaxis]
y_trainnp.log(data_train.price)treeDecisionTreeRegressor().fit(X_train,y_train)
line_regLinearRegression().fit(X_train,y_train)X_all np.array(ram_price)
#X_allram_price.date[:,np.newaxis]
pred_treetree.predict(X_all)
pred_lrline_reg.predict(X_all)price_treenp.exp(pred_tree)
price_lrnp.exp(pred_lr)plt.semilogy(data_train.date,data_train.price,label训练数据)
plt.semilogy(data_test.date,data_test.price,label测试数据)
plt.semilogy(ram_price.date,price_tree,label决策树预测)
plt.semilogy(ram_price.date,price_lr,label线性预测)
plt.legend()
plt.show() 可以看到两个模型的差异非常明显。线性模型用一条直线对数据做近似对2000年后的价格预测结果非常好但忽略了训练数据和测试数据中一些更细微的变化。树模型则完美预测了训练数据但一旦输入超过了模型训练数据的范围模型就只能持续预测最后一个已知数据点。树不能在训练数据的范围之外生成新的响应所有基于树的模型都有这个缺点。