python脚本中的错误“预期的2D数组,改为获得1D数组:”?

时间:2022-12-29 18:49:38

I'm following this tutorial to make this ML prediction:

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

Link Tutorial

链接教程

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]))

Im 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,我得到错误“预期的2D阵列,而不是1D阵列:”我认为该脚本适用于旧版本,但我不知道如何将其转换为3.6版本。

Already try with the:

已经尝试过:

   X.reshape(1, -1)

4 个解决方案

#1


33  

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

您应该使用相同的2D数组提供预测方法,但要使用一个您想要处理的值(或更多)。简而言之,您只需更换即可

[0.58,0.76]

With

[[0.58,0.76]]

And it should work

它应该工作

#2


4  

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

#3


0  

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,以便它从1D数组转换为2D数组..即X = pd.DataFrame(X)和Y = pd.dataFrame(Y)其中pd是python中的pandas类。因此特征缩放又不会导致任何错误!

#4


0  

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

我遇到了同样的问题,除了我想要预测的实例的数据类型是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 reshape it.

在这种情况下,您需要将其转换为1-D数组,然后重新整形。

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

From the docs, values will convert Series into a numpy array.

从文档中,值会将Series转换为numpy数组。

#1


33  

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

您应该使用相同的2D数组提供预测方法,但要使用一个您想要处理的值(或更多)。简而言之,您只需更换即可

[0.58,0.76]

With

[[0.58,0.76]]

And it should work

它应该工作

#2


4  

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

#3


0  

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,以便它从1D数组转换为2D数组..即X = pd.DataFrame(X)和Y = pd.dataFrame(Y)其中pd是python中的pandas类。因此特征缩放又不会导致任何错误!

#4


0  

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

我遇到了同样的问题,除了我想要预测的实例的数据类型是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 reshape it.

在这种情况下,您需要将其转换为1-D数组,然后重新整形。

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

From the docs, values will convert Series into a numpy array.

从文档中,值会将Series转换为numpy数组。