近年来,Python在统计分析和数据可视化领域取得了显著进展,许多新的库和工具被引入,使得Python在统计分析方面的能力得到了大幅提升。虽然R和SAS仍然是统计学领域的传统强手,但Python凭借其灵活性和强大的生态系统,已经成为数据科学家的首选工具之一。
1. 数据可视化:Plotnine 和 Seaborn
虽然 plotnine 仍然是一个强大的工具,模仿了R中的 ggplot2,但 seaborn 和 matplotlib 仍然是Python中最常用的可视化库。seaborn 提供了更简洁的API,并且与 pandas 数据框无缝集成。
使用 Seaborn 绘制散点图和回归线
python
import seaborn as snsimport matplotlib.pyplot as plt# 使用 seaborn 绘制散点图和回归线sns.lmplot(x='mpg', y='wt', data=df, hue='vs', ci=95)plt.show()使用 Seaborn 进行分面绘图
python
# 使用 seaborn 进行分面绘图sns.lmplot(x='mpg', y='wt', data=df, col='vs', row='gear', ci=95)plt.show()
2. 统计分析:Pingouin 和 Scipy
Pingouin 是一个新兴的统计库,提供了简单易用的API来进行常见的统计分析。它比 scipy 和 statsmodels 更易于使用,尤其是在进行假设检验时。
正态性检验
python
import pingouin as pg# 正态性检验pg.normality(df['wt'])两独立样本 t 检验
python
# 两独立样本 t 检验pg.ttest(df.loc[df['vs'] == '0', 'wt'], df.loc[df['vs'] == '1', 'wt'])方差分析 (ANOVA)
python
# 方差分析pg.anova(data=df, dv='wt', between='gear')多重比较
python
# 多重比较pg.pairwise_tukey(data=df, dv='wt', between='gear')
3. 相关分析
Pingouin 也提供了简单的方法来计算相关性。
Pearson 相关
python
# Pearson 相关pg.corr(df['wt'], df['mpg'])Spearman 相关
python
# Spearman 相关pg.corr(df['wt'], df['mpg'], method='spearman')
4. 回归分析:Statsmodels 和 Scikit-learn
statsmodels 仍然是进行回归分析的首选工具,但 scikit-learn 也提供了强大的回归模型,尤其是在机器学习的背景下。
多重线性回归
python
import statsmodels.api as sm# 多重线性回归model = sm.OLS(df['wt'], sm.add_constant(df[['mpg', 'cyl']]))results = model.fit()print(results.summary())Logistic 回归
python
# Logistic 回归logit_model = sm.Logit(df['vs'], sm.add_constant(df[['mpg', 'am']]))logit_results = logit_model.fit()print(logit_results.summary())泊松回归
python
# 泊松回归poisson_model = sm.Poisson(df['count'], sm.add_constant(df[['mpg', 'vs']]))poisson_results = poisson_model.fit()print(poisson_results.summary())
5. 数据处理:Pandas 和 NumPy
pandas 和 numpy 仍然是数据处理的核心工具。pandas 提供了强大的数据操作功能,而 numpy 则提供了高效的数值计算。
数据描述
python
# 数据描述df.describe()分类变量的频数统计
python
# 分类变量的频数统计df['vs'].value_counts()缺失值处理
python
# 缺失值处理df.isnull().sum()
6. 其他统计检验
卡方检验
python
# 卡方检验pg.chi2_independence(df, x='vs', y='am')Fisher 确切概率法
python
# Fisher 确切概率法pg.fisher_exact(df, x='vs', y='am')
7. 最新趋势:自动化机器学习 (AutoML)
近年来,自动化机器学习(AutoML)工具如 TPOT 和 Auto-Sklearn 变得越来越流行。这些工具可以自动选择最佳的机器学习模型和超参数,大大简化了建模过程。
python
from tpot import TPOTRegressor# 使用 TPOT 进行自动化机器学习tpot = TPOTRegressor(generations=5, population_size=50, verbosity=2)tpot.fit(df[['mpg', 'cyl']], df['wt'])print(tpot.score(df[['mpg', 'cyl']], df['wt']))
总结
Python 在统计分析和数据可视化方面的能力已经得到了显著提升。虽然 plotnine 仍然是一个强大的工具,但 seaborn 和 pingouin 等库提供了更简洁和强大的功能。statsmodels 和 scikit-learn 仍然是回归分析和机器学习的首选工具,而 pandas 和 numpy 则继续在数据处理方面发挥着核心作用。随着 AutoML 工具的兴起,数据科学家可以更加专注于问题的解决,而不是模型的调参。
更多相关技术内容咨询欢迎前往并持续关注好学星城论坛了解详情。
想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!