"ValueError:预期的2D数组,取而代之的是1D数组"问题. [英] Problem with "ValueError: Expected 2D array, got 1D array instead"

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

问题描述

我需要运行SVR(支持的矢量回归).我有一个CSV数据框,使用一个目标变量和多个回归变量可以运行OLS回归没有问题.但是我对这部分代码有疑问.

I need to run a SVR (supported vector regression). I have a CSV data frame.I had no problems to run the OLS regression, with one target variable and multiple regressors. But I have a problem with this part of the code.

所以,这是我的代码:


import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR

sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_y.fit_transform(y)

y_pred = sc_y.inverse_transform ((regressor.predict (sc_X.transform(np.array([[6.5]])))))
plt.scatter(X, y, color = 'magenta')
plt.plot(X, regressor.predict(X))
plt.title('SVR')
plt.xlabel('X')
plt.ylabel('VF')
plt.show()

X_grid = np.arange(min(X), max(X), 0.1)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y)
plt.plot(X_grid, regressor.predict(X_grid))
plt.title('SVR')
plt.xlabel('X')
plt.ylabel('VF')
plt.show()

我收到以下错误消息:"ValueError:预期的2D数组,取而代之的是1D数组如果数据具有单个功能,则使用array.reshape(-1,1)调整数据的形状;如果数据包含单个样本,则使用array.reshape(1,-1)调整数据的形状."

I have the following error message:"ValueError: Expected 2D array, got 1D array instead 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."

这是我第一次遇到此问题.我在其他主题中看到的并不稀少,但实际上我不知道在代码中重塑数据的位置.当我尝试这样做时,它说DataFrame没有重塑功能.

It's the first time I encounter this problem. I saw in other topic that is not scarce, but in fact i don't understand where to reshape the data in my code. When i tried to do it, it says that DataFrame has no reshape function.

这是我的数据集的图片.目标是VF,所有其他变量都是回归变量.

Here is a pic of my dataset. The target is VF, all the other variables are the regressors.

谢谢

推荐答案

看来,当您这样做时:

 X = sc_X.fit_transform(X)

X包含多个变量.8个具体

X contains more than one variables. 8 to be specific

接下来,您要做的事:

 regressor.predict(sc_X.transform(np.array([[6.5]])))

现在,您尝试转换一个只有一个变量的新样本,但是 sc 模型已针对具有多个变量的数据进行了训练.

Now you try to transform a new sample that has only one variable but the sc model was trained on data with more than one variable.

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

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