人工智能训练师的核心任务之一是数据采集和处理,确保 AI 训练数据的质量、完整性和多样性。数据的好坏直接决定了 AI 模型的性能,因此需要从数据采集、数据清洗、数据增强、数据存储与管理等多个方面进行优化。
本指南详细介绍 AI 训练数据的采集、预处理、增强、存储等关键步骤,并提供完整的 Python 代码示例。
1. 数据采集
数据采集是 AI 训练的第一步,来源主要包括:
- 网页爬取(Scrapy、BeautifulSoup)
- API 数据抓取(Twitter API、OpenAI API)
- 数据库提取(SQL、MongoDB)
- 传感器/物联网数据(IoT 设备)
- 合成数据生成(数据增强、GPT 生成)
1.1 网页爬取(BeautifulSoup)
安装依赖
pip install requests beautifulsoup4
爬取新闻数据示例
import requests
from bs4 import BeautifulSoup
def scrape_news(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# 提取新闻标题和正文
title = soup.find("h1").text
paragraphs = soup.find_all("p")
content = " ".join([p.text for p in paragraphs])
return {"title": title, "content": content}
news_data = scrape_news("https://example-news-website.com/article")
print(news_data)
1.2 API 数据采集(Twitter API)
安装 Tweepy
pip install tweepy
获取 Twitter 数据
import tweepy
# Twitter API 凭据
API_KEY = "your_api_key"
API_SECRET = "your_api_secret"
ACCESS_TOKEN = "your_access_token"
ACCESS_SECRET = "your_access_secret"
# 认证 Twitter API
auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)
# 采集推文
tweets = api.search_tweets(q="AI", count=10)
for tweet in tweets:
print(tweet.text)
1.3 数据库采集(SQL 提取)
安装 MySQL 依赖
pip install mysql-connector-python
从数据库提取数据
import mysql.connector
conn = mysql.connector.connect(
host="localhost", user="root", password="password", database="ai_data"
)
cursor = conn.cursor()
cursor.execute("SELECT * FROM training_data")
rows = cursor.fetchall()
for row in rows:
print(row)
conn.close()
1.4 传感器 / 物联网数据采集
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 4 # 传感器连接的 GPIO 端口
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
print(f"温度: {temperature}°C, 湿度: {humidity}%")
2. 数据预处理
在采集到原始数据后,需要进行清洗、去重、标准化等处理。
2.1 数据清洗
去除 HTML / 特殊字符
import re
from bs4 import BeautifulSoup
def clean_text(text):
text = BeautifulSoup(text, "lxml").text # 去除 HTML
text = re.sub(r"[^a-zA-Z0-9\s]", "", text) # 去除特殊字符
return text.lower()
cleaned_text = clean_text("<p>Hello, AI!</p>")
print(cleaned_text) # 输出: hello ai
去除停用词
import nltk
from nltk.corpus import stopwords
nltk.download("stopwords")
stop_words = set(stopwords.words("english"))
def remove_stopwords(text):
words = text.split()
return " ".join([word for word in words if word.lower() not in stop_words])
filtered_text = remove_stopwords("This is an example of text pre-processing")
print(filtered_text) # 输出: example text preprocessing
数据去重
import pandas as pd
df = pd.DataFrame({"text": ["AI is great", "AI is great", "Deep Learning"]})
df = df.drop_duplicates()
print(df)
3. 数据增强
在数据不足的情况下,可以进行数据增强来提高模型的泛化能力。
3.1 文本数据增强
from nlpaug.augmenter.word import SynonymAug
aug = SynonymAug()
text = "Artifi