【问题标题】:SVR Model -->Feature Scaling - Expected 2D array, got 1D array insteadSVR 模型 --> 特征缩放 - 预期 2D 阵列,得到 1D 阵列
【发布时间】:2018-10-29 19:29:39
【问题描述】:

我试图了解下面的代码有什么问题。我知道Y 变量是1D 数组,预计是2D 数组,需要重新调整结构,但该代码之前工作正常,但出现警告。

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

ValueError: Expected 2D array, got a 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.

【问题讨论】:

    标签: python pandas scikit-learn


    【解决方案1】:

    解决方法在错误信息中:

    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.
    

    由于您传递的是单个特征(不是单个样本),请尝试:

    y = sc_y.fit_transform(y.reshape(-1, 1))
    

    【讨论】:

    • 你能帮我从基础和高级python开始哪个教程最好吗?
    • @Ramangoyal,对于基本的 Python,我是automatetheboringstuff.com 的忠实粉丝!对于更高级的 Python 进行数据分析,我强烈推荐 Wes McKinney 的 Python For Data Analysis,笔记本在这里:github.com/wesm/pydata-book
    • 您介意解释一下为什么需要二维数组吗?毕竟,数据只是“标准化”,不需要二维数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 2018-09-16
    • 1970-01-01
    • 2019-10-20
    • 2018-11-26
    相关资源
    最近更新 更多