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

拜拜、爱过 提交于 2019-12-20 17:41:24

问题


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

Ys = scaler.fit_transform(Y)

I got

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

After that I reshaped using:

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

But got error again:

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

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

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


回答1:


Solution was linked on reshaped method on documentation page.

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

Y.values.reshape(-1,1)



回答2:


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


来源:https://stackoverflow.com/questions/53723928/attributeerror-series-object-has-no-attribute-reshape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!