AttributeError:'Series'对象没有属性'reshape'

Har*_*vey 23 python attributeerror reshape python-3.x pandas

我正在使用sci-kit学习线性回归算法.在缩放Y目标功能的同时:

Ys = scaler.fit_transform(Y)
Run Code Online (Sandbox Code Playgroud)

我有

ValueError:预期的2D数组,改为获得1D数组:

之后我改编成使用:

Ys = scaler.fit_transform(Y.reshape(-1,1))
Run Code Online (Sandbox Code Playgroud)

但又得到了错误:

AttributeError:'Series'对象没有属性'reshape'

所以我检查了pandas.Series文档页面,它说:

reshape(*args,**kwargs) 从版本0.19.0开始不推荐使用.

Har*_*vey 44

解决方案与文档页面上的重新整形方法相关联.

的insted的Y.reshape(-1,1),你需要使用:

Y.values.reshape(-1,1)
Run Code Online (Sandbox Code Playgroud)


Joã*_*ida 11

解决方案的确可以做到:

Y.values.reshape(-1,1)

这将使用您的pandas Series对象的值提取一个numpy数组,然后将其重塑为2D数组。

您需要执行此操作的原因是,熊猫系列对象是设计使然的一维。如果您希望留在pandas库中,则另一个解决方案是将Series转换为DataFrame,然后将其转换为2D:

Y = pd.Series([1,2,3,1,2,3,4,32,2,3,42,3])

scaler = StandardScaler()

Ys = scaler.fit_transform(pd.DataFrame(Y))
Run Code Online (Sandbox Code Playgroud)