[英]How do you calculate the standard deviation for gap-statistics using inertia in scikit-learn?
[英]How to normalize dataframe by standard deviation using scikit-learn?
給定以下數據幀和left-x
列:
| | 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 |
我如何使用scikit-learn庫將標准偏差標准化為left-x
?
您可以通過標准偏差進行標准化,而無需使用sci-kit-learn,如下所示:
df['left-x'] = df['left-x'] / df['left-x'].std()
或者,如果您還想要中心數據:
df['left-x'] = (df['left-x'] - df['left-x'].mean())/df['left-x'].std()
這里df
是你的asl.df[l]
變量。
.std()
方法給出了給定軸上數據幀的標准偏差。 通過首先選擇一列,僅為該列計算標准偏差。
如果你需要做很多事情並希望避免混亂,你可以將它包裝成一個函數,例如
def std_norm(df, column):
c = df[column]
df[column] = (c - c.mean())/c.std()
你稱之為:
std_norm(df, 'left-x')
請注意,這會就地更新傳遞的DataFrame。
您可以使用sklearn.preprocessing
模塊中的縮放功能。
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
sc.fit(df['left-x'])
df['left-x'] = sc.transform(df['left-x'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.