Python數(shù)據(jù)分析中常見(jiàn)統(tǒng)計(jì)方法詳解
數(shù)據(jù)分析是現(xiàn)代社會(huì)中不可或缺的一部分,通過(guò)對(duì)數(shù)據(jù)的統(tǒng)計(jì)和分析,我們可以得出有用的信息和見(jiàn)解,支持決策和解決問(wèn)題。本文將介紹在 Python 中常見(jiàn)的數(shù)據(jù)統(tǒng)計(jì)方法,包括描述性統(tǒng)計(jì)、假設(shè)檢驗(yàn)、回歸分析等,并提供詳細(xì)的示例代碼。
描述性統(tǒng)計(jì)
描述性統(tǒng)計(jì)是數(shù)據(jù)分析的第一步,它幫助了解數(shù)據(jù)的基本特征。以下是一些常見(jiàn)的描述性統(tǒng)計(jì)方法:
1. 平均值(均值)
平均值是數(shù)據(jù)集中所有數(shù)據(jù)的總和除以數(shù)據(jù)點(diǎn)的數(shù)量,用于衡量數(shù)據(jù)的集中趨勢(shì)。
import numpy as np
data = [10, 20, 30, 40, 50]
mean = np.mean(data)
print("平均值:", mean)
2. 中位數(shù)
中位數(shù)是數(shù)據(jù)集中的中間值,將數(shù)據(jù)排序后位于中間位置的值。
import numpy as np
data = [10, 20, 30, 40, 50]
median = np.median(data)
print("中位數(shù):", median)
3. 眾數(shù)
眾數(shù)是數(shù)據(jù)集中出現(xiàn)次數(shù)最多的值。
from statistics import mode
data = [10, 20, 30, 20, 50, 20]
mode_value = mode(data)
print("眾數(shù):", mode_value)
4. 標(biāo)準(zhǔn)差和方差
標(biāo)準(zhǔn)差和方差度量了數(shù)據(jù)的離散程度,標(biāo)準(zhǔn)差是方差的平方根。
import numpy as np
data = [10, 20, 30, 40, 50]
std_deviation = np.std(data)
variance = np.var(data)
print("標(biāo)準(zhǔn)差:", std_deviation)
print("方差:", variance)
5. 百分位數(shù)
百分位數(shù)表示數(shù)據(jù)中小于或等于給定百分比的觀察值。常見(jiàn)的百分位數(shù)包括第25、第50和第75百分位數(shù),分別對(duì)應(yīng)于數(shù)據(jù)的下四分位數(shù)、中位數(shù)和上四分位數(shù)。
import numpy as np
data = [10, 20, 30, 40, 50]
q1 = np.percentile(data, 25)
median = np.percentile(data, 50)
q3 = np.percentile(data, 75)
print("下四分位數(shù)(Q1):", q1)
print("中位數(shù):", median)
print("上四分位數(shù)(Q3):", q3)
假設(shè)檢驗(yàn)
假設(shè)檢驗(yàn)是用于驗(yàn)證關(guān)于總體統(tǒng)計(jì)特征的假設(shè)的方法。以下是一些常見(jiàn)的假設(shè)檢驗(yàn)方法:
1. t-檢驗(yàn)
t-檢驗(yàn)用于比較兩組數(shù)據(jù)之間的均值是否具有統(tǒng)計(jì)顯著性差異。
import scipy.stats as stats
group1 = [25, 30, 35, 40, 45]
group2 = [20, 28, 32, 38, 42]
t_statistic, p_value = stats.ttest_ind(group1, group2)
print("t-統(tǒng)計(jì)量:", t_statistic)
print("p-值:", p_value)
2. 卡方檢驗(yàn)
卡方檢驗(yàn)用于確定兩個(gè)分類變量之間是否存在相關(guān)性。
import scipy.stats as stats
observed = [[10, 20], [30, 40]]
chi2, p, dof, expected = stats.chi2_contingency(observed)
print("卡方統(tǒng)計(jì)量:", chi2)
print("p-值:", p)
3. 方差分析
方差分析用于比較多個(gè)組之間的均值是否存在統(tǒng)計(jì)顯著性差異。
import scipy.stats as stats
group1 = [25, 30, 35, 40, 45]
group2 = [20, 28, 32, 38, 42]
group3 = [15, 18, 25, 30, 35]
f_statistic, p_value = stats.f_oneway(group1, group2, group3)
print("F-統(tǒng)計(jì)量:", f_statistic)
print("p-值:", p_value)
回歸分析
回歸分析用于探究變量之間的關(guān)系,其中最常見(jiàn)的是線性回歸。
線性回歸
線性回歸用于擬合數(shù)據(jù)并確定自變量與因變量之間的線性關(guān)系。
import numpy as np
from scipy.stats import linregress
import matplotlib.pyplot as plt
???????x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 5, 4, 5])
slope, intercept, r_value, p_value, std_err = linregress(x, y)
plt.scatter(x, y)
plt.plot(x, slope * x + intercept, color='red')
plt.xlabel('自變量')
plt.ylabel('因變量')
plt.show()
print("斜率:", slope)
print("截距:", intercept)
print("相關(guān)系數(shù):", r_value)
print("p-值:", p_value)數(shù)據(jù)可視化
數(shù)據(jù)可視化是數(shù)據(jù)分析的重要部分,它可以幫助更好地理解數(shù)據(jù)和趨勢(shì)。
1. 直方圖
直方圖用于展示數(shù)據(jù)的分布情況。
import numpy as np
import matplotlib.pyplot as plt
data = np.random.randn(1000) # 生成隨機(jī)數(shù)據(jù)
plt.hist(data, bins=20, density=True, alpha=0.6, color='g')
plt.xlabel('值')
plt.ylabel('頻率')
plt.title('直方圖')
plt.show()
2. 散點(diǎn)圖
散點(diǎn)圖用于展示兩個(gè)變量之間的關(guān)系。
import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn(100)
y = 2 * x + np.random.randn(100) # 創(chuàng)建線性關(guān)系
plt.scatter(x, y, marker='o', color='b', alpha=0.6)
plt.xlabel('自變量')
plt.ylabel('因變量')
plt.title('散點(diǎn)圖')
plt.show()
以上只是數(shù)據(jù)分析中常見(jiàn)的一些統(tǒng)計(jì)方法和數(shù)據(jù)可視化技巧的示例,實(shí)際應(yīng)用中可能需要根據(jù)具體問(wèn)題選擇合適的方法。
到此這篇關(guān)于Python數(shù)據(jù)分析中常見(jiàn)統(tǒng)計(jì)方法詳解的文章就介紹到這了,更多相關(guān)Python數(shù)據(jù)分析內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 中的paramiko模塊簡(jiǎn)介及安裝過(guò)程
這篇文章主要介紹了python 中的paramiko模塊簡(jiǎn)介及安裝過(guò)程,通過(guò)實(shí)例詳解給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-02-02
從基礎(chǔ)到高級(jí)詳解Python時(shí)區(qū)處理的完全指南
在全球化的數(shù)字時(shí)代,時(shí)區(qū)處理已成為系統(tǒng)開(kāi)發(fā)的關(guān)鍵挑戰(zhàn),本文將深入解析Python時(shí)區(qū)處理技術(shù)體系,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2025-08-08
利用Python實(shí)現(xiàn)某OA系統(tǒng)的自動(dòng)定位功能
這篇文章主要介紹了利用Python實(shí)現(xiàn)某OA系統(tǒng)的自動(dòng)定位功能,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
python2與python3的print及字符串格式化小結(jié)
最近一直在用python寫(xiě)程序,對(duì)于python的print一直很惱火,老是不按照預(yù)期輸出。今天特來(lái)總結(jié)一樣print和format,也希望能幫助大家徹底理解它們2018-11-11
Python使用Python-docx庫(kù)實(shí)現(xiàn)Word文檔自動(dòng)化
在日常辦公中,Word文檔處理是高頻需求,無(wú)論是生成報(bào)告、合同,還是批量修改文檔內(nèi)容,手動(dòng)操作效率低下且易出錯(cuò),Python-docx作為Python生態(tài)中處理.docx文件的王牌庫(kù),本文將帶您全面掌握該庫(kù)的核心功能,并附實(shí)戰(zhàn)代碼示例,需要的朋友可以參考下2025-12-12
關(guān)于torch.scatter與torch_scatter庫(kù)的使用整理
這篇文章主要介紹了關(guān)于torch.scatter與torch_scatter庫(kù)的使用整理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

