数据归一化处理的两种方式
import pandas as pd
import numpy as np
test_data = pd.DataFrame([[1,1,1],[2,2,2],[3,3,3]])
print(test_data)
def MinMaxNormalization(test_data):
return test_data.apply(lambda x:(x-np.min(x))/(np.max(x)-np.min(x)))
normalization_data = MinMaxNormalization(test_data)
print(normalization_data)
import pandas as pd
import numpy as np
test_data = pd.DataFrame([[1,1,1],[2,2,2],[3,3,3]])
print(test_data)
def Z_ScoreNormalization(test_data):
return test_data.apply(lambda x:(x-np.mean(x))/(np.std(x)))
print(Z_ScoreNormalization(test_data))
或者使用sklearn包中的函数完成:
# 将数据进行标准化
# 使用sklearn将数据标准化处理,即对数据减去均值,在除以其标准差
from sklearn import preprocessing as pp
scaled_data = orig_data.copy()
scaled_data[:, 1:3] = pp.scale(orig_data[:, 1:3])