首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >预期的二维数组,得到一维数组而不是误差

预期的二维数组,得到一维数组而不是误差
EN

Stack Overflow用户
提问于 2018-10-24 05:52:20
回答 3查看 23.9K关注 0票数 5

我得到的错误是

"ValueError:预期的2D数组,得到1D数组: array= 45000. 50000. 60000. 80000. 110000. 150000. 200000. 300000. 500000.使用array.reshape(-1,1) (如果数据具有单个特性)或array.reshape(1,-1) (如果包含单个样本)对数据进行整形。“

在执行以下代码时:

代码语言:javascript
运行
复制
# SVR

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Position_S.csv')
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values

 # Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_y.fit_transform(y)

# Fitting SVR to the dataset
from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf')
regressor.fit(X, y)

# Visualising the SVR results
plt.scatter(X, y, color = 'red')
plt.plot(X, regressor.predict(X), color = 'blue')
plt.title('Truth or Bluff (SVR)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

# Visualising the SVR results (for higher resolution and smoother curve)
X_grid = np.arange(min(X), max(X), 0.01)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y, color = 'red')
plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')
plt.title('Truth or Bluff (SVR)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-10-24 06:26:16

似乎,预期的维度是错误的。你能试试:

代码语言:javascript
运行
复制
regressor = SVR(kernel='rbf')
regressor.fit(X.reshape(-1, 1), y)
票数 7
EN

Stack Overflow用户

发布于 2020-07-25 04:54:08

问题是,如果您键入y.ndim,您将看到维度为1,如果键入X.ndim,您将看到维度为2。

因此,要解决这个问题,必须将y.ndim的结果从1更改为2。

为此,只需使用numpy类下的reshape函数即可。

代码语言:javascript
运行
复制
data=pd.read_csv("Position_Salaries.csv")
X=data.iloc[:,1:2].values
y=data.iloc[:,2].values
y=np.reshape(y,(10,1))

它应该解决因尺寸而引起的问题。在上面的代码之后进行常规的功能缩放,它肯定会工作的。

如果对你有用的话就投票吧。

谢谢。

票数 0
EN

Stack Overflow用户

发布于 2022-02-05 05:12:27

代码语言:javascript
运行
复制
from sklearn.preprocessing import StandardScaler  

#Creating two objects for dependent and independent variable 
ss_X = StandardScaler()
ss_y = StandardScaler()

X = ss_X.fit_transform(X)
y = ss_y.fit_transform(y.reshape(-1,1))

改造后的,它将工作得很好,

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52961851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档