当前位置:   article > 正文

python实现(0,1)的归一化_python 归一化到0 1

python 归一化到0 1

数据需要进行归一化,整理了以下三种版本

(1)按列进行归一化

  1. #(1)
  2. import pandas as pd
  3. import numpy as np
  4. from sklearn.preprocessing import MinMaxScaler
  5. weight_unbn = np.loadtxt('D:\\weight_unbn.txt')
  6. scaler = MinMaxScaler( )
  7. scaler.fit(weight_unbn)
  8. scaler.data_max_
  9. weight_bn=scaler.transform(weight_unbn)
  10. np.savetxt('D:\\weight_bn.txt', weight_bn)
  11. weight_bn_pd = pd.DataFrame(weight_bn)
  12. weight_bn_pd.to_csv('D:\\weight_bn.csv')
  13. # print(weight_bn_pd.head())

 (2)按列进行归一化

  1. import numpy as np
  2. def noramlization(data):
  3. minVals = data.min(0)
  4. maxVals = data.max(0)
  5. ranges = maxVals - minVals
  6. normData = np.zeros(np.shape(data))
  7. m = data.shape[0]
  8. normData = data - np.tile(minVals, (m, 1))
  9. normData = normData/np.tile(ranges, (m, 1))
  10. return normData, ranges, minVals
  11. weight_unbn = np.loadtxt('D:\\weight_unbn.txt')
  12. weight_unbn = np.array(weight_unbn)
  13. weight_bn, _, _, = noramlization(weight_unbn)
  14. #print(weight_bn[:3,:])

(3)按列进行归一化

  1. from sklearn import preprocessing
  2. import numpy as np
  3. weight_unbn = np.loadtxt('D:\\Data\\biclustering_data\\weight_unbn.txt')
  4. min_max_scaler = preprocessing.MinMaxScaler()
  5. weight_bn = min_max_scaler.fit_transform(weight_unbn)
  6. #print(weight_bn[:3,:])

(4)全局进行归一化

  1. import numpy as np
  2. def noramlization(data):
  3. minVals = data.min()
  4. maxVals = data.max()
  5. ranges = maxVals - minVals
  6. normData = np.zeros(np.shape(data))
  7. m = data.shape[0]
  8. normData = data - np.tile(minVals, (m, 1))
  9. normData = normData/np.tile(ranges, (m, 1))
  10. return normData, ranges, minVals, maxVals
  11. weight_unbn = np.array([[ 0, 1, 5, 3, 4],
  12. [ 5, 5.5, 6, 8, 9],
  13. [10, 11, 12, 13, 14]])
  14. weight_unbn = np.array(weight_unbn)
  15. weight_bn, ranges, minVals, maxVals,= noramlization(weight_unbn)
  16. print(weight_bn, ranges, minVals, maxVals)
  1. from sklearn import preprocessing
  2. import numpy as np
  3. genome_derived_features_con = pd.read_csv('D:/Data/features/genome_derived_features_con.csv', index_col=0)
  4. min_max_scaler = preprocessing.MinMaxScaler()
  5. genome_derived_features_con = np.array(genome_derived_features_con)
  6. genome_derived_features_con_reshape = genome_derived_features_con.reshape([-1, 1])
  7. genome_derived_features_con_row_norm_reshape = min_max_scaler.fit_transform(genome_derived_features_con_reshape)
  8. #print(weight_bn[:3,:])
  9. genome_derived_features_con_row_norm = genome_derived_features_con_row_norm_reshape.reshape(genome_derived_features_con.shape)
  10. genome_derived_features_con_row_norm = pd.DataFrame(genome_derived_features_con_row_norm)
  11. genome_derived_features_con_row_norm.to_csv('D:/Data/features/genome_derived_features_con_row_norm_1.csv')

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/174517
推荐阅读
相关标签
  

闽ICP备14008679号