【问题标题】:ValueError: Expected 2D array, got scalar array instead: array=5.5ValueError:预期的 2D 数组,得到了标量数组:array=5.5
【发布时间】: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


【解决方案1】:

尝试使用它来预测:

  tree_reg.predict([[5.5]])  

注意要使用[[]]作为2d数组,如(sample_num,feature_num)

【讨论】:

  • 它如此多地工作。我从Udemy学习Python。这些课程来自2017年。我认为我带走了所有错误。更新问题。我的意思是他们的版本是旧的 span>
猜你喜欢
  • 2021-11-15
  • 2019-09-11
  • 2018-12-11
  • 2021-09-02
  • 2020-03-19
  • 2021-12-29
  • 1970-01-01
  • 2018-12-21
  • 2019-06-15
相关资源
最近更新 更多