预期为2D数组,而是改为1D数组.如何去爱这个? [英] Expected 2D array, got 1D array instead. How to reslove this?

查看:60
本文介绍了预期为2D数组,而是改为1D数组.如何去爱这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个错误,该错误会抛出一个预期的2D数组,而是得到了1D数组.错误会在下面详细说明,在输出之后也可以找到完整的代码,

There is an Error which throws an Expected 2D array, got 1D array instead. THw error is detailed below, also find the complete code after the Output,

输出:

Traceback (most recent call last):

  File "<ipython-input-16-d2ec9bb14152>", line 5, in <module>
    y = sc_y.fit_transform(y).reshape(1,-1)

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\base.py", line 553, in fit_transform
    return self.fit(X, **fit_params).transform(X)

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 639, in fit
    return self.partial_fit(X, y)

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 663, in partial_fit
    force_all_finite='allow-nan')

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 521, in check_array
    "if it contains a single sample.".format(array))

ValueError: Expected 2D array, got 1D array instead:
array=[  45000.   50000.   60000.   80000.  110000.  150000.  200000.  300000.
  500000. 1000000.].
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*```



The complete code is as follows:

**Code:**

```# SVR Template
# Data Preprocessing

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

# Importing the dataset
dataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:, 1:2].values # 1:2 is to consider X as a matrix
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 the SVR Model to the dataset
from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf')
regressor.fit(X,y)

# Predicting a new result 
y_pred = regressor.predict([[6.5]]) 

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


  [1]: https://i.stack.imgur.com/ksQs2.png

推荐答案

这与以下内容有关:

This is related to: stackoverflow question from a year ago The error message is saying exactly what you need to do: y = sc_y.fit_transform(y.reshape(-1, 1)) However, I don't think you need to standard scale your ys to achieve good performance with SVR (the x yes though) ref of statexchange.

这篇关于预期为2D数组,而是改为1D数组.如何去爱这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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