Python数据分析全栈入门指南:从基础库到机器学习实战

一、NumPy:高效数值计算基石

NumPy是Python科学计算的基础库,其核心N-dimensional array对象提供比原生列表快50-100倍的数值运算能力。掌握以下核心操作可覆盖80%的数据处理场景:

1.1 数组创建与操作

  1. import numpy as np
  2. # 创建多维数组
  3. arr = np.array([[1,2,3],[4,5,6]]) # 显式创建
  4. zeros = np.zeros((3,4)) # 零矩阵
  5. range_arr = np.arange(0,10,2) # 步长生成
  6. # 索引与切片
  7. print(arr[1,2]) # 输出6(二维索引)
  8. print(arr[:,1]) # 输出第2列[2,5]
  9. print(arr[::-1]) # 数组反转

1.2 维度变换与组合

  1. # 改变维度
  2. reshaped = arr.reshape(3,2) # 必须保证元素总数不变
  3. flattened = arr.flatten() # 降维为一维
  4. # 数组拼接
  5. vstack = np.vstack([arr, [[7,8,9]]]) # 垂直堆叠
  6. hstack = np.hstack([arr, arr*10]) # 水平堆叠

1.3 统计与数学运算
核心统计函数:

  • np.mean()/np.median():均值/中位数
  • np.std()/np.var():标准差/方差
  • np.corrcoef():相关系数矩阵

随机模块应用示例:

  1. # 生成正态分布随机数
  2. normal_dist = np.random.normal(loc=0, scale=1, size=1000)
  3. # 随机排序
  4. data = np.array([5,2,9,1])
  5. shuffled = np.random.permutation(data)

二、Pandas:结构化数据处理利器

Pandas提供DataFrame数据结构,可高效处理表格型数据。掌握以下技能可应对90%的数据清洗需求:

2.1 数据加载与预览

  1. import pandas as pd
  2. # 读取CSV/Excel文件
  3. df = pd.read_csv('data.csv', encoding='utf-8')
  4. excel_df = pd.read_excel('report.xlsx', sheet_name='Sales')
  5. # 数据预览
  6. print(df.head(3)) # 前3行
  7. print(df.info()) # 数据类型与缺失值统计
  8. print(df.describe()) # 数值型列统计摘要

2.2 数据清洗与转换

  1. # 处理缺失值
  2. df.fillna(0, inplace=True) # 填充0
  3. df.dropna(axis=0, how='any') # 删除含缺失值的行
  4. # 类型转换
  5. df['Date'] = pd.to_datetime(df['Date']) # 字符串转日期
  6. df['Price'] = df['Price'].astype('float') # 类型转换
  7. # 离散化处理
  8. df['Age_Group'] = pd.cut(df['Age'], bins=[0,18,35,60,100],
  9. labels=['Child','Youth','Adult','Senior'])

2.3 高级操作技巧

  1. # 分组聚合
  2. grouped = df.groupby('Region')['Sales'].agg(['sum','mean','count'])
  3. # 多表合并
  4. merged = pd.merge(df1, df2, on='CustomerID', how='left')
  5. # 透视表
  6. pivot = pd.pivot_table(df, values='Sales', index='Date', columns='Product')

三、Matplotlib:数据可视化核心库

掌握Matplotlib可创建出版级图表,关键在于理解Figure-Axes对象模型:

3.1 基础图表绘制

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # 折线图
  4. x = np.linspace(0, 10, 100)
  5. y = np.sin(x)
  6. plt.figure(figsize=(8,4))
  7. plt.plot(x, y, label='sin(x)', color='red', linewidth=2)
  8. plt.title('Sine Wave')
  9. plt.xlabel('X Axis')
  10. plt.ylabel('Y Axis')
  11. plt.legend()
  12. plt.grid(True)
  13. plt.show()

3.2 多子图布局

  1. fig, axes = plt.subplots(2, 2, figsize=(10,8))
  2. axes[0,0].hist(np.random.normal(0,1,1000), bins=30)
  3. axes[0,1].scatter(np.random.rand(50), np.random.rand(50))
  4. axes[1,0].plot(np.cumsum(np.random.randn(100)))
  5. axes[1,1].boxplot([np.random.normal(0,std,100) for std in range(1,4)])
  6. plt.tight_layout()

3.3 3D可视化

  1. from mpl_toolkits.mplot3d import Axes3D
  2. fig = plt.figure(figsize=(10,7))
  3. ax = fig.add_subplot(111, projection='3d')
  4. x = np.linspace(-5, 5, 100)
  5. y = np.linspace(-5, 5, 100)
  6. X, Y = np.meshgrid(x, y)
  7. Z = np.sin(np.sqrt(X**2 + Y**2))
  8. ax.plot_surface(X, Y, Z, cmap='viridis')
  9. ax.set_zlim(-1.5, 1.5)

四、Scikit-learn:机器学习工程化实践

掌握机器学习工作流比记忆算法更重要,以下是标准项目流程:

4.1 数据准备与划分

  1. from sklearn.model_selection import train_test_split
  2. from sklearn.preprocessing import StandardScaler
  3. # 数据标准化
  4. scaler = StandardScaler()
  5. X_scaled = scaler.fit_transform(X)
  6. # 训练测试集划分
  7. X_train, X_test, y_train, y_test = train_test_split(
  8. X_scaled, y, test_size=0.3, random_state=42
  9. )

4.2 模型训练与评估

  1. from sklearn.linear_model import LogisticRegression
  2. from sklearn.metrics import classification_report, roc_auc_score
  3. # 模型训练
  4. model = LogisticRegression(max_iter=1000)
  5. model.fit(X_train, y_train)
  6. # 预测评估
  7. y_pred = model.predict(X_test)
  8. print(classification_report(y_test, y_pred))
  9. print("AUC Score:", roc_auc_score(y_test, model.predict_proba(X_test)[:,1]))

4.3 参数调优与交叉验证

  1. from sklearn.model_selection import GridSearchCV
  2. # 定义参数网格
  3. param_grid = {
  4. 'C': [0.001, 0.01, 0.1, 1, 10],
  5. 'penalty': ['l1', 'l2']
  6. }
  7. # 网格搜索
  8. grid_search = GridSearchCV(
  9. LogisticRegression(solver='liblinear', max_iter=1000),
  10. param_grid, cv=5, scoring='roc_auc'
  11. )
  12. grid_search.fit(X_train, y_train)
  13. # 最佳参数
  14. print("Best Parameters:", grid_search.best_params_)

五、学习资源推荐

  1. 官方文档:NumPy/Pandas/Matplotlib/Scikit-learn官方文档(中英文版)
  2. 实践平台:Kaggle入门竞赛(Titanic/House Prices等)
  3. 交互学习:DataCamp/Coursera数据分析专项课程
  4. 开源项目:GitHub上star>1k的数据分析项目

掌握这四大库后,可进一步学习:

  • 深度学习框架(TensorFlow/PyTorch)
  • 大数据处理(Dask/PySpark)
  • 可视化增强(Seaborn/Plotly)
  • 自动化机器学习(AutoML工具)

建议每天投入2小时,通过”理论学习→代码实践→项目复现”的循环,3个月内可达到初级数据分析师水平。附赠完整学习文档包含:各库API速查表、典型项目案例代码、常见错误解决方案。