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

前端 未结 3 1633
情歌与酒
情歌与酒 2021-02-03 17:45

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

Ys = scaler.fit_transform(Y)

I got

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-03 18:05

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

提交回复
热议问题