python數(shù)據(jù)分析數(shù)據(jù)標(biāo)準(zhǔn)化及離散化詳解
本文為大家分享了python數(shù)據(jù)分析數(shù)據(jù)標(biāo)準(zhǔn)化及離散化的具體內(nèi)容,供大家參考,具體內(nèi)容如下
標(biāo)準(zhǔn)化
1、離差標(biāo)準(zhǔn)化
是對原始數(shù)據(jù)的線性變換,使結(jié)果映射到[0,1]區(qū)間。方便數(shù)據(jù)的處理。消除單位影響及變異大小因素影響。
基本公式為:
x'=(x-min)/(max-min)
代碼:
#?。痷ser/bin/env python
#-*- coding:utf-8 -*-
#author:M10
import numpy as np
import pandas as pd
import matplotlib.pylab as plt
import mysql.connector
conn = mysql.connector.connect(host='localhost',
user='root',
passwd='123456',
db='python')#鏈接本地數(shù)據(jù)庫
sql = 'select price,comment from taob'#sql語句
data = pd.read_sql(sql,conn)#獲取數(shù)據(jù)
#離差標(biāo)準(zhǔn)化
data1 = (data-data.min())/(data.max()-data.min())
print(data1)
運(yùn)行結(jié)果

2、標(biāo)準(zhǔn)差標(biāo)準(zhǔn)化
消除單位影響以及變量自身變異影響。(零-均值標(biāo)準(zhǔn)化)
基本公式為:
x'=(x-平均數(shù))/標(biāo)準(zhǔn)差
python代碼:
#?。痷ser/bin/env python
#-*- coding:utf-8 -*-
#author:M10
import numpy as np
import pandas as pd
import matplotlib.pylab as plt
import mysql.connector
conn = mysql.connector.connect(host='localhost',
user='root',
passwd='123456',
db='python')#鏈接本地數(shù)據(jù)庫
sql = 'select price,comment from taob'#sql語句
data = pd.read_sql(sql,conn)#獲取數(shù)據(jù)
#標(biāo)準(zhǔn)差標(biāo)準(zhǔn)化
data1 = (data-data.mean())/data.std()
print(data1)
運(yùn)行結(jié)果:

3、小數(shù)定標(biāo)標(biāo)準(zhǔn)化
消除單位影響
基本公式為:
其中j=lg(max(|x|)),即以10為底的x的絕對值最大的對數(shù)
x' = x/10^j
實現(xiàn)代碼為:
#?。痷ser/bin/env python
#-*- coding:utf-8 -*-
#author:M10
import numpy as np
import pandas as pd
import matplotlib.pylab as plt
import mysql.connector
conn = mysql.connector.connect(host='localhost',
user='root',
passwd='123456',
db='python')#鏈接本地數(shù)據(jù)庫
sql = 'select price,comment from taob'#sql語句
data = pd.read_sql(sql,conn)#獲取數(shù)據(jù)
#標(biāo)準(zhǔn)差標(biāo)準(zhǔn)化
j = np.ceil(np.log10(data.abs().max()))#進(jìn)一取整,abs()為取絕對值
data1 = data/10**j
print(data1)
結(jié)果:

離散化
離散化是程序設(shè)計中一個常用的技巧,它可以有效的降低時間復(fù)雜度。其基本思想就是在眾多可能的情況中,只考慮需要用的值。離散化可以改進(jìn)一個低效的算法,甚至實現(xiàn)根本不可能實現(xiàn)的算法
1、等寬離散化
將連續(xù)數(shù)據(jù)按照等寬區(qū)間標(biāo)準(zhǔn)離散化數(shù)據(jù),好處之一是處理的數(shù)據(jù)是有限個數(shù)據(jù)而不是無限多。
使用pandas的cut方法。非等寬只需要更改cut的第二個參數(shù),例如:第二個參數(shù)為[1,100,3000,10000,200000],即劃分為了四個區(qū)間。
#?。痷ser/bin/env python
#-*- coding:utf-8 -*-
#author:M10
import numpy as np
import pandas as pd
import matplotlib.pylab as plt
import mysql.connector
conn = mysql.connector.connect(host='localhost',
user='root',
passwd='123456',
db='python')#鏈接本地數(shù)據(jù)庫
sql = 'select price,comment from taob'#sql語句
data = pd.read_sql(sql,conn)#獲取數(shù)據(jù)
#離散化
data1 = data['price'].T.values#獲取價格的一維數(shù)組
lable=['很低','低','中','高','很高']
data2 = pd.cut(data1,5,labels=lable)
print(data2)
執(zhí)行結(jié)果:

2、等頻率離散化
將相同數(shù)量的數(shù)據(jù)放進(jìn)一個區(qū)間。
3、一維聚類離散化
按屬性對數(shù)據(jù)進(jìn)行聚類離散。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python3下錯誤AttributeError: ‘dict’ object has no attribute’ite
這篇文章主要跟大家介紹了關(guān)于在Python3下錯誤AttributeError: 'dict' object has no attribute 'iteritems'的分析與解決方法,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-07-07
圖文詳解在Anaconda安裝Pytorch的詳細(xì)步驟
Anaconda指的是一個開源的Python發(fā)行版本,其包含了conda、Python等180多個科學(xué)包及其依賴項,下面這篇文章主要給大家介紹了關(guān)于在Anaconda安裝Pytorch的詳細(xì)步驟,需要的朋友可以參考下2022-07-07
Python實現(xiàn)字典排序、按照list中字典的某個key排序的方法示例
這篇文章主要介紹了Python實現(xiàn)字典排序、按照list中字典的某個key排序的方法,涉及Python字典與列表排序相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
在VSCode中配置Python開發(fā)環(huán)境的詳細(xì)教程
Visual Studio Code(簡稱VSCode)以其強(qiáng)大的功能和靈活的擴(kuò)展性,成為了許多開發(fā)者的首選,本文將詳細(xì)介紹如何在VSCode中配置Python開發(fā)環(huán)境,需要的朋友可以參考下2025-04-04
pytorch finetuning 自己的圖片進(jìn)行訓(xùn)練操作
這篇文章主要介紹了pytorch finetuning 自己的圖片進(jìn)行訓(xùn)練操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

