ValueError:预期的2D数组,而是标量数组 [英] ValueError: Expected 2D array, got scalar array instead

查看:35
本文介绍了ValueError:预期的2D数组,而是标量数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在练习简单线性回归模型时,我遇到了这个错误:

  ValueError:预期的2D数组,而是标量数组:数组= 60.如果数据只有一个,则使用array.reshape(-1,1)重塑数据feature或array.reshape(1,-1)(如果它包含单个样本). 

这是我的代码(Python 3.7):

 将pandas导入为pd将numpy导入为np导入matplotlib.pyplot作为plt从sklearn.linear_model导入LinearRegression从sklearn.metrics导入r2_score数据= pd.read_csv("hw_25000.csv")hgt = data.Height.values.reshape(-1,1)wgt = data.Weight.values.reshape(-1,1)回归= LinearRegression()gression.fit(hgt,wgt)打印(regression.predict(60)) 

解决方案

简短答案:

  regression.predict([[60]]) 

长答案:gression.predict接受您要预测的二维数组.数组中的每个项目都是您要模型进行预测的点".假设我们要对点60、52和31进行预测.然后我们说 regression.predict([[60],[52],[31]])

之所以需要2d数组,是因为我们可以在比2d高的维度空间中进行线性回归.例如,我们可以在3d空间中进行线性回归.假设我们要预测给定数据点(x,y)的"z".然后我们需要说"regression.predict([[x,y]]).

再来看这个例子,我们可以为一组"x"和"y"点预测"z".例如,我们要预测每个点的"z"值:(0,2),(3,7),(10,8).然后,我们将说"regression.predict([[0,2],[3,7],[10,8]])",这充分表明了对gression.predict需要采用二维值数组来预测点的需求./p>

While practicing Simple Linear Regression Model I got this error:

ValueError: Expected 2D array, got scalar array instead:
array=60.
Reshape your data either using array.reshape(-1, 1) if your data has a single 
feature or array.reshape(1, -1) if it contains a single sample.

This is my code (Python 3.7):

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
data = pd.read_csv("hw_25000.csv")


hgt = data.Height.values.reshape(-1,1)
wgt = data.Weight.values.reshape(-1,1)

regression = LinearRegression()
regression.fit(hgt,wgt)

print(regression.predict(60))

解决方案

Short answer:

regression.predict([[60]])

Long answer: regression.predict takes a 2d array of values you want to predict on. Each item in the array is a "point" you want your model to predict on. Suppose we want to predict on the points 60, 52, and 31. Then we'd say regression.predict([[60], [52], [31]])

The reason we need a 2d array is because we can do linear regression in a higher dimension space than just 2d. For example, we could do linear regression in a 3d space. Suppose we want to predict "z" for a given data point (x, y). Then we'd need to say regression.predict([[x, y]]).

Taking this example further, we could predict "z" for a set of "x" and "y" points. For example, we want to predict the "z" values for each of the points: (0, 2), (3, 7), (10, 8). Then we would say regression.predict([[0, 2], [3, 7], [10, 8]]) which fully demonstrates the need for regression.predict to take a 2d array of values to predict on points.

这篇关于ValueError:预期的2D数组,而是标量数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