python - ValueError:预期的 2D 数组,得到了标量数组:array=5.5
问题描述
为什么我收到以下错误?
ValueError:预期的 2D 数组,得到了标量数组:array=5.5。如果您的数据具有单个特征,则使用 array.reshape(-1, 1) 重塑您的数据,如果它包含单个样本,则使用 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]])
注意使用 [[]] 作为二维数组,如 (sample_num, feature_num)
推荐阅读
- javascript - 如何在我的附加多过滤器菜单中显示添加过滤器选项?寻找 javascript 代码
- javascript - 如何正确使用 axios.get.mockResolvedValue 进行异步调用
- java - 使用 jgit 仅添加新文件
- database - 刷新雪花数据库
- node.js - Puppeteer / Nodejs 正确的函数结构,它似乎没有执行
- javascript - For循环没有显示网站上的所有类
- node.js - 我需要找到将 NodeJs 与 AWS Code Pipeline 集成的 SonarQube 插件
- c# - 如何正确实现 .net 标准库的单元测试
- node.js - 如何在 Express JS 中使用导出的函数?
- java - 如果条件不匹配,字符串函数需要返回什么?