Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to normalize dataframe by standard deviation using scikit-learn?

Given the following dataframe and left-x column:

|       | left-x | left-y | right-x | right-y |
|-------|--------|--------|---------|---------|
| frame |        |        |         |         |
| 0     | 149    | 181    | 170     | 175     |
| 1     | 149    | 181    | 170     | 175     |
| 2     | 149    | 181    | 170     | 175     |
| 3     | 149    | 181    | 170     | 175     |

how may I normalize left-x by standard deviation using scikit-learn library?

like image 505
JP Ventura Avatar asked Mar 04 '18 23:03

JP Ventura


1 Answers

You can normalize by standard deviation without using sci-kit-learn, as follows:

df['left-x'] = df['left-x'] / df['left-x'].std()

Or if you also want to mean-center the data:

df['left-x'] = (df['left-x'] - df['left-x'].mean())/df['left-x'].std()

Here df is your asl.df[l] variable.

The .std() method gives the standard deviation of a dataframe across the given axis. By selecting a column first, the standard deviation is calculated for that column alone.

If you need to do this a lot and want to avoid the clutter, you can wrap it into a function, e.g.

def std_norm(df, column):
    c = df[column]
    df[column] = (c - c.mean())/c.std()

You call it as:

std_norm(df, 'left-x')

Note that this updates the passed DataFrame in-place.

like image 150
mfitzp Avatar answered Oct 21 '22 02:10

mfitzp