系列对象没有“重塑”属性

我在用科学工具箱学习线性回归算法。 使用以下方法扩展 Y 目标特性:

Ys = scaler.fit_transform(Y)

我有

ValueError: 预期的2D 数组,取而代之的是1D 数组:

在那之后,我重新塑造了自己:

Ys = scaler.fit_transform(Y.reshape(-1,1))

但是又出错了:

系列对象没有“重塑”属性

所以我查了熊猫,系列文档页面上写着:

重塑(* args,* * kwargs) 自0.19.0版本以来已弃用。

158531 次浏览

Solution was linked on reshaped method on documentation page.

Insted of Y.reshape(-1,1) you need to use:

Y.values.reshape(-1,1)

The solution is indeed to do:

Y.values.reshape(-1,1)

This extracts a numpy array with the values of your pandas Series object and then reshapes it to a 2D array.

The reason you need to do this is that pandas Series objects are by design one dimensional. Another solution if you would like to stay within the pandas library would be to convert the Series to a DataFrame which would then be 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))

You cannot reshape a pandas series, so you need to perform the operation on a numpy array. As others have suggested, you can use y.values.reshape(-1, 1), but if you want to impress your friends, you can use:

y.values[Ellipsis, None]

Which is equivalent to:

y.values[..., None]

It basically means all dimensions as they where, then a new dimension for the last one. Here's a fully working example:

import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler


y = pd.Series(np.random.rand(5))
0    0.497165
1    0.818659
2    0.327064
3    0.772548
4    0.095715
dtype: float64
scaler = StandardScaler()


scaler.fit_transform(y.values[Ellipsis, None])
array([[-0.019],
[ 1.165],
[-0.645],
[ 0.995],
[-1.496]])

Using MinMaxScaler to transform the Series to Dataframe worked on my end.

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()


Y = scaler.fit_transform(pd.DataFrame(y))