Python机器学习入门LLM Course:NumPy、Pandas、Scikit-learn

Python机器学习入门LLM Course:NumPy、Pandas、Scikit-learn

【免费下载链接】llm-course 通过提供路线图和Colab笔记本的课程,助您入门大型语言模型(LLMs)领域。 【免费下载链接】llm-course 项目地址: https://gitcode.com/GitHub_Trending/ll/llm-course

为什么LLM工程师必须掌握Python数据科学三件套?

你是否曾在LLM微调时因数据格式错误浪费数小时?是否在模型评估阶段因特征处理不当导致性能骤降?作为LLM领域的核心基础设施,NumPy(数值计算)、Pandas(数据处理)和Scikit-learn(机器学习工具库)构成了从数据预处理到模型部署的完整技术栈。本教程将通过LLM开发实战场景,系统讲解这三个库的核心功能与最佳实践,帮你突破数据处理瓶颈,显著提升模型训练效率。

读完本文你将掌握:

  • 用NumPy高效处理LLM训练数据的张量操作技巧
  • 基于Pandas的百万级指令数据集清洗与增强方案
  • Scikit-learn特征工程在LLM微调数据准备中的应用
  • 构建完整的LLM数据预处理流水线(附Colab可运行代码)

一、NumPy:LLM张量计算的基石

1.1 多维数组(ndarray)与LLM输入表示

NumPy的核心数据结构ndarray(N-dimensional array,多维数组)是所有LLM框架的基础数据格式。在Transformer模型中,文本经过标记化(Tokenization)后会转换为形状为[batch_size, sequence_length]的整数数组,而嵌入层(Embedding Layer)会将其映射为[batch_size, sequence_length, hidden_size]的浮点数张量。

import numpy as np

# 模拟LLM输入:batch_size=2, sequence_length=5
token_ids = np.array([[101, 2054, 1037, 3835, 102],  # 句子1: [CLS] hello world [SEP]
                      [101, 1996, 2003, 2070, 102]]) # 句子2: [CLS] i am here [SEP]
print(f"张量形状: {token_ids.shape}")
print(f"数据类型: {token_ids.dtype}")
print(f"元素总数: {token_ids.size}")

1.2 广播机制与LLM参数初始化

LLM的注意力机制(Attention Mechanism)大量使用广播(Broadcasting)操作。当进行不同形状数组的运算时,NumPy会自动扩展维度较小的数组以匹配较大数组的形状,这在实现缩放点积注意力(Scaled Dot-Product Attention)时尤为关键:

# 模拟注意力分数计算(Scaled Dot-Product Attention)
d_k = 64
Q = np.random.randn(2, 8, 10, d_k)  # [batch_size, num_heads, seq_len, d_k]
K = np.random.randn(2, 8, 12, d_k)  # [batch_size, num_heads, seq_len_k, d_k]
V = np.random.randn(2, 8, 12, d_k)  # [batch_size, num_heads, seq_len_k, d_k]

# 矩阵乘法 + 广播缩放
scores = np.matmul(Q, K.transpose(-2, -1)) / np.sqrt(d_k)  # [2,8,10,12]
weights = np.exp(scores) / np.sum(np.exp(scores), axis=-1, keepdims=True)
output = np.matmul(weights, V)  # [2,8,10,64]
print(f"注意力输出形状: {output.shape}")

1.3 向量化操作与LLM训练效率

向量化(Vectorization)是提升LLM训练速度的核心技巧。通过NumPy的内置函数替代Python循环,可将计算效率提升100倍以上,这在处理百万级token的训练数据时至关重要:

import time

# 非向量化实现(低效)
def naive_token_count(texts):
    count = 0
    for text in texts:
        count += len(text.split())
    return count

# 向量化实现(高效)
def vectorized_token_count(texts):
    return np.char.count(texts, ' ').sum() + len(texts)  # 空格数+句子数=单词数

# 性能对比
texts = np.array(["hello world"] * 1_000_000)

start = time.time()
print(f"非向量化结果: {naive_token_count(texts)}, 耗时: {time.time()-start:.2f}s")

start = time.time()
print(f"向量化结果: {vectorized_token_count(texts)}, 耗时: {time.time()-start:.2f}s")

二、Pandas:LLM数据集处理实战

2.1 DataFrame与LLM指令数据集结构

Pandas的DataFrame是处理LLM指令微调数据的标准格式。典型的SFT(Supervised Fine-Tuning)数据集包含instruction(指令)、input(输入)和output(输出)三列,Pandas提供了高效的行/列操作接口:

import pandas as pd

# 创建LLM指令数据集
data = {
    "instruction": ["解释什么是LLM", "总结以下文本", "翻译英文到中文"],
    "input": ["", "LLM是基于Transformer的大型语言模型...", "Hello world"],
    "output": [
        "大型语言模型(LLM)是参数量达数十亿的深度学习模型...",
        "本文介绍了LLM的基本原理...",
        "你好世界"
    ]
}

