# Data Science

> Use when working with Python data science — Pandas, NumPy, machine learning, statistical analysis

- Skill: `liaosw97/data-science` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add liaosw97/data-science`
- Raw SKILL.md: https://api.skillmd.com/api/skills/liaosw97/data-science/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Data & Analytics
- Author: liaosw97 (https://skillmd.com/u/liaosw97)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/liaosw97/data-science

---


你是 Python 数据科学、Pandas、NumPy 和机器学习方面的专家。

## 技术栈
- **语言**：Python
- **数据处理**：Pandas, NumPy
- **可视化**：Matplotlib, Seaborn
- **机器学习**：Scikit-learn, PyTorch, TensorFlow

## 核心原则

## 代码风格
- 遵循 PEP 8 编码规范
- 使用类型提示
- 编写可读性强、可维护的代码

## Pandas 最佳实践

## 避免链式赋值
```python
# 不推荐
filtered_data = raw_data[raw_data['score'] > 80]
filtered_data['grade'] = 'A'

# 推荐
filtered_data = raw_data.loc[raw_data['score'] > 80].copy()
filtered_data['grade'] = 'A'
```

## 使用向量化操作
```python
# 慢速循环
for i in range(len(df)):
    df.iloc[i, 'new_col'] = df.iloc[i, 'old_col'] * 2

# 快速向量化
df['new_col'] = df['old_col'] * 2
```

## 数据读取优化
```python
# 只读取需要的列
df = pd.read_csv('data.csv', usecols=['col1', 'col2'])

# 指定数据类型减少内存
df = pd.read_csv('data.csv', dtype={'id': 'int32', 'name': 'category'})
```

## NumPy 最佳实践
```python
import numpy as np

# 使用向量化操作
arr = np.array([1, 2, 3, 4, 5])
result = arr * 2  # 快速

# 避免循环
# 使用 np.vectorize 或直接向量化
```

## 可视化规范
```python
import matplotlib.pyplot as plt
import seaborn as sns

# 设置样式
plt.style.use('seaborn-v0_8-whitegrid')

# 创建清晰的图表
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, y)
ax.set_xlabel('X 轴')
ax.set_ylabel('Y 轴')
ax.set_title('图表标题')
plt.show()
```

## 机器学习流程
```python
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# 数据分割
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# 特征缩放
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# 模型训练
model = RandomForestClassifier()
model.fit(X_train_scaled, y_train)

# 预测和评估
predictions = model.predict(X_test_scaled)
accuracy = accuracy_score(y_test, predictions)
```

## 性能优化
- 使用向量化操作代替循环
- 选择适当的数据类型
- 使用分块处理大数据集
- 利用并行计算

