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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53723928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:21:50  来源:igfitidea点击:

AttributeError: 'Series' object has no attribute 'reshape'

pythonpython-3.xpandasreshapeattributeerror

提问by Harvey

I'm using sci-kit learn linear regression algorithm. While scaling Y target feature with:

我正在使用 sci-kit 学习线性回归算法。在缩放 Y 目标特征时:

Ys = scaler.fit_transform(Y)

I got

我有

ValueError: Expected 2D array, got 1D array instead:

ValueError:预期的二维数组,而是得到一维数组:

After that I reshaped using:

之后,我使用以下方法进行了重塑:

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

But got error again:

但是又报错了:

AttributeError: 'Series' object has no attribute 'reshape'

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

So I checked pandas.Series documentation page and it says:

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

reshape(*args, **kwargs) Deprecated since version 0.19.0.

reshape(*args, **kwargs) 自 0.19.0 版起已弃用。

回答by Harvey

Solution was linked on reshaped method on documentation page.

解决方案链接到文档页面上的重塑方法。

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

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

Y.values.reshape(-1,1)

回答by Jo?o Almeida

The solution is indeed to do:

解决办法确实是这样做:

Y.values.reshape(-1,1)

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.

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

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:

您需要这样做的原因是 pandas Series 对象是设计为一维的。如果您想留在熊猫库中,另一种解决方案是将系列转换为数据帧,然后将其变为 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))

回答by Nicolas Gervais

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:

您无法重塑 Pandas 系列,因此您需要对 numpy 数组执行操作。正如其他人所建议的,您可以使用y.values.reshape(-1, 1),但如果您想给您的朋友留下深刻印象,您可以使用:

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