df = pd.DataFrame(data)
print("数据集基本信息:")
print(df.info())
print("\n数据集统计摘要:")
print(df.describe(include='all'))

2.2 数据清洗与LLM质量控制

低质量数据会导致LLM过拟合或产生幻觉(Hallucination)。Pandas提供了完整的数据清洗工具链,包括缺失值处理、重复数据删除和异常值检测:

# 模拟含噪声的LLM数据集
raw_data = pd.read_csv("llm_training_data.csv")

# 1. 处理缺失值
clean_data = raw_data.dropna(subset=["instruction", "output"])  # 关键列缺失直接删除
clean_data["input"] = clean_data["input"].fillna("")  # 输入可选,用空字符串填充

# 2. 去重处理
clean_data = clean_data.drop_duplicates(subset=["instruction", "input"])  # 基于指令和输入去重

# 3. 长度过滤(防止异常样本)
clean_data["instruction_length"] = clean_data["instruction"].str.len()
clean_data = clean_data[(clean_data["instruction_length"] > 5) & (clean_data["instruction_length"] < 200)]

# 4. 保存清洗后的数据
clean_data.to_json("clean_llm_data.json", orient="records", force_ascii=False)
print(f"原始样本数: {len(raw_data)}, 清洗后样本数: {len(clean_data)}")

2.3 数据增强与LLM鲁棒性提升

通过Pandas的向量化字符串操作,可以快速实现LLM数据增强,如随机插入、同义词替换等,提升模型的泛化能力:

import random
from synonyms import synonyms  # 中文同义词库

# 同义词替换增强
def augment_text(text, prob=0.2):
    words = text.split()
    augmented_words = []
    for word in words:
        if random.random() < prob and len(synonyms.nearby(word)) > 0:
            # 随机选择一个同义词
            augmented_words.append(synonyms.nearby(word)[0][0])
        else:
            augmented_words.append(word)
    return ' '.join(augmented_words)

# 向量化应用增强函数
df["augmented_output"] = df["output"].apply(augment_text)

# 生成对比样本
enhanced_data = pd.concat([df[["instruction", "input", "output"]],
                          df[["instruction", "input", "augmented_output"]].rename(columns={"augmented_output": "output"})],
                         ignore_index=True)

print(f"增强后样本数: {len(enhanced_data)}")

三、Scikit-learn:LLM特征工程与评估

3.1 文本向量化与LLM输入表示

虽然现代LLM使用预训练词嵌入(Embedding),但Scikit-learn的文本向量化方法仍是理解LLM底层原理的基础。TF-IDF和词袋模型(Bag-of-Words)可用于快速构建基线模型或进行特征重要性分析:

from sklearn.feature_extraction.text import TfidfVectorizer

# 准备LLM指令数据
corpus = df["instruction"].tolist()

# 初始化TF-IDF向量化器
vectorizer = TfidfVectorizer(max_features=5000,  # 保留 top 5000 特征词
                             ngram_range=(1, 2),  # 考虑1-gram和2-gram
                             stop_words="english")  # 移除停用词

# 拟合并转换文本数据
tfidf_matrix = vectorizer.fit_transform(corpus)

# 查看特征词与权重
feature_names = vectorizer.get_feature_names_out()
sample_idx = 0
sample_weights = tfidf_matrix[sample_idx].toarray().flatten()

top_features = pd.DataFrame({
    "feature": feature_names,
    "weight": sample_weights
}).sort_values("weight", ascending=False).head(10)

print(f"样本指令: {corpus[sample_idx]}")
print("Top TF-IDF特征词:\n", top_features)

3.2 模型评估与LLM性能优化

Scikit-learn提供了全面的评估指标,可用于LLM的量化评估,包括准确率、F1分数和混淆矩阵等。以下是评估LLM分类任务性能的标准流程:

from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
import matplotlib.pyplot as plt
import seaborn as sns

# 加载模型预测结果
results = pd.read_csv("llm_classification_results.csv")

y_true = results["true_label"]
y_pred = results["predicted_label"]

# 计算评估指标
accuracy = accuracy_score(y_true, y_pred)
report = classification_report(y_true, y_pred)

# 绘制混淆矩阵
cm = confusion_matrix(y_true, y_pred)
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", xticklabels=classes, yticklabels=classes)
plt.xlabel("Predicted Label")
plt.ylabel("True Label")
plt.title(f"LLM Classification Confusion Matrix (Accuracy: {accuracy:.2f})")
plt.savefig("llm_confusion_matrix.png")

print(f"准确率: {accuracy:.4f}")
print("分类报告:\n", report)

3.3 流水线构建与LLM工程化

Scikit-learn的Pipeline可以将文本预处理、特征提取和模型训练整合为一个可复用的工作流,这对LLM工程化至关重要:

from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split

