# pandas学习（四）--数据的归一化

9 篇文章 6 订阅
4 篇文章 3 订阅

##常见归一化算法
###1、min-max标准化（Min-Max Normalization）

###2、Z-score标准化方法

##pandas归一化方法

###1、min-max标准化

import numpy as np
import pandas as pd

np.random.seed(1)
df = pd.DataFrame(np.random.randn(4, 4) * 4 + 3)
print(df)
"""
0         1         2         3
0  9.497381  0.552974  0.887313 -1.291874
1  6.461631 -6.206155  9.979247 -0.044828
2  4.276156  2.002518  8.848432 -5.240563
3  1.710331  1.463783  7.535078 -1.399565

"""
df_norm = (df - df.min()) / (df.max() - df.min())
print(df_norm)
"""
0         1         2         3
0  1.000000  0.823413  0.000000  0.759986
1  0.610154  0.000000  1.000000  1.000000
2  0.329499  1.000000  0.875624  0.000000
3  0.000000  0.934370  0.731172  0.739260
"""
df_norm2=df.apply(lambda x: (x - np.min(x)) / (np.max(x) - np.min(x)))
print(df_norm2)
"""
0         1         2         3
0  1.000000  0.823413  0.000000  0.759986
1  0.610154  0.000000  1.000000  1.000000
2  0.329499  1.000000  0.875624  0.000000
3  0.000000  0.934370  0.731172  0.739260
"""


####2、Z-score标准化方法

import numpy as np
import pandas as pd

np.random.seed(1)
df = pd.DataFrame(np.random.randn(4, 4) * 4 + 3)
print(df)
"""
0         1         2         3
0  9.497381  0.552974  0.887313 -1.291874
1  6.461631 -6.206155  9.979247 -0.044828
2  4.276156  2.002518  8.848432 -5.240563
3  1.710331  1.463783  7.535078 -1.399565
"""
df_norm = (df - df.mean()) / (df.std())
print(df_norm)
"""
0         1         2         3
0  1.213741  0.287871 -1.454237  0.312166
1  0.295115 -1.481492  0.777218  0.866440
2 -0.366215  0.667324  0.499679 -1.442906
3 -1.142640  0.526297  0.177340  0.264301
"""

df_norm2 = df.apply(lambda x: (x - np.mean(x)) / (np.std(x)))
print(df_norm2)
"""
0         1         2         3
0  1.401507  0.332405 -1.679208  0.360458
1  0.340769 -1.710680  0.897454  1.000479
2 -0.422869  0.770560  0.576980 -1.666125
3 -1.319407  0.607716  0.204774  0.305188
"""


import numpy as np
import pandas as pd

data = [(1, 2), (3, 4)]
df = pd.DataFrame(data)
print(df)
"""
0  1
0  1  2
1  3  4
"""
df_std1 = df.std(axis=0)
print(df_std1)
"""
0    1.414214
1    1.414214
"""
df_std2 = df.apply(lambda x: np.std(x), axis=0)
print(df_std2)
"""
0    1.0
1    1.0
"""


https://stackoverflow.com/questions/24984178/different-std-in-pandas-vs-numpy

import numpy as np
import pandas as pd

data = [(1, 2), (3, 4)]
df = pd.DataFrame(data)
print(df)
"""
0  1
0  1  2
1  3  4
"""
df_std1 = df.std(axis=0)
print(df_std1)
"""
0    1.414214
1    1.414214
"""
df_std2 = df.apply(lambda x: np.std(x，ddof=1), axis=0)
print(df_std2)
"""
0    1.414214
1    1.414214
"""


ddof : int, optional
Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. By default ddof is zero.

1、https://stackoverflow.com/questions/12525722/normalize-data-in-pandas
2、https://stackoverflow.com/questions/26414913/normalize-columns-of-pandas-data-frame
3、https://en.wikipedia.org/wiki/Unbiased_estimation_of_standard_deviation

• 23
点赞
• 119
收藏
觉得还不错? 一键收藏
• 4
评论
06-23 657
10-27 4649
05-26 418
12-21 632
07-25 883

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。