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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With