欢迎加入python学习交流群 667279387
Pandas学习(一)–数据的导入
pandas学习(二)–双色球数据分析
pandas学习(三)–NAB球员薪资分析
pandas学习(四)–数据的归一化
pandas学习(五)–pandas学习视频
归一化方法有两种形式,一种是把数变为(0,1)之间的小数,一种是把有量纲表达式变为无量纲表达式。主要是为了数据处理方便提出来的,把数据映射到0~1范围之内处理。
也称为离差标准化,是对原始数据的线性变换,使结果值映射到[0 - 1]之间。转换函数如下:
这种方法给予原始数据的均值(mean)和标准差(standard deviation)进行数据的标准化。经过处理的数据符合标准正态分布,即均值为0,标准差为1,转化函数为:
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
"""
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
"""
比较好奇为啥上面df.std()和np.std()算出来的值不一样,估计哪里有点不一样的地方,还需要研究研究。下面做了一个简单的实验,不知道df.std()具体是怎么算的。
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.
这个是numpy对ddof的解释。
简单点说np.std()计算的是标准差,df.std()计算的是标准差的无偏估计
参考资料
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
欢迎加入python学习交流群 667279387