Debian Python如何进行数据分析和可视化
导读:在 Debian 上使用 Python 进行数据分析与可视化 一 环境准备与核心工具 更新系统并安装基础工具: sudo apt update && sudo apt install python3 python3-pi...
在 Debian 上使用 Python 进行数据分析与可视化
一 环境准备与核心工具
- 更新系统并安装基础工具:
- sudo apt update & & sudo apt install python3 python3-pip
- 建议优先使用虚拟环境隔离依赖:
- python3 -m venv .venv & & source .venv/bin/activate
- 安装常用库(pip 安装更全更及时):
- pip install pandas numpy matplotlib seaborn scikit-learn
- 可选系统包(Debian 仓库提供,稳定但版本可能偏旧):
- sudo apt install python3-numpy python3-pandas python3-matplotlib
- 交互式开发推荐安装 Jupyter Notebook:
- pip install jupyter & & jupyter notebook
二 标准工作流程
- 数据收集:从 CSV/Excel/数据库/API 导入,例如:pd.read_csv(‘data.csv’)
- 数据清洗:处理缺失值(均值/众数填充或删除)、重复值(drop_duplicates)、异常值(统计或可视化识别)
- 数据探索:使用 describe()、分组聚合、相关性分析了解分布与关系
- 数据建模:用 scikit-learn 进行分类/回归/聚类等任务
- 结果评估:交叉验证、混淆矩阵、准确率等指标
- 结果展示:用 Matplotlib/Seaborn 生成图表,或导出 HTML/Notebook 报告
三 完整示例 Titanic 入门分析
- 目标:读取数据、清洗缺失、EDA 与可视化、训练简单模型并评估
- 代码示例(可直接在 Jupyter/终端运行):
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score # 1) 读取数据 df = pd.read_csv('titanic.csv') # 请确保同目录下有该文件 # 2) 数据清洗 df['Age'].fillna(df['Age'].mean(), inplace=True) df['Embarked'].fillna(df['Embarked'].mode()[0], inplace=True) df.drop_duplicates(inplace=True) # 3) 描述性统计 print(df.describe(include='all').to_string()) # 4) 可视化:生存率按性别 sns.barplot(x='Sex', y='Survived', data=df) plt.title('Survival Rate by Gender') plt.ylabel('Survival Rate') plt.show() # 5) 可视化:年龄分布箱线图 sns.boxplot(x='Pclass', y='Age', hue='Sex', data=df) plt.title('Age Distribution by Class and Sex') plt.show() # 6) 建模与评估 features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare'] df = pd.get_dummies(df, columns=['Sex'], drop_first=True) # One-hot 编码 X = df[features] y = df['Survived'] X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y) clf = DecisionTreeClassifier(random_state=42) clf.fit(X_tr, y_tr) preds = clf.predict(X_te) print(f'Accuracy: { accuracy_score(y_te, preds):.3f} ') - 说明:示例使用了 Pandas 清洗与统计、Seaborn/Matplotlib 可视化、scikit-learn 训练决策树并输出准确率。
四 进阶与扩展
- 图数据分析:安装 graph-tool(Debian 源或 pip,注意依赖与编译要求)
- sudo apt install python3-graph-tool # 或按项目需求使用 pip 安装
- 统计建模与时间序列:statsmodels(回归、假设检验、时间序列)
- pip install statsmodels
- 大数据与并行:Dask(分块计算、延迟执行)
- pip install dask[complete]
五 常见问题与建议
- 依赖与环境:优先使用 venv/conda 隔离;避免系统 Python 与 pip 混用引发冲突
- 中文与字体:在 Matplotlib 中设置中文字体,例如:
- import matplotlib; matplotlib.rcParams[‘font.sans-serif’] = [‘Noto Sans CJK JP’]; matplotlib.rcParams[‘axes.unicode_minus’] = False
- 大数据处理:数据超出内存时考虑 Dask 或 PySpark 替代 Pandas 进行分块与分布式计算
- 版本兼容:确保 Python≥3.7(如 scikit-learn 要求),库版本之间保持兼容
- 可视化导出:在 Jupyter 中使用 %matplotlib inline;脚本中 plt.savefig(‘fig.png’, dpi=150, bbox_inches=‘tight’)
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Debian Python如何进行数据分析和可视化
本文地址: https://pptw.com/jishu/771635.html
