I\'m using sci-kit learn linear regression algorithm. While scaling Y target feature with:
Ys = scaler.fit_transform(Y)
I got
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]])