在大數(shù)據(jù)和人工智能時(shí)代,數(shù)據(jù)科學(xué)和機(jī)器學(xué)習(xí)在許多科技領(lǐng)域都變得必不可少。處理數(shù)據(jù)的一個(gè)必要方面是能夠直觀地描述、總結(jié)和表示數(shù)據(jù)。Python 統(tǒng)計(jì)庫是全面、流行且廣泛使用的工具,可幫助處理數(shù)據(jù)。
對于數(shù)據(jù)的基礎(chǔ)描述我們可以進(jìn)行下面的操作:
整套學(xué)習(xí)自學(xué)教程中應(yīng)用的數(shù)據(jù)都是《三國志》、《真·三國無雙》系列游戲中的內(nèi)容。
描述性統(tǒng)計(jì)是關(guān)于描述和總結(jié)數(shù)據(jù)。
使用兩種主要方法:
可以將描述性統(tǒng)計(jì)應(yīng)用于一個(gè)或多個(gè)數(shù)據(jù)集或變量。當(dāng)描述和總結(jié)單個(gè)變量時(shí),執(zhí)行單變量分析。當(dāng)搜索一對變量之間的統(tǒng)計(jì)關(guān)系時(shí),進(jìn)行雙變量分析。同樣多變量分析同時(shí)涉及多個(gè)變量。
描述性統(tǒng)計(jì)分析需要理解的度量類型有:
在統(tǒng)計(jì)中總體是所有元素或項(xiàng)目的集合??傮w通常很大,這使得它們不適合收集和分析數(shù)據(jù)。這就是為什么統(tǒng)計(jì)學(xué)家通常試圖通過選擇和檢查該總體的代表性子集來對總體做出一些結(jié)論。
總體的這個(gè)子集稱為樣本。理想情況樣本應(yīng)在令人滿意的程度上保留總體的基本統(tǒng)計(jì)特征。這樣將能夠使用樣本來收集有關(guān)總體的結(jié)論。
異常值是與從樣本或總體中獲取的大多數(shù)數(shù)據(jù)顯著不同的數(shù)據(jù)點(diǎn)。通常造成異常數(shù)據(jù)原因有數(shù)據(jù)的自然變化、觀察系統(tǒng)行為的變化、數(shù)據(jù)收集錯誤。
異常值沒有精確的數(shù)學(xué)定義。必須依靠經(jīng)驗(yàn)、有關(guān)主題的知識和常識來確定數(shù)據(jù)點(diǎn)是否為異常值以及如何處理。
既然要做描述性統(tǒng)計(jì)分析,就一定要先學(xué)習(xí)統(tǒng)計(jì)學(xué)相關(guān)的基礎(chǔ)內(nèi)容,否則概念都不知道是無法下手操作的,甚至連對應(yīng)的三方庫都不知道哪里去找。
建議先看一下下面的內(nèi)容之后回來看如何進(jìn)行python操作,如果你已經(jīng)對統(tǒng)計(jì)有初步的了解了可以跳過這個(gè)部分。
導(dǎo)入應(yīng)用的三方包。
import mathimport statisticsimport numpy as npimport scipy.statsimport pandas as pd
隨機(jī)創(chuàng)建一些數(shù)據(jù),通常缺失值用 nan 表示。表示的方法有很多種,常用的有float('nan')、math.nan、np.nan。
x = [1.0, 5, 7.5, 4, 99.1]x_with_nan = [1.0, 5, 7.5, math.nan, 4, 99.1]
同時(shí)創(chuàng)建 np.ndarray 和 pd.Series 對象。
兩個(gè) NumPy 數(shù)組(y和y_with_nan)和兩個(gè) Pandas Series(z和z_with_nan),這些都是一維值序列。
y, y_with_nan = np.array(x), np.array(x_with_nan)z, z_with_nan = pd.Series(x), pd.Series(x_with_nan)yarray([ 8. , 1. , 2.5, 4. , 28. ])y_with_nanarray([ 8. , 1. , 2.5, nan, 4. , 28. ])z0 8.01 1.02 2.53 4.04 28.0dtype: float64z_with_nan0 8.01 1.02 2.53 NaN4 4.05 28.0dtype: float64
集中趨勢的度量顯示數(shù)據(jù)集的中心值或中間值。有幾種定義被認(rèn)為是數(shù)據(jù)集的中心。
上述三種均值的實(shí)現(xiàn)方法可以參考下面這篇內(nèi)容。
Mr數(shù)據(jù)楊:『迷你教程』數(shù)據(jù)分析師常用的三大均值統(tǒng)計(jì)方法0 贊同 · 0 評論文章
n = len(x)if n % 2: median_ = sorted(x)[round(0.5*(n-1))]else: x_ord, index = sorted(x), round(0.5 * n) median_ = 0.5 * (x_ord[index-1] + x_ord[index])median_7.5
u = [2, 3, 2, 8, 12]# 常規(guī)獲取方式mode_ = max((u.count(item), item) for item in set(u))[1]# statistics.mode() 和 statistics.multimode()模式mode_ = statistics.mode(u)mode_ = statistics.multimode(u)# scipy.stats.mode() 模式u = np.array(u)mode_ = scipy.stats.mode(u)mode_# Pandas Series對象.mode()模式u= pd.Series(u)u.mode()
# 常規(guī)獲取方式n = len(x)mean_ = sum(x) / nvar_ = sum((item - mean_)**2 for item in x) / (n - 1)# statistics.variance() 模式var_ = statistics.variance(x)var_ =statistics.variance(x_with_nan)# np.var() 或?.var() 模式 ddof(自由度)var_ = np.var(y, ddof=1)var_ = y.var(ddof=1)var_ = np.nanvar(y_with_nan, ddof=1) # 跳過 nan 值# Pandas Series對象.var()模式z.var(ddof=1) z_with_nan.var(ddof=1) # 默認(rèn)跳過nan值
# 常規(guī)獲取方式std_ = var_ ** 0.5# statistics.stdev() 模式,自動忽略 nan 值std_ = statistics.stdev(x)# np.std() 或??.std() 模式 ddof(自由度)var_ = np.std(y, ddof=1)var_ = y.std(ddof=1)var_ = np.std(y_with_nan, ddof=1) # 跳過 nan 值# Pandas Series對象.std()模式z.std(ddof=1)z_with_nan.std(ddof=1) # 默認(rèn)跳過nan值
'''左偏分布(負(fù)偏態(tài))中:mean(平均數(shù))<median(中位數(shù))<mode(眾數(shù))右偏分布(正偏態(tài))中:mode(眾數(shù))<median(中位數(shù))<mean(平均數(shù))'''# 常規(guī)獲取方式x = [1.0, 1, 6.5, 41, 28.5]n = len(x)mean_ = sum(x) / nvar_ = sum((item - mean_)**2 for item in x) / (n - 1)std_ = var_ ** 0.5skew_ = (sum((item - mean_)**3 for item in x)* n / ((n - 1) * (n - 2) * std_**3))skew_0.8025583716578066 # 數(shù)值為正則右側(cè)甩尾# scipy.stats.skew() 模式y(tǒng), y_with_nan = np.array(x), np.array(x_with_nan)scipy.stats.skew(y, bias=False)0.8025583716578066scipy.stats.skew(y_with_nan, bias=False)nan# Pandas Series對象.skew()模式z, z_with_nan = pd.Series(x), pd.Series(x_with_nan)z.skew()0.8025583716578066z_with_nan.skew()0.8025583716578066
# Python 3.8+# statistics.quantiles() 模式x = [-5.0, -1.1, 0.1, 2.0, 8.0, 12.8, 21.0, 25.8, 41.0]statistics.quantiles(x, n=2)[8.0]statistics.quantiles(x, n=4, method='inclusive')[0.1, 8.0, 21.0]# np.percentile() 模式y(tǒng) = np.array(x)np.percentile(y, 5)np.percentile(y, 95)np.percentile(y, [25, 50, 75])np.median(y)# np.nanpercentile() 忽略 nan值y_with_nan = np.insert(y, 2, np.nan)np.nanpercentile(y_with_nan, [25, 50, 75])# pd.Series對象有方法.quantile()z, z_with_nan = pd.Series(y), pd.Series(y_with_nan)z.quantile(0.05)z.quantile(0.95)z.quantile([0.25, 0.5, 0.75])z_with_nan.quantile([0.25, 0.5, 0.75])
# np.ptp() 模式np.ptp(y)np.ptp(z)np.ptp(y_with_nan)np.ptp(z_with_nan)'''不同的計(jì)算方式max() 、min() # Python 標(biāo)準(zhǔn)庫amax() 、amin() # NumPynanmax() 、nanmin() # NumPy 忽略 nan.max() 、 .min() # NumPy.max() 、.min() # Pandas 默認(rèn)忽略 nan 值'''
# scipy.stats.describe() 模塊'''nobs:數(shù)據(jù)集中的觀察或元素的數(shù)量minmax:具有數(shù)據(jù)集最小值和最大值的元組mean:數(shù)據(jù)集的平均值variance:數(shù)據(jù)集的方差skewness:數(shù)據(jù)集的偏度kurtosis:數(shù)據(jù)集的峰度'''result = scipy.stats.describe(y, ddof=1, bias=False)# Series對象有方法.describe()'''count:數(shù)據(jù)集中的元素?cái)?shù)量mean:數(shù)據(jù)集的平均值std:數(shù)據(jù)集的標(biāo)準(zhǔn)差min和max:數(shù)據(jù)集的最小值和最大值25%, 50%, and 75%:數(shù)據(jù)集的四分位數(shù)'''result = z.describe()
經(jīng)常需要檢查數(shù)據(jù)集中兩個(gè)變量的對應(yīng)元素之間的關(guān)系。假設(shè)有兩個(gè)變量 和 ,具有相同數(shù)量的元素 。讓 中的 ? 對應(yīng)于 中的 ?, 中的 ? 對應(yīng) 中的 ? 以此類推。
數(shù)據(jù)對之間相關(guān)性的度量:
衡量數(shù)據(jù)集之間相關(guān)性的兩個(gè)統(tǒng)計(jì)量是 協(xié)方差和相關(guān)系數(shù)。
x = list(range(-10, 11))y = [0, 2, 2, 2, 2, 3, 3, 6, 7, 4, 7, 6, 6, 9, 4, 5, 5, 10, 11, 12, 14]x_, y_ = np.array(x), np.array(y)x__, y__ = pd.Series(x_), pd.Series(y_)
協(xié)方差,樣本協(xié)方差是量化一對變量之間關(guān)系的強(qiáng)度和方向的度量。
# 純 Python 中計(jì)算協(xié)方差n = len(x)mean_x, mean_y = sum(x) / n, sum(y) / ncov_xy = (sum((x[k] - mean_x) * (y[k] - mean_y) for k in range(n))/ (n - 1))# NumPy 具有cov()模塊,直接返回協(xié)方差矩陣cov_matrix = np.cov(x_, y_)# PandasSeries 具有.cov()模塊,計(jì)算協(xié)方差cov_xy = x__.cov(y__)cov_xy = y__.cov(x__)
相關(guān)系數(shù)
關(guān)于相關(guān)系數(shù)有三種不同的計(jì)算方式。
這里舉例說明 連續(xù)數(shù)據(jù) & 連續(xù)數(shù)據(jù) 的pearsonr相關(guān)系數(shù)計(jì)算方式。
# Python 計(jì)算相關(guān)系數(shù)var_x = sum((item - mean_x)**2 for item in x) / (n - 1)var_y = sum((item - mean_y)**2 for item in y) / (n - 1)std_x, std_y = var_x ** 0.5, var_y ** 0.5r = cov_xy / (std_x * std_y)# scipy.stats.linregress()模塊result = scipy.stats.linregress(x_, y_)r = result.rvalue# scipy.stats具有pearsonr()模塊r, p = scipy.stats.pearsonr(x_, y_)# Numpy具有np.corrcoef()模塊corr_matrix = np.corrcoef(x_, y_)# Pandas的Series有.corr()模塊r = x__.corr(y__)r = y__.corr(x__)
統(tǒng)計(jì)學(xué)家經(jīng)常使用二維數(shù)據(jù),其中包括數(shù)據(jù)庫表、CSV 文件、Excel等電子表格。
關(guān)于這方面的數(shù)據(jù)處理應(yīng)用內(nèi)容會有很多,建議選擇一個(gè)自己擅長的就可以了,比如使用DataFrame進(jìn)行處理。
Mr數(shù)據(jù)楊:「Python 數(shù)據(jù)處理基礎(chǔ)」數(shù)據(jù)預(yù)處理連續(xù)變量10種常用方法
Mr數(shù)據(jù)楊:「Python 數(shù)據(jù)處理基礎(chǔ)」英文文本數(shù)據(jù)預(yù)處理操作的9種常用方法
Mr數(shù)據(jù)楊:「Python 數(shù)據(jù)處理基礎(chǔ)」數(shù)據(jù)特征處理標(biāo)準(zhǔn)化和歸一化應(yīng)用
Mr數(shù)據(jù)楊:「Python 數(shù)據(jù)處理基礎(chǔ)」數(shù)據(jù)重復(fù)值的統(tǒng)計(jì)和處理2種常用方法
Mr數(shù)據(jù)楊:「Python 數(shù)據(jù)處理基礎(chǔ)」數(shù)據(jù)預(yù)處理離散變量5種常用方法
Mr數(shù)據(jù)楊:「Python 數(shù)據(jù)處理基礎(chǔ)」數(shù)據(jù)的降維以及Scikit-learn實(shí)現(xiàn)的14種方式
Mr數(shù)據(jù)楊:「Python 數(shù)據(jù)處理基礎(chǔ)」數(shù)據(jù)沖突和樣本的選取和處理
Mr數(shù)據(jù)楊:「Python 數(shù)據(jù)處理基礎(chǔ)」中文文本數(shù)據(jù)預(yù)處理操作的8種常用方法
Mr數(shù)據(jù)楊:「Python 數(shù)據(jù)處理基礎(chǔ)」數(shù)據(jù)缺失值的可視化和處理8種常用方法
Mr數(shù)據(jù)楊:「Python 數(shù)據(jù)處理基礎(chǔ)」數(shù)據(jù)離群值的可視化和處理5種常用方法
Mr數(shù)據(jù)楊:「Python 數(shù)據(jù)處理基礎(chǔ)」數(shù)據(jù)特征處理離散化和二值化應(yīng)用
數(shù)據(jù)可視化的方法庫有很多之前也提到過,這里進(jìn)行一個(gè)未來匯總起來的一個(gè)數(shù)據(jù)可視化內(nèi)容列表。工作中需要的數(shù)據(jù)可視化庫都再這里面能知道對應(yīng)的制作方法。
數(shù)據(jù)可視化是可以讓外行人通過圖形的方式快速、直接的了解數(shù)據(jù)所表達(dá)的含義。