15_人工智能与机器学习入门
机器学习是人工智能的一个子领域,它关注如何让计算机系统从数据中学习并改进,而不需要明确编程。从数据中自动发现模式使用这些模式做出预测或决策随着经验(数据)的积累而不断改进人工智能和机器学习是快速发展的领域,本章只是入门介绍。随着技术不断发展,新的算法、模型和应用将不断涌现。要成为这一领域的专家,需要不断学习和实践。在线课程:Coursera的"Machine Learning"、Stanford的
人工智能与机器学习入门
人工智能(AI)和机器学习(ML)已成为科技领域最热门的方向之一。Python凭借其简洁的语法和丰富的库生态系统,成为了AI和ML领域的首选语言。本章将介绍机器学习的基础概念和实践应用,帮助你踏入这个激动人心的领域。
1. 机器学习基础概念
1.1 什么是机器学习
机器学习是人工智能的一个子领域,它关注如何让计算机系统从数据中学习并改进,而不需要明确编程。
机器学习的核心思想是:
- 从数据中自动发现模式
- 使用这些模式做出预测或决策
- 随着经验(数据)的积累而不断改进
1.2 机器学习的类型
机器学习主要分为三大类:
1. 监督学习:使用带标签的数据进行训练
- 分类:预测离散类别(如垃圾邮件检测、图像识别)
- 回归:预测连续值(如房价预测、温度预测)
2. 无监督学习:使用没有标签的数据发现模式
- 聚类:将相似的数据分组(如客户细分、异常检测)
- 降维:减少数据特征数量(如PCA、t-SNE)
- 关联规则学习:发现数据项之间的关系(如购物篮分析)
3. 强化学习:通过与环境交互和反馈来学习最优策略
- 代理根据环境状态采取行动
- 获得奖励或惩罚
- 学习最大化长期奖励的策略(如游戏AI、机器人控制)
1.3 机器学习的基本流程
一个典型的机器学习项目包括以下步骤:
- 问题定义:明确要解决的问题和目标
- 数据收集:获取相关数据
- 数据预处理:
- 清洗数据(处理缺失值、异常值)
- 特征工程(创建、选择、转换特征)
- 数据规范化/标准化
- 选择模型:根据问题类型选择合适的算法
- 训练模型:使用训练数据拟合模型
- 评估模型:使用测试数据评估模型性能
- 调优模型:调整参数以提高性能
- 部署模型:将模型集成到实际应用中
1.4 常见的机器学习算法
监督学习算法:
- 线性回归
- 逻辑回归
- 决策树
- 随机森林
- 支持向量机(SVM)
- K-近邻(KNN)
- 朴素贝叶斯
- 神经网络
无监督学习算法:
- K-均值聚类
- 层次聚类
- DBSCAN
- 主成分分析(PCA)
- t-SNE
- Apriori算法
强化学习算法:
- Q-learning
- 深度Q网络(DQN)
- 策略梯度方法
- Actor-Critic方法
1.5 评估机器学习模型
不同类型的问题需要不同的评估指标:
分类问题的指标:
- 准确率(Accuracy):正确预测的比例
- 精确率(Precision):被预测为正样本中真正的正样本比例
- 召回率(Recall):真正的正样本中被预测为正样本的比例
- F1分数:精确率和召回率的调和平均
- ROC曲线和AUC:真阳性率与假阳性率关系的曲线
回归问题的指标:
- 均方误差(MSE)
- 均方根误差(RMSE)
- 平均绝对误差(MAE)
- R²(决定系数)
聚类问题的指标:
- 轮廓系数
- 肘部法则
- 互信息分数
2. Python机器学习环境搭建
在开始实践机器学习之前,需要搭建Python机器学习环境。
2.1 安装必要的库
# 安装基础科学计算库
pip install numpy pandas matplotlib seaborn
# 安装机器学习库
pip install scikit-learn
# 安装深度学习库(可选)
pip install tensorflow keras
2.2 常用Python机器学习库
- NumPy:科学计算基础库,提供多维数组支持
- Pandas:数据分析和操作库
- Matplotlib/Seaborn:数据可视化库
- Scikit-learn:机器学习算法和工具集
- TensorFlow/PyTorch:深度学习框架
- Keras:高级神经网络API
- NLTK/SpaCy:自然语言处理库
- OpenCV:计算机视觉库
2.3 Jupyter Notebook
Jupyter Notebook是进行数据科学和机器学习的理想环境:
# 安装Jupyter
pip install jupyter
# 启动Jupyter Notebook
jupyter notebook
3. scikit-learn库入门
scikit-learn(sklearn)是Python机器学习中最流行的库,提供了丰富的算法和工具。
3.1 数据集处理
from sklearn import datasets
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# 加载内置数据集
iris = datasets.load_iris()
X = iris.data # 特征矩阵
y = iris.target # 目标向量
# 查看数据集信息
print(f"特征形状: {X.shape}")
print(f"目标形状: {y.shape}")
print(f"特征名称: {iris.feature_names}")
print(f"目标类别: {iris.target_names}")
# 将数据转换为DataFrame查看
iris_df = pd.DataFrame(X, columns=iris.feature_names)
iris_df['species'] = [iris.target_names[i] for i in y]
print(iris_df.head())
# 绘制数据
plt.figure(figsize=(10, 6))
colors = ['red', 'green', 'blue']
target_names = iris.target_names
for target, color in zip(range(len(target_names)), colors):
plt.scatter(X[y == target, 0], X[y == target, 1],
color=color, label=target_names[target])
plt.xlabel(iris.feature_names[0])
plt.ylabel(iris.feature_names[1])
plt.legend()
plt.title('鸢尾花数据集前两个特征')
plt.show()
3.2 数据集划分
机器学习模型需要在训练集上学习,然后在测试集上评估:
from sklearn.model_selection import train_test_split
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y)
print(f"训练集大小: {X_train.shape[0]} 样本")
print(f"测试集大小: {X_test.shape[0]} 样本")
# 查看类别分布
print("原始数据类别分布:")
for target in range(len(target_names)):
print(f"{target_names[target]}: {np.sum(y == target)} 样本")
print("\n训练集类别分布:")
for target in range(len(target_names)):
print(f"{target_names[target]}: {np.sum(y_train == target)} 样本")
print("\n测试集类别分布:")
for target in range(len(target_names)):
print(f"{target_names[target]}: {np.sum(y_test == target)} 样本")
3.3 特征处理
特征处理是机器学习中的重要步骤:
from sklearn.preprocessing import StandardScaler, MinMaxScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.feature_selection import SelectKBest, f_classif
# 标准化:均值为0,标准差为1
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test) # 注意测试集使用训练集的标准
# 归一化:缩放到[0,1]区间
min_max_scaler = MinMaxScaler()
X_train_normalized = min_max_scaler.fit_transform(X_train)
X_test_normalized = min_max_scaler.transform(X_test)
# 缺失值处理(示例:创建带缺失值的数据)
X_missing = X_train.copy()
n_samples, n_features = X_missing.shape
mask = np.random.randint(0, 2, size=n_samples * n_features).reshape(X_missing.shape).astype(bool)
X_missing[mask] = np.nan
# 使用均值填充缺失值
imputer = SimpleImputer(strategy='mean')
X_imputed = imputer.fit_transform(X_missing)
# 特征选择
selector = SelectKBest(f_classif, k=2) # 选择2个最佳特征
X_selected = selector.fit_transform(X_train, y_train)
selected_indices = selector.get_support(indices=True)
print(f"选择的特征索引: {selected_indices}")
print(f"选择的特征名称: {[iris.feature_names[i] for i in selected_indices]}")
3.4 构建和训练模型
scikit-learn提供了一致的API来训练各种模型:
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
# 逻辑回归
lr_model = LogisticRegression(max_iter=200)
lr_model.fit(X_train_scaled, y_train)
# 决策树
dt_model = DecisionTreeClassifier(random_state=42)
dt_model.fit(X_train, y_train) # 决策树不需要标准化
# 随机森林
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)
# 支持向量机
svm_model = SVC(kernel='rbf', probability=True, random_state=42)
svm_model.fit(X_train_scaled, y_train) # SVM通常需要标准化
# K近邻
knn_model = KNeighborsClassifier(n_neighbors=5)
knn_model.fit(X_train_scaled, y_train) # KNN需要标准化
3.5 模型评估和预测
训练后,需要评估模型性能:
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
# 使用逻辑回归模型预测
y_pred_lr = lr_model.predict(X_test_scaled)
y_prob_lr = lr_model.predict_proba(X_test_scaled)
# 评估准确率
accuracy = accuracy_score(y_test, y_pred_lr)
print(f"逻辑回归准确率: {accuracy:.4f}")
# 详细分类报告
print("\n分类报告:")
print(classification_report(y_test, y_pred_lr, target_names=iris.target_names))
# 混淆矩阵
conf_matrix = confusion_matrix(y_test, y_pred_lr)
print("\n混淆矩阵:")
print(conf_matrix)
# 可视化混淆矩阵
import seaborn as sns
plt.figure(figsize=(8, 6))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues',
xticklabels=iris.target_names,
yticklabels=iris.target_names)
plt.xlabel('预测标签')
plt.ylabel('真实标签')
plt.title('逻辑回归混淆矩阵')
plt.show()
# 比较多个模型
models = {
"逻辑回归": lr_model,
"决策树": dt_model,
"随机森林": rf_model,
"支持向量机": svm_model,
"K近邻": knn_model
}
# 存储每个模型的准确率
accuracies = {}
for name, model in models.items():
# 对于需要标准化的模型,使用标准化后的数据
if name in ["逻辑回归", "支持向量机", "K近邻"]:
y_pred = model.predict(X_test_scaled)
else:
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
accuracies[name] = accuracy
print(f"{name} 准确率: {accuracy:.4f}")
# 可视化模型比较
plt.figure(figsize=(10, 6))
plt.bar(accuracies.keys(), accuracies.values())
plt.ylim(0.8, 1.0) # 调整y轴范围以便更好地显示差异
plt.ylabel('准确率')
plt.title('不同模型准确率比较')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
3.6 模型持久化
训练好的模型可以保存以便将来使用:
import joblib
# 保存模型
joblib.dump(rf_model, 'random_forest_model.pkl')
# 加载模型
loaded_model = joblib.load('random_forest_model.pkl')
# 使用加载的模型预测
y_pred_loaded = loaded_model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred_loaded)
print(f"加载的模型准确率: {accuracy:.4f}")
3.7 交叉验证
交叉验证提供了更可靠的模型性能评估:
from sklearn.model_selection import cross_val_score, KFold, StratifiedKFold
# 基本交叉验证
cv_scores = cross_val_score(rf_model, X, y, cv=5) # 5折交叉验证
print(f"交叉验证分数: {cv_scores}")
print(f"平均准确率: {cv_scores.mean():.4f} ± {cv_scores.std():.4f}")
# 分层K折交叉验证(保持类别比例)
stratified_kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
stratified_scores = cross_val_score(rf_model, X, y, cv=stratified_kfold)
print(f"分层交叉验证平均准确率: {stratified_scores.mean():.4f}")
# 使用不同模型进行交叉验证
cv_results = {}
for name, model in models.items():
scores = cross_val_score(model, X, y, cv=5)
cv_results[name] = (scores.mean(), scores.std())
print(f"{name} CV 准确率: {scores.mean():.4f} ± {scores.std():.4f}")
3.8 超参数调优
通过网格搜索或随机搜索来找到最佳模型参数:
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
# 网格搜索示例 - 随机森林
param_grid = {
'n_estimators': [50, 100, 200],
'max_depth': [None, 10, 20, 30],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
}
grid_search = GridSearchCV(
RandomForestClassifier(random_state=42),
param_grid,
cv=5,
scoring='accuracy',
n_jobs=-1 # 使用所有CPU核心
)
grid_search.fit(X, y)
print(f"最佳参数: {grid_search.best_params_}")
print(f"最佳交叉验证分数: {grid_search.best_score_:.4f}")
# 使用最佳参数的模型
best_rf_model = grid_search.best_estimator_
# 随机搜索示例 - SVM
from scipy.stats import uniform, randint
param_dist = {
'C': uniform(0.1, 10),
'gamma': uniform(0.001, 0.1),
'kernel': ['rbf', 'poly', 'sigmoid']
}
random_search = RandomizedSearchCV(
SVC(probability=True, random_state=42),
param_distributions=param_dist,
n_iter=20, # 尝试20组参数
cv=5,
scoring='accuracy',
n_jobs=-1,
random_state=42
)
random_search.fit(X_train_scaled, y_train)
print(f"SVM最佳参数: {random_search.best_params_}")
print(f"SVM最佳交叉验证分数: {random_search.best_score_:.4f}")
神经网络与深度学习
随着计算能力的提升和大规模数据的可用性,深度学习已成为人工智能领域最受关注的技术之一。深度学习是机器学习的一个子集,使用多层神经网络对数据进行建模。
神经网络基础
神经网络的灵感来源于人脑的工作方式,由多个相互连接的神经元组成。一个基本的神经网络包含以下组件:
- 输入层:接收初始数据
- 隐藏层:处理输入信息(可以有多层)
- 输出层:产生最终结果
- 权重和偏置:调整网络性能的参数
- 激活函数:引入非线性,使网络能学习复杂模式
以下是一个简单神经网络的Python实现:
import numpy as np
class SimpleNeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
# 初始化权重和偏置
self.W1 = np.random.randn(input_size, hidden_size) * 0.01
self.b1 = np.zeros((1, hidden_size))
self.W2 = np.random.randn(hidden_size, output_size) * 0.01
self.b2 = np.zeros((1, output_size))
def sigmoid(self, x):
# 激活函数
return 1 / (1 + np.exp(-x))
def forward(self, X):
# 前向传播
self.z1 = np.dot(X, self.W1) + self.b1
self.a1 = self.sigmoid(self.z1)
self.z2 = np.dot(self.a1, self.W2) + self.b2
self.a2 = self.sigmoid(self.z2)
return self.a2
# 简化版本,实际应用中还需要实现反向传播等算法
深度学习库:TensorFlow 和 PyTorch
现在,大多数深度学习项目都使用专业库来构建和训练模型。最流行的两个框架是TensorFlow和PyTorch。
TensorFlow 基础
TensorFlow是由Google开发的开源深度学习框架。
# 安装
# pip install tensorflow
import tensorflow as tf
# 创建一个简单的神经网络
model = tf.keras.Sequential([
tf.keras.layers.Dense(128, activation='relu', input_shape=(784,)),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
# 编译模型
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 训练模型 (假设x_train和y_train已定义)
# model.fit(x_train, y_train, epochs=5, validation_split=0.2)
PyTorch 基础
PyTorch是由Facebook开发的更为灵活的深度学习框架,广受研究人员欢迎。
# 安装
# pip install torch torchvision
import torch
import torch.nn as nn
import torch.optim as optim
# 定义网络
class SimpleNet(nn.Module):
def __init__(self):
super(SimpleNet, self).__init__()
self.fc1 = nn.Linear(784, 128)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(0.2)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.dropout(x)
x = self.fc2(x)
return x
# 创建模型、损失函数和优化器
model = SimpleNet()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# 训练循环会在实际项目中实现
卷积神经网络 (CNN)
卷积神经网络是专为处理具有网格状拓扑结构的数据(如图像)设计的深度学习架构。
CNN的主要组件:
- 卷积层:应用过滤器提取特征
- 池化层:降低维度,保留重要特征
- 全连接层:进行最终的分类或回归
使用TensorFlow构建CNN:
model = tf.keras.Sequential([
# 输入: 28x28 像素的图像,1个通道
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
循环神经网络 (RNN)
循环神经网络专为处理序列数据设计,如文本、时间序列和语音。
RNN的变体:
- LSTM (长短期记忆网络):解决了传统RNN的梯度消失问题
- GRU (门控循环单元):LSTM的简化版本,性能类似但参数更少
使用TensorFlow构建LSTM:
model = tf.keras.Sequential([
tf.keras.layers.Embedding(input_dim=10000, output_dim=64),
tf.keras.layers.LSTM(128, return_sequences=True),
tf.keras.layers.LSTM(64),
tf.keras.layers.Dense(10, activation='softmax')
])
自然语言处理 (NLP)
自然语言处理是AI领域的重要分支,专注于让计算机理解和处理人类语言。
文本预处理技术
文本数据需要经过多个预处理步骤才能用于机器学习:
import nltk
import re
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
# 下载必要资源
nltk.download('punkt')
nltk.download('stopwords')
text = "Natural language processing (NLP) is a subfield of linguistics, computer science, and AI."
# 转换为小写
text = text.lower()
# 移除标点和数字
text = re.sub(r'[^\w\s]', '', text)
# 分词
tokens = word_tokenize(text)
# 移除停用词
stop_words = set(stopwords.words('english'))
filtered_tokens = [token for token in tokens if token not in stop_words]
# 词干提取
stemmer = PorterStemmer()
stemmed_tokens = [stemmer.stem(token) for token in filtered_tokens]
print(stemmed_tokens)
文本表示方法
将文本转换为机器可理解的数值形式:
- Bag of Words (BoW)
from sklearn.feature_extraction.text import CountVectorizer
corpus = [
"This is the first document.",
"This document is the second document.",
"And this is the third one.",
]
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)
print(vectorizer.get_feature_names_out())
print(X.toarray())
- TF-IDF (词频-逆文档频率)
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vectorizer = TfidfVectorizer()
X_tfidf = tfidf_vectorizer.fit_transform(corpus)
print(X_tfidf.toarray())
- Word Embeddings (词嵌入)
# 使用预训练的Word2Vec模型
# pip install gensim
from gensim.models import KeyedVectors
# 加载预训练模型
model = KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
# 查找单词向量
vector = model['python'] # 获取'python'的向量表示
使用Transformers进行NLP任务
Transformers是当前NLP领域最先进的架构,是BERT、GPT等强大模型的基础。
# 安装
# pip install transformers
from transformers import pipeline
# 情感分析
sentiment_analyzer = pipeline("sentiment-analysis")
result = sentiment_analyzer("I love using Python for NLP tasks!")
print(result)
# 文本生成
generator = pipeline("text-generation")
result = generator("Python is a programming language that", max_length=50)
print(result[0]['generated_text'])
# 问答
qa = pipeline("question-answering")
context = "PyTorch is a machine learning framework developed by Facebook."
result = qa(question="Who developed PyTorch?", context=context)
print(f"Answer: {result['answer']}")
计算机视觉
计算机视觉专注于让机器能够从图像或视频中获取高级理解。
图像处理基础
Python提供了多种图像处理库,其中OpenCV是最常用的:
# pip install opencv-python
import cv2
import matplotlib.pyplot as plt
# 读取图像
img = cv2.imread('example.jpg')
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # OpenCV默认使用BGR
# 显示图像
plt.imshow(img_rgb)
plt.axis('off')
plt.show()
# 缩放图像
resized = cv2.resize(img, (300, 200))
# 灰度转换
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 高斯模糊
blurred = cv2.GaussianBlur(img, (5, 5), 0)
# 边缘检测
edges = cv2.Canny(gray, 100, 200)
使用CNN进行图像分类
利用预训练模型进行图像分类是计算机视觉的常见任务:
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
import numpy as np
# 加载预训练模型
model = MobileNetV2(weights='imagenet')
# 预处理图像
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
# 预测
preds = model.predict(x)
results = decode_predictions(preds, top=3)[0]
# 显示结果
for result in results:
print(f"{result[1]}: {result[2]*100:.2f}%")
目标检测
目标检测不仅可以分类图像中的对象,还能定位它们:
# 使用YOLOv5进行目标检测
# pip install torch torchvision
# 此处假设已经下载了YOLOv5模型
import torch
# 加载YOLOv5模型
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
# 进行检测
img_path = 'street.jpg'
results = model(img_path)
# 显示结果
results.print() # 打印检测到的对象
results.show() # 显示带有边界框的图像
# 获取检测结果
results_df = results.pandas().xyxy[0] # 边界框坐标和类别
强化学习
强化学习是一种通过与环境交互并从反馈中学习的机器学习范式。
基本概念
强化学习系统包含:
- 环境:agent所处的世界
- 状态:环境的当前状况
- 动作:agent可以采取的行为
- 奖励:环境对agent行为的反馈
使用OpenAI Gym
OpenAI Gym是一个强化学习的标准工具包:
# pip install gym
import gym
import numpy as np
# 创建环境
env = gym.make('CartPole-v1')
# 简单的随机策略
episodes = 10
for episode in range(episodes):
state = env.reset()
done = False
total_reward = 0
while not done:
# 环境渲染
env.render()
# 随机选择行动
action = env.action_space.sample()
# 执行行动
state, reward, done, info = env.step(action)
total_reward += reward
print(f"Episode {episode+1}: Total Reward = {total_reward}")
env.close()
Q-Learning 实例
Q-Learning是一种基本的强化学习算法:
import gym
import numpy as np
# 创建环境
env = gym.make('FrozenLake-v1')
# 初始化Q表
q_table = np.zeros([env.observation_space.n, env.action_space.n])
# 学习参数
learning_rate = 0.8
discount_factor = 0.95
exploration_rate = 1.0
max_exploration_rate = 1.0
min_exploration_rate = 0.01
exploration_decay_rate = 0.001
# 训练参数
num_episodes = 10000
max_steps_per_episode = 100
for episode in range(num_episodes):
state = env.reset()
done = False
for step in range(max_steps_per_episode):
# 探索或利用
exploration_threshold = np.random.random()
if exploration_threshold > exploration_rate:
action = np.argmax(q_table[state,:])
else:
action = env.action_space.sample()
# 执行动作
new_state, reward, done, info = env.step(action)
# 更新Q表
q_table[state, action] = q_table[state, action] * (1 - learning_rate) + \
learning_rate * (reward + discount_factor * np.max(q_table[new_state, :]))
state = new_state
if done:
break
# 减少探索率
exploration_rate = min_exploration_rate + \
(max_exploration_rate - min_exploration_rate) * np.exp(-exploration_decay_rate * episode)
# 测试训练好的智能体
env.reset()
for episode in range(3):
state = env.reset()
done = False
print(f"EPISODE {episode + 1}")
for step in range(max_steps_per_episode):
env.render()
action = np.argmax(q_table[state,:])
new_state, reward, done, info = env.step(action)
if done:
if reward == 1:
print(f"成功到达目标,用了 {step + 1} 步!")
else:
print(f"掉入陷阱,用了 {step + 1} 步!")
break
state = new_state
env.close()
实战项目:情感分析系统
下面是一个将NLP知识应用于实际问题的端到端项目实例。
项目目标
构建一个系统,分析产品评论的情感极性(积极/消极)。
数据获取与预处理
import pandas as pd
import re
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
# 加载数据
df = pd.read_csv('product_reviews.csv')
print(f"数据集大小: {df.shape}")
print(df.head())
# 文本预处理函数
def preprocess_text(text):
# 转小写
text = text.lower()
# 移除HTML标签
text = re.sub(r'<.*?>', '', text)
# 移除非字母字符
text = re.sub(r'[^a-zA-Z\s]', '', text)
# 分词
tokens = nltk.word_tokenize(text)
# 移除停用词
stop_words = set(stopwords.words('english'))
tokens = [token for token in tokens if token not in stop_words]
# 词形还原
lemmatizer = WordNetLemmatizer()
tokens = [lemmatizer.lemmatize(token) for token in tokens]
# 重新组合成文本
return ' '.join(tokens)
# 应用预处理
df['processed_review'] = df['review_text'].apply(preprocess_text)
特征提取与建模
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
# 划分训练集和测试集
X = df['processed_review']
y = df['sentiment'] # 假设'sentiment'列包含标签:1为积极,0为消极
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# TF-IDF特征提取
tfidf_vectorizer = TfidfVectorizer(max_features=5000)
X_train_tfidf = tfidf_vectorizer.fit_transform(X_train)
X_test_tfidf = tfidf_vectorizer.transform(X_test)
# 训练逻辑回归模型
model = LogisticRegression()
model.fit(X_train_tfidf, y_train)
# 预测
y_pred = model.predict(X_test_tfidf)
# 评估模型
accuracy = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred)
print(f"准确率: {accuracy:.4f}")
print("分类报告:")
print(report)
使用深度学习提升性能
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
# 参数
max_words = 10000
max_len = 200
# 创建分词器
tokenizer = Tokenizer(num_words=max_words)
tokenizer.fit_on_texts(X_train)
# 将文本转换为序列
X_train_seq = tokenizer.texts_to_sequences(X_train)
X_test_seq = tokenizer.texts_to_sequences(X_test)
# 填充序列
X_train_pad = pad_sequences(X_train_seq, maxlen=max_len)
X_test_pad = pad_sequences(X_test_seq, maxlen=max_len)
# 构建LSTM模型
model = tf.keras.Sequential([
tf.keras.layers.Embedding(max_words, 128, input_length=max_len),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, return_sequences=True)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1, activation='sigmoid')
])
# 编译模型
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# 训练模型
history = model.fit(
X_train_pad, y_train,
epochs=5,
batch_size=32,
validation_split=0.2,
verbose=1
)
# 评估模型
loss, accuracy = model.evaluate(X_test_pad, y_test)
print(f"测试集准确率: {accuracy:.4f}")
创建简单的Web应用
import pickle
import flask
from flask import Flask, request, jsonify, render_template
app = Flask(__name__)
# 加载模型和向量化器
with open('sentiment_model.pkl', 'rb') as f:
model = pickle.load(f)
with open('tfidf_vectorizer.pkl', 'rb') as f:
vectorizer = pickle.load(f)
def preprocess_text(text):
# 同上面的预处理函数
pass
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
# 获取用户输入
review = request.form.get('review')
# 预处理
processed_review = preprocess_text(review)
# 向量化
review_vector = vectorizer.transform([processed_review])
# 预测
prediction = model.predict(review_vector)[0]
# 返回结果
sentiment = "积极" if prediction == 1 else "消极"
return render_template('index.html', prediction_text=f'评论情感: {sentiment}', review=review)
if __name__ == '__main__':
app.run(debug=True)
生成式AI
生成式AI是人工智能领域中快速发展的一个分支,专注于创建能够生成新内容的模型。
基于扩散模型的图像生成
扩散模型是近年来在图像生成领域取得突破性进展的技术:
import torch
from diffusers import StableDiffusionPipeline
# 加载预训练的Stable Diffusion模型
model_id = "stabilityai/stable-diffusion-2-1"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to("cuda")
# 生成图像
prompt = "一只宇航员骑马的照片,高清晰度"
image = pipe(prompt).images[0]
image.save("astronaut_on_horse.png")
生成式文本模型
使用Hugging Face Transformers库实现文本生成:
from transformers import AutoModelForCausalLM, AutoTokenizer
# 加载预训练模型和分词器
model_name = "gpt2" # 或其他模型,如"baichuan-inc/Baichuan2-7B-Chat"等
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# 生成文本
input_text = "人工智能的未来发展将"
input_ids = tokenizer.encode(input_text, return_tensors="pt")
output = model.generate(input_ids,
max_length=100,
num_return_sequences=1,
no_repeat_ngram_size=2,
top_k=50,
top_p=0.95,
temperature=0.7)
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
print(generated_text)
音频生成
使用TTS (Text-to-Speech) 库生成语音:
from TTS.api import TTS
# 初始化TTS模型
tts = TTS("tts_models/zh-CN/baker/tacotron2-DDC-GST")
# 文本转语音
text = "欢迎学习Python中的人工智能和机器学习。"
output_path = "output.wav"
tts.tts_to_file(text=text, file_path=output_path)
可解释性AI
可解释性AI (XAI) 旨在使AI系统的决策过程对人类可理解。
LIME解释器
LIME (Local Interpretable Model-agnostic Explanations) 是一种解释任何黑盒模型预测的技术:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from lime.lime_tabular import LimeTabularExplainer
# 加载数据
iris = load_iris()
X, y = iris.data, iris.target
feature_names = iris.feature_names
# 训练模型
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X, y)
# 创建LIME解释器
explainer = LimeTabularExplainer(X,
feature_names=feature_names,
class_names=iris.target_names,
mode='classification')
# 解释一个预测
instance = X[0] # 解释第一个实例
exp = explainer.explain_instance(instance, model.predict_proba, num_features=4)
# 可视化解释
exp.show_in_notebook(show_table=True) # 在Jupyter中显示
# 或保存为图像
exp.as_pyplot_figure()
plt.tight_layout()
plt.savefig('lime_explanation.png')
SHAP值分析
SHAP (SHapley Additive exPlanations) 是一种基于博弈论的模型解释方法:
import shap
import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_california_housing
# 加载数据
housing = fetch_california_housing()
X, y = housing.data, housing.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 训练模型
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# 创建SHAP解释器
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_test[:100]) # 计算前100个测试实例的SHAP值
# 可视化
shap.summary_plot(shap_values, X_test[:100], feature_names=housing.feature_names)
特征重要性可视化
直接可视化机器学习模型中的特征重要性:
from sklearn.ensemble import RandomForestClassifier
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
# 训练模型(假设X_train, y_train已定义)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# 提取特征重要性
importances = model.feature_importances_
indices = np.argsort(importances)[::-1]
# 创建DataFrame
feature_importance_df = pd.DataFrame({
'Feature': [feature_names[i] for i in indices],
'Importance': [importances[i] for i in indices]
})
# 可视化
plt.figure(figsize=(10, 6))
sns.barplot(x='Importance', y='Feature', data=feature_importance_df)
plt.title('特征重要性')
plt.tight_layout()
plt.savefig('feature_importance.png')
AI伦理与责任
偏见检测与缓解
检测并减少机器学习模型中的偏见:
import pandas as pd
from aif360.datasets import BinaryLabelDataset
from aif360.metrics import BinaryLabelDatasetMetric
from aif360.algorithms.preprocessing import Reweighing
# 假设我们有一个含有敏感属性的数据集
df = pd.read_csv('employment_data.csv')
# 创建二元标签数据集
privileged_groups = [{'gender': 1}] # 假设1代表男性
unprivileged_groups = [{'gender': 0}] # 假设0代表女性
dataset = BinaryLabelDataset(
df=df,
label_names=['employed'],
protected_attribute_names=['gender'],
favorable_label=1,
unfavorable_label=0
)
# 计算偏见指标
metric = BinaryLabelDatasetMetric(
dataset,
unprivileged_groups=unprivileged_groups,
privileged_groups=privileged_groups
)
print(f"统计均等差异: {metric.statistical_parity_difference()}")
print(f"差异影响率: {metric.disparate_impact()}")
# 应用重新加权算法以减轻偏见
rw = Reweighing(unprivileged_groups=unprivileged_groups,
privileged_groups=privileged_groups)
dataset_transformed = rw.fit_transform(dataset)
# 评估处理后的偏见
metric_transformed = BinaryLabelDatasetMetric(
dataset_transformed,
unprivileged_groups=unprivileged_groups,
privileged_groups=privileged_groups
)
print(f"处理后的统计均等差异: {metric_transformed.statistical_parity_difference()}")
print(f"处理后的差异影响率: {metric_transformed.disparate_impact()}")
差分隐私
保护训练数据中的隐私信息:
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import datasets
from diffprivlib.models import LogisticRegression
# 加载数据
iris = datasets.load_iris()
X, y = iris.data, iris.target
# 创建二分类问题
X = X[y != 2]
y = y[y != 2]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 使用差分隐私逻辑回归
dp_clf = LogisticRegression(epsilon=1.0, random_state=42) # epsilon参数控制隐私水平
dp_clf.fit(X_train, y_train)
# 预测并评估
dp_accuracy = dp_clf.score(X_test, y_test)
print(f"使用差分隐私的模型准确率: {dp_accuracy:.4f}")
# 比较普通逻辑回归
from sklearn.linear_model import LogisticRegression as SklearnLogisticRegression
sklearn_clf = SklearnLogisticRegression(random_state=42)
sklearn_clf.fit(X_train, y_train)
sklearn_accuracy = sklearn_clf.score(X_test, y_test)
print(f"普通模型准确率: {sklearn_accuracy:.4f}")
模型安全性评估
测试机器学习模型对对抗性攻击的鲁棒性:
import numpy as np
import tensorflow as tf
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
from art.attacks.evasion import FastGradientMethod
from art.estimators.classification import TensorFlowV2Classifier
# 加载预训练模型
model = MobileNetV2(weights='imagenet')
# 创建ART分类器
def predict(x):
return model(x)
classifier = TensorFlowV2Classifier(
model=model,
nb_classes=1000,
input_shape=(224, 224, 3),
loss_object=tf.keras.losses.CategoricalCrossentropy(),
clip_values=(0, 1)
)
# 创建FGSM攻击
attack = FastGradientMethod(estimator=classifier, eps=0.1)
# 准备示例图像
x = np.random.rand(1, 224, 224, 3) # 随机图像,实际应用中应使用真实图像
x_preprocessed = preprocess_input(x.copy())
# 生成对抗样本
x_adv = attack.generate(x=x_preprocessed)
# 比较原始预测和对抗样本预测
y_pred = np.argmax(classifier.predict(x_preprocessed), axis=1)
y_adv = np.argmax(classifier.predict(x_adv), axis=1)
print(f"原始预测: {y_pred}")
print(f"对抗样本预测: {y_adv}")
联邦学习
联邦学习是一种分布式机器学习方法,允许多个参与者在不共享原始数据的情况下共同训练模型。
使用Flower框架实现联邦学习
import flwr as fl
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import numpy as np
# 模拟联邦学习的客户端
class FlowerClient(fl.client.NumPyClient):
def __init__(self, model, x_train, y_train, x_test, y_test):
self.model = model
self.x_train, self.y_train = x_train, y_train
self.x_test, self.y_test = x_test, y_test
def get_parameters(self, config):
return [np.array(layer.get_weights()[0]) for layer in self.model.layers]
def fit(self, parameters, config):
# 更新本地模型参数
for i, layer in enumerate(self.model.layers):
weights = layer.get_weights()
weights[0] = parameters[i]
layer.set_weights(weights)
# 在本地数据上训练
self.model.fit(self.x_train, self.y_train, epochs=1, batch_size=32, verbose=0)
# 返回更新后的参数
return self.get_parameters(config), len(self.x_train), {}
def evaluate(self, parameters, config):
# 更新本地模型参数
for i, layer in enumerate(self.model.layers):
weights = layer.get_weights()
weights[0] = parameters[i]
layer.set_weights(weights)
# 在测试数据上评估
loss, accuracy = self.model.evaluate(self.x_test, self.y_test, verbose=0)
return loss, len(self.x_test), {"accuracy": accuracy}
# 创建简单模型
def create_model():
model = Sequential([
Dense(128, activation='relu', input_shape=(784,)),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
# 模拟联邦学习系统
def start_federated_learning():
# 加载数据
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train, x_test = x_train.reshape(-1, 784) / 255.0, x_test.reshape(-1, 784) / 255.0
# 模拟3个客户端,每个有部分数据
clients = []
client_data = []
num_clients = 3
samples_per_client = len(x_train) // num_clients
for i in range(num_clients):
# 分割数据
start, end = i * samples_per_client, (i + 1) * samples_per_client
client_data.append((x_train[start:end], y_train[start:end], x_test, y_test))
# 为每个客户端创建模型
client_model = create_model()
clients.append(FlowerClient(client_model, *client_data[i]))
# 此处应有服务器启动代码
# 在实际应用中,使用fl.server.start_server()启动联邦学习服务器
print("联邦学习系统已准备就绪")
# 返回客户端供演示
return clients
# 实际使用时,通过以下方式启动联邦学习:
# clients = start_federated_learning()
隐私保护联邦学习
在联邦学习中加入差分隐私来增强隐私保护:
import tensorflow as tf
import tensorflow_privacy as tfp
# 创建带差分隐私的优化器
def create_dp_model():
vector_size = 784
num_classes = 10
# 创建模型
model = tf.keras.Sequential([
tf.keras.layers.Dense(128, activation='relu', input_shape=(vector_size,)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(num_classes, activation='softmax')
])
# 差分隐私参数
l2_norm_clip = 1.0
noise_multiplier = 0.1
num_microbatches = 1
# 创建差分隐私优化器
optimizer = tfp.DPKerasAdamOptimizer(
l2_norm_clip=l2_norm_clip,
noise_multiplier=noise_multiplier,
num_microbatches=num_microbatches,
learning_rate=0.001
)
# 编译模型
model.compile(
optimizer=optimizer,
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy']
)
return model
# 示例使用
# model = create_dp_model()
# 在联邦学习中,每个客户端都使用此模型
大型语言模型实践
大型语言模型(LLM)已成为AI领域的重要研究方向,下面介绍如何使用Python与LLM进行交互。
使用OpenAI API
通过API调用OpenAI模型:
import openai
from openai import OpenAI
# 初始化客户端
client = OpenAI(api_key="your_api_key") # 使用你的API密钥
# 生成文本
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "你是一位Python专家,帮助用户学习和使用Python。"},
{"role": "user", "content": "请解释一下Python中的装饰器并给出一个简单例子。"}
]
)
# 输出生成结果
print(completion.choices[0].message.content)
使用LangChain框架开发LLM应用
LangChain是构建LLM应用的流行框架:
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.memory import ConversationBufferMemory
# 初始化LLM
llm = OpenAI(temperature=0.7, openai_api_key="your_api_key")
# 创建提示模板
template = """
你是一位Python教程助手。
{chat_history}
用户: {user_question}
助手: """
prompt = PromptTemplate(
input_variables=["chat_history", "user_question"],
template=template
)
# 创建对话内存
memory = ConversationBufferMemory(memory_key="chat_history", input_key="user_question")
# 创建链
chain = LLMChain(
llm=llm,
prompt=prompt,
memory=memory,
verbose=True
)
# 使用链进行对话
response = chain.predict(user_question="如何在Python中处理异常?")
print(response)
# 继续对话
response = chain.predict(user_question="能给我一个具体的例子吗?")
print(response)
本地部署开源语言模型
使用Python部署和运行开源的大型语言模型:
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# 加载模型和分词器(以较小的模型为例,实际LLM通常较大)
model_name = "THUDM/chatglm3-6b" # 可替换为其他开源模型
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_name,
device_map="auto", # 自动处理设备分配
torch_dtype=torch.float16, # 使用半精度浮点数
trust_remote_code=True
)
# 创建对话函数
def chat_with_model(prompt, history=None):
if history is None:
history = []
# 生成回复
response, history = model.chat(tokenizer, prompt, history=history)
return response, history
# 示例对话
history = []
prompt = "请介绍一下Python中的生成器和迭代器的区别。"
response, history = chat_with_model(prompt, history)
print(f"用户: {prompt}")
print(f"模型: {response}")
# 继续对话
prompt = "能给出一些实际的应用场景吗?"
response, history = chat_with_model(prompt, history)
print(f"用户: {prompt}")
print(f"模型: {response}")
结语
人工智能和机器学习是快速发展的领域,本章只是入门介绍。随着技术不断发展,新的算法、模型和应用将不断涌现。要成为这一领域的专家,需要不断学习和实践。
推荐资源:
- 在线课程:Coursera的"Machine Learning"、Stanford的CS231n和CS224n
- 书籍:《Deep Learning》(Ian Goodfellow)、《Hands-On Machine Learning with Scikit-Learn and TensorFlow》
- 网站:Kaggle、Towards Data Science、arXiv
记住,理论学习固然重要,但通过实际项目获得的实践经验更为宝贵。开始你的AI之旅吧!
更多推荐




所有评论(0)