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

前端 未结 3 1673
情歌与酒
情歌与酒 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:29

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

提交回复
热议问题