Python 脚本中的错误“预期的二维数组,而是得到一维数组:”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45554008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:07:20  来源:igfitidea点击:

Error in Python script "Expected 2D array, got 1D array instead:"?

pythonpython-3.xmachine-learningpredict

提问by JonTargaryen

I'm following this tutorialto make this ML prediction:

我正在按照本教程进行此 ML 预测:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style

style.use("ggplot")
from sklearn import svm

x = [1, 5, 1.5, 8, 1, 9]
y = [2, 8, 1.8, 8, 0.6, 11]

plt.scatter(x,y)
plt.show()

X = np.array([[1,2],
             [5,8],
             [1.5,1.8],
             [8,8],
             [1,0.6],
             [9,11]])

y = [0,1,0,1,0,1]
X.reshape(1, -1)

clf = svm.SVC(kernel='linear', C = 1.0)
clf.fit(X,y)

print(clf.predict([0.58,0.76]))

I'm using Python 3.6 and I get error "Expected 2D array, got 1D array instead:" I think the script is for older versions, but I don't know how to convert it to the 3.6 version.

我正在使用 Python 3.6,但出现错误“预期的二维数组,改为一维数组:”我认为该脚本适用于旧版本,但我不知道如何将其转换为 3.6 版本。

Already try with the:

已经尝试使用:

X.reshape(1, -1)

回答by Ofer Sadan

You are just supposed to provide the predictmethod with the same 2D array, but with one value that you want to process (or more). In short, you can just replace

您只应该为该predict方法提供相同的二维数组,但提供一个您想要处理的(或更多)值。简而言之,你可以更换

[0.58,0.76]

With

[[0.58,0.76]]

And it should work.

它应该工作。

EDIT: This answer became popular so I thought I'd add a little more explanation about ML. The short version: we can only use predicton data that is of the same dimensionality as the training data (X) was.

编辑:这个答案很受欢迎,所以我想我会添加更多关于 ML 的解释。简短版本:我们只能使用predict与训练数据 ( X)具有相同维度的数据。

In the example in question, we give the computer a bunch of rows in X(with 2 values each) and we show it the correct responses in y. When we want to predictusing new values, our program expects the same - a bunchof rows. Even if we want to do it to just one row (with two values), that row has to be part of another array.

在所讨论的示例中,我们为计算机提供了一堆行X(每行有 2 个值),并在 中向它显示正确的响应y。当我们想要predict使用新值时,我们的程序期望相同 -一堆行。即使我们只想对一行(有两个值)执行此操作,该行也必须是另一个数组的一部分。

回答by stackoverflowuser2010

The problem is occurring when you run prediction on the array [0.58,0.76]. Fix the problem by reshaping it before you call predict():

当您在数组上运行预测时会出现问题[0.58,0.76]。通过在调用之前重塑它来解决问题predict()

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style

style.use("ggplot")
from sklearn import svm

x = [1, 5, 1.5, 8, 1, 9]
y = [2, 8, 1.8, 8, 0.6, 11]

plt.scatter(x,y)
plt.show()

X = np.array([[1,2],
             [5,8],
             [1.5,1.8],
             [8,8],
             [1,0.6],
             [9,11]])

y = [0,1,0,1,0,1]

clf = svm.SVC(kernel='linear', C = 1.0)
clf.fit(X,y)

test = np.array([0.58, 0.76])
print test       # Produces: [ 0.58  0.76]
print test.shape # Produces: (2,) meaning 2 rows, 1 col

test = test.reshape(1, -1)
print test       # Produces: [[ 0.58  0.76]]
print test.shape # Produces (1, 2) meaning 1 row, 2 cols

print(clf.predict(test)) # Produces [0], as expected

回答by devsaw

I faced the same issue except that the data type of the instance I wanted to predict was a panda.Seriesobject.

我遇到了同样的问题,只是我想预测的实例的数据类型是一个panda.Series对象。

Well I just needed to predict one input instance. I took it from a slice of my data.

好吧,我只需要预测一个输入实例。我从我的数据切片中获取了它。

df = pd.DataFrame(list(BiogasPlant.objects.all()))
test = df.iloc[-1:]       # sliced it here

In this case, you'll need to convert it into a 1-D array and then reshapeit.

在这种情况下,您需要将其转换为一维数组,然后再转换reshape

 test2d = test.values.reshape(1,-1)

From the docs, valueswill convert Series into a numpy array.

docsvalues将 Series 转换为一个 numpy 数组。

回答by Vikas Rathour

I use the below approach.

我使用以下方法。

reg = linear_model.LinearRegression()
reg.fit(df[['year']],df.income)

reg.predict([[2136]])

回答by Satyam Mittal

I faced the same problem. You just have to make it an array and moreover you have to put double squared brackets to make it a single element of the 2D array as first bracket initializes the array and the second makes it an element of that array.

我遇到了同样的问题。你只需要把它变成一个数组,而且你必须把双方括号使它成为二维数组的一个元素,因为第一个括号初始化数组,第二个使它成为该数组的一个元素。

So simply replace the last statement by:

因此,只需将最后一条语句替换为:

print(clf.predict(np.array[[0.58,0.76]]))

回答by FASIH AHMED

I was facing the same issue earlier but I have somehow found the solution, You can try reg.predict([[3300]]).

我之前也遇到过同样的问题,但我以某种方式找到了解决方案,您可以尝试reg.predict([[3300]]).

The API used to allow scalar value but now you need to give a 2D array.

API 过去允许使用标量值,但现在您需要提供一个二维数组。

回答by Camunatas

Just insert the argument between a double square bracket:

只需在双方括号之间插入参数:

regressor.predict([[values]])

regressor.predict([[values]])

that worked for me

这对我有用

回答by kingarthur

With one feature my Dataframe list converts to a Series. I had to convert it back to a Dataframe list and it worked.

通过一项功能,我的 Dataframe 列表可以转换为系列。我不得不将它转换回 Dataframe 列表并且它起作用了。

if type(X) is Series:
    X = X.to_frame()

回答by Chahat Agarwal

The X and Y matrix of Independent Variable and Dependent Variable respectively to DataFrame from int64 Type so that it gets converted from 1D array to 2D array.. i.e X=pd.DataFrame(X) and Y=pd.dataFrame(Y) where pd is of pandas class in python. and thus feature scaling in-turn doesn't lead to any error!

自变量和因变量的 X 和 Y 矩阵分别从 int64 类型转换为 DataFrame,使其从一维数组转换为二维数组.. 即 X=pd.DataFrame(X) 和 Y=pd.dataFrame(Y) 其中 pd是python中的pandas类。因此,依次进行特征缩放不会导致任何错误!