Python數(shù)據(jù)預處理之數(shù)據(jù)規(guī)范化(歸一化)示例
本文實例講述了Python數(shù)據(jù)預處理之數(shù)據(jù)規(guī)范化。分享給大家供大家參考,具體如下:
數(shù)據(jù)規(guī)范化
為了消除指標之間的量綱和取值范圍差異的影響,需要進行標準化(歸一化)處理,將數(shù)據(jù)按照比例進行縮放,使之落入一個特定的區(qū)域,便于進行綜合分析。
數(shù)據(jù)規(guī)范化方法主要有:
- 最小-最大規(guī)范化
- 零-均值規(guī)范化
數(shù)據(jù)示例
代碼實現(xiàn)
#-*- coding: utf-8 -*- #數(shù)據(jù)規(guī)范化 import pandas as pd import numpy as np datafile = 'normalization_data.xls' #參數(shù)初始化 data = pd.read_excel(datafile, header = None) #讀取數(shù)據(jù) (data - data.min())/(data.max() - data.min()) #最小-最大規(guī)范化 (data - data.mean())/data.std() #零-均值規(guī)范化
從命令行可以看到下面的輸出:
>>> (data-data.min())/(data.max()-data.min(
0 1 2 3
0 0.074380 0.937291 0.923520 1.000000
1 0.619835 0.000000 0.000000 0.850941
2 0.214876 0.119565 0.813322 0.000000
3 0.000000 1.000000 1.000000 0.563676
4 1.000000 0.942308 0.996711 0.804149
5 0.264463 0.838629 0.814967 0.909310
6 0.636364 0.846990 0.786184 0.929571>>> (data-data.mean())/data.std()
0 1 2 3
0 -0.905383 0.635863 0.464531 0.798149
1 0.604678 -1.587675 -2.193167 0.369390
2 -0.516428 -1.304030 0.147406 -2.078279
3 -1.111301 0.784628 0.684625 -0.456906
4 1.657146 0.647765 0.675159 0.234796
5 -0.379150 0.401807 0.152139 0.537286
6 0.650438 0.421642 0.069308 0.595564
上述代碼改為使用print
語句打印,如下:
#-*- coding: utf-8 -*- #數(shù)據(jù)規(guī)范化 import pandas as pd import numpy as np datafile = 'normalization_data.xls' #參數(shù)初始化 data = pd.read_excel(datafile, header = None) #讀取數(shù)據(jù) print((data - data.min())/(data.max() - data.min())) #最小-最大規(guī)范化 print((data - data.mean())/data.std()) #零-均值規(guī)范化
可輸出如下打印結果:
0 1 2 3
0 0.074380 0.937291 0.923520 1.000000
1 0.619835 0.000000 0.000000 0.850941
2 0.214876 0.119565 0.813322 0.000000
3 0.000000 1.000000 1.000000 0.563676
4 1.000000 0.942308 0.996711 0.804149
5 0.264463 0.838629 0.814967 0.909310
6 0.636364 0.846990 0.786184 0.929571
0 1 2 3
0 -0.905383 0.635863 0.464531 0.798149
1 0.604678 -1.587675 -2.193167 0.369390
2 -0.516428 -1.304030 0.147406 -2.078279
3 -1.111301 0.784628 0.684625 -0.456906
4 1.657146 0.647765 0.675159 0.234796
5 -0.379150 0.401807 0.152139 0.537286
6 0.650438 0.421642 0.069308 0.595564
附:代碼中使用到的normalization_data.xls點擊此處本站下載。
更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學運算技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
- Python多進程模式實現(xiàn)多核CPU并行計算
- python multiprocessing 多進程并行計算的操作
- Python基于pyCUDA實現(xiàn)GPU加速并行計算功能入門教程
- Python 多核并行計算的示例代碼
- python數(shù)據(jù)預處理之數(shù)據(jù)標準化的幾種處理方式
- python實現(xiàn)數(shù)據(jù)預處理之填充缺失值的示例
- python數(shù)據(jù)預處理之將類別數(shù)據(jù)轉換為數(shù)值的方法
- Python----數(shù)據(jù)預處理代碼實例
- python數(shù)據(jù)預處理 :樣本分布不均的解決(過采樣和欠采樣)
- python 刪除excel表格重復行,數(shù)據(jù)預處理操作
- Python Joblib庫使用方法案例總結
相關文章
pytorch深度神經(jīng)網(wǎng)絡入門準備自己的圖片數(shù)據(jù)
這篇文章主要為大家介紹了pytorch深度神經(jīng)網(wǎng)絡入門準備自己的圖片數(shù)據(jù)示例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06python數(shù)據(jù)庫開發(fā)之MongoDB安裝及Python3操作MongoDB數(shù)據(jù)庫詳細方法與實例
這篇文章主要介紹了python數(shù)據(jù)庫開發(fā)之MongoDB安裝及Python3操作MongoDB數(shù)據(jù)庫詳細方法與實例,需要的朋友可以參考下2020-03-03Python實現(xiàn)根據(jù)IP地址和子網(wǎng)掩碼算出網(wǎng)段的方法
這篇文章主要介紹了Python實現(xiàn)根據(jù)IP地址和子網(wǎng)掩碼算出網(wǎng)段的方法,涉及Python基于Linux平臺的字符串操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07python 檢查數(shù)據(jù)中是否有缺失值,刪除缺失值的方式
今天小編就為大家分享一篇python 檢查數(shù)據(jù)中是否有缺失值,刪除缺失值的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12