【发布时间】:2019-09-19 21:27:56
【问题描述】:
为什么会出现以下错误?
ValueError:预期的 2D 数组,得到的是标量数组:array=5.5。 如果您的数据有 单个特征或 array.reshape(1, -1) 如果它包含单个样本。
这是我的代码:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.read_csv("decision-tree-regression-dataset.csv",sep = ";",header = None)
x = df.iloc[:,0].values.reshape(-1,1)
y = df.iloc[:,1].values.reshape(-1,1)
# decision tree regression
from sklearn.tree import DecisionTreeRegressor
tree_reg = DecisionTreeRegressor() # random sate = 0
tree_reg.fit(x,y)
tree_reg.predict(5.5)
x_ = np.arange(min(x),max(x),0.01).reshape(-1,1)
y_head = tree_reg.predict(x_)
# visualize
plt.scatter(x,y,color="red")
plt.plot(x_,y_head,color = "green")
plt.xlabel("tribun level")
plt.ylabel("ucret")
plt.show()
【问题讨论】:
-
哪一行会报错?
tree_reg.predict(5.5)?x.shape的输出是什么? -
您能粘贴整个错误引用吗?这样就更容易找到问题了。
-
@sentence 哪一行抛出错误? -idk-tree_reg.predict(5.5)? -so:)- x.shape 的输出是什么? -我需要在哪里写 x.shape?-
-
@funie200 评论框显示整个错误引用“字符太长”,因此无法添加抱歉
标签: python machine-learning python-3.7