[英]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.