0

I am trying to scale a pandas Series with StandardScaler().fit_transform(). However, the output is always an array of zeros.

The input Series has a length of 201, when I do:

print values[:5]

I get a list of floats as below:

0    1943.0
1     508.0
2    1657.0
3     872.0
4     693.0

When I apply the scaler:

X = preprocessing.StandardScaler().fit_transform(values)
print X

Output:

[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
0.  0.  0.]]

How can I fix this?

  • Are you sure that you have a pd.Series? check with `type(values)` this tends to happen if you have a dataframe with several columns. – elyase Sep 13 '16 at 17:46
  • I'm sure it's a Series (I constructed it as pd.Series(data=lst) ). It's a series with features of one sample though, is it possible to scale a single sample with StandardScaler? I managed to fix the issue by using preprocessing.scale(values, axis=1) instead – user3391529 Sep 15 '16 at 05:37
  • can you make a [mcve](http://stackoverflow.com/help/mcve) with sample data? I tried your code and it works ok for me. – elyase Sep 15 '16 at 12:33
  • `import pandas as pd from sklearn import preprocessing inputs = pd.DataFrame() lst = range(5) inpt = pd.Series(data=lst) values = inputs.append(inpt, ignore_index=True) scaler = preprocessing.StandardScaler() X = scaler.fit_transform(values) print X` – user3391529 Sep 23 '16 at 15:53
  • In your example `values` is a DataFrame while you mention that you are sure that your data is a pd.Series. You can check what you pass to the scaler with `type(values)`. – elyase Sep 23 '16 at 19:12
  • if you use a pd.Series, for example `inpt = pd.Series(range(5));scaler.fit_transform(inpt.reshape(-1, 1))` then it works as it should. – elyase Sep 23 '16 at 19:15

0 Answers0