# 加载LLM意图识别数据集
data = pd.read_csv("llm_intent_data.csv")
X = data["user_query"]
y = data["intent_label"]

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 构建流水线
llm_intent_pipeline = Pipeline([
    ("vectorizer", CountVectorizer(ngram_range=(1, 2), stop_words="english")),  # 文本向量化
    ("classifier", MultinomialNB(alpha=0.1))  # 分类器
])

# 训练模型
llm_intent_pipeline.fit(X_train, y_train)

# 评估性能
train_accuracy = llm_intent_pipeline.score(X_train, y_train)
test_accuracy = llm_intent_pipeline.score(X_test, y_test)

print(f"训练集准确率: {train_accuracy:.4f}")
print(f"测试集准确率: {test_accuracy:.4f}")

# 保存模型
import joblib
joblib.dump(llm_intent_pipeline, "llm_intent_pipeline.pkl")

四、LLM开发全流程实战:从数据到部署

4.1 数据预处理流水线

以下是一个完整的LLM微调数据预处理流水线,整合了NumPy、Pandas和Scikit-learn的核心功能:

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder

# 1. 加载原始数据
dataset = pd.read_json("raw_llm_data.json", orient="records")

# 2. 数据清洗
dataset = dataset.dropna(subset=["instruction", "response"])
dataset = dataset.drop_duplicates()

dataset["instruction_length"] = dataset["instruction"].str.len()
dataset = dataset[dataset["instruction_length"] < 300]

# 3. 数据划分
train_data, val_data = train_test_split(dataset, test_size=0.15, random_state=42)

# 4. 格式转换(适配LLM微调框架)
train_data["text"] = train_data.apply(lambda x: f"### Instruction: {x['instruction']}\n### Input: {x['input']}\n### Response: {x['response']}", axis=1)
val_data["text"] = val_data.apply(lambda x: f"### Instruction: {x['instruction']}\n### Input: {x['input']}\n### Response: {x['response']}", axis=1)

# 5. 保存为文本文件
train_data["text"].to_csv("train.txt", index=False, header=False)
val_data["text"].to_csv("validation.txt", index=False, header=False)

print(f"训练集样本数: {len(train_data)}, 验证集样本数: {len(val_data)}")

4.2 性能优化与最佳实践

操作场景低效实现高效实现性能提升
文本长度统计for text in texts: len(text.split())texts.str.split().str.len().sum()~100x
数据去重嵌套循环比较drop_duplicates(subset)~1000x
特征缩放手动计算均值方差StandardScaler()~50x
数据合并for循环appendpd.concat()~500x
条件筛选for循环if判断boolean indexing~200x

4.3 常见问题与解决方案

Q1: 处理超大LLM数据集时内存不足怎么办?

A1: 使用Pandas的分块处理功能:

chunk_iter = pd.read_csv("100m_llm_data.csv", chunksize=10000)  # 每次读取10k行
for chunk in chunk_iter:
    process_chunk(chunk)  # 逐块处理
Q2: 如何加速NumPy矩阵运算?

A2: 启用MKL加速或使用CuPy(GPU加速的NumPy替代品):

import cupy as cp  # 需安装cupy

# NumPy数组转换为CuPy数组
gpu_array = cp.array(numpy_array)
# GPU上执行计算
gpu_result = cp.matmul(gpu_array, gpu_array.T)
# 结果转回NumPy
numpy_result = gpu_result.get()
Q3: Scikit-learn模型如何与LLM框架集成?

A3: 导出特征向量供LLM使用:

# 提取特征向量
vectorizer = joblib.load("llm_vectorizer.pkl")
llm_features = vectorizer.transform(texts)

# 转换为NumPy数组供PyTorch使用
import torch
llm_feature_tensor = torch.tensor(llm_features.toarray(), dtype=torch.float32)

五、总结与进阶路线

NumPy、Pandas和Scikit-learn构成了LLM开发的技术基石。掌握这些工具不仅能显著提升开发效率,更能帮助你深入理解LLM的底层原理。建议通过以下路径继续提升:

  1. 基础巩固:完成NumPy官方教程和Pandas实战指南
  2. LLM进阶:学习用scikit-learn构建RAG(检索增强生成)系统的向量数据库
  3. 工程化实践:使用Dask处理超大规模LLM数据集(100M+样本)
  4. 性能优化:掌握NumPy向量化技巧和Pandas查询优化

通过本文的代码和最佳实践,你现在已经具备了处理LLM数据的核心能力。下一步,尝试用这些工具优化你的LLM微调流程,或构建一个完整的RAG应用吧!

提示:关注本项目后续教程,我们将深入讲解如何用PyTorch和Hugging Face Transformers库实现LLM微调与部署。

【免费下载链接】llm-course 通过提供路线图和Colab笔记本的课程,助您入门大型语言模型(LLMs)领域。 【免费下载链接】llm-course 项目地址: https://gitcode.com/GitHub_Trending/ll/llm-course

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值