JP Ventura
JP Ventura

Reputation: 5732

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?

Upvotes: 5

Views: 7911

Answers (2)

mfitzp
mfitzp

Reputation: 15545

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.

Upvotes: 9

YOLO
YOLO

Reputation: 21709

You can use scaling functions from sklearn.preprocessing module.

from sklearn.preprocessing import StandardScaler

sc = StandardScaler()
sc.fit(df['left-x'])

df['left-x'] = sc.transform(df['left-x'])

Upvotes: 3

Related Questions