简体   繁体   English

在StandardScalar Fit_Transform上获取错误

[英]Getting Error on StandardScalar Fit_Transform

 import numpy as np
 import matplotlib.pyplot as plt
 import pandas as pd

 dataset = pd.read_csv('Position_Salaries.csv')
 X = dataset.iloc[:, 1:2].values
 y = dataset.iloc[:, 2].values

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

Ok so here is the problem. 好的,这就是问题所在。 both X and y are single feature and have one column. X和y都是单一要素,并且只有一列。 As you can see X is a matrix . 如您所见,X是一个矩阵 and y is a vector X = dataset.iloc[:, 1:2].values y = dataset.iloc[:, 2].values y是向量 X = dataset.iloc [:, 1:2] .values y = dataset.iloc [:, 2] .values

Now when I run y = sc_y.fit_transform(y) I get the error that it is a 1D array. 现在,当我运行y = sc_y.fit_transform(y)我得到一个错误,它是一维数组。 And if i change y = dataset.iloc[:, 2:3].values making it a 2D array. 如果我更改y = dataset.iloc[:, 2:3].values ,使其成为2D数组。 But I want it to stay as 1D array since its the dependent variable and want it to stay that way. 但是我希望它由于其因变量而保持为一维数组,并希望保持这种状态。 Also i solved earlier different examples where I had to rescale similar data, and it did not give me this kind of error. 我也解决了较早的不同示例,在这些示例中,我不得不重新缩放相似的数据,但是它没有给我这种错误。 Not sure why it is giving me now. 不知道为什么现在给我。 Moreover i am watching a video while coding and in the video everything is the same but he doesn't get any error. 此外,我在编码时正在观看视频,并且视频中的所有内容都相同,但他没有出现任何错误。

StandardScaler is meant to work on the features, not labels or target data. StandardScaler用于处理功能,而不是标签或目标数据。 Hence only works on 2-d Data. 因此仅适用于二维数据。 Please see here for documentation: 请参阅此处获取文档:

What you can do is, use scale function . 您可以做的是使用比例功能 StandardScaler is just a wrapper over this function. StandardScaler只是该函数的包装。

from sklearn.preprocessing import scale
y = scale(y)

Or if you want to use StandarScaler, you need to reshape your y to a 2-d array like this: 或者,如果您想使用StandarScaler,则需要将y重塑为二维数组,如下所示:

import numpy as np
y = np.array(y).reshape(-1,1)
y = sc_y.fit_transform(y)
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X = sc_X.fit_transform(X)

sc_y = StandardScaler()
y = np.array(y).reshape(-1,1)
y = sc_y.fit_transform(y)
y = y.flatten()

您可以使用flatten从2D数组获取1D数组:

y.flatten()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有 fit_transform 错误的列转换器 - Column Transformer with fit_transform error PolynomialFeatures fit_transform给出值错误 - PolynomialFeatures fit_transform is giving Value error TypeError: fit_transform() 缺少 1 个必需的位置参数:'X' - 运行相同的代码并得到一个唯一的错误 - TypeError: fit_transform() missing 1 required positional argument: 'X' - Running identical code & getting a unique error 使用 Pickle 加载保存的 model - 在加载的程序中完成 fit_transform 时出现错误 - Loading saved model using Pickle - getting error as fit_transform is done in loaded program 使用 fit_transform() 和 transform() - Using fit_transform() and transform() 将 fit_transform 与 OneHotEncoder 一起使用时出现 Memory 错误 - Memory error when using fit_transform with OneHotEncoder 数据框fit_transform抛出错误,看似错误 - Dataframe fit_transform throwing error with seemingly incorrect error 添加停用词后CountVectorizer在fit_transform上引发错误 - CountVectorizer throws error on fit_transform after adding stop words 在 Python 中查找 tf-idf 时出现 fit_transform 错误 - Error in fit_transform while finding tf-idf in Python 为什么 fit_transform 不断抛出错误? - Why does fit_transform keep throwing an error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM