机器学习基础
简介
机器学习(Machine Learning, ML)是人工智能的一个重要分支,它使计算机能够在没有明确编程的情况下学习和改进。通过算法和统计模型,机器学习系统可以从数据中识别模式,并基于这些模式做出预测或决策。
目录
机器学习概述
定义
机器学习是一种让计算机系统自动学习和改进的方法,无需明确编程。它通过算法分析数据,识别模式,并基于学到的模式做出预测。
与传统编程的区别
传统编程:
机器学习:
机器学习的优势
- 自动化决策:减少人工干预
- 模式识别:发现数据中的隐藏模式
- 预测能力:基于历史数据预测未来
- 适应性:随着新数据的加入不断改进
- 处理复杂性:处理高维度、大规模数据
机器学习类型
1. 监督学习(Supervised Learning)
使用标记的训练数据来学习输入和输出之间的映射关系。
特点:
- 有标签的训练数据
- 目标是学习输入到输出的映射
- 可以评估模型性能
主要任务:
分类(Classification)
预测离散的类别标签。
示例:
- 邮件垃圾检测(垃圾/非垃圾)
- 图像识别(猫/狗/鸟)
- 疾病诊断(阳性/阴性)
常用算法:
- 逻辑回归(Logistic Regression)
- 决策树(Decision Tree)
- 随机森林(Random Forest)
- 支持向量机(SVM)
- 朴素贝叶斯(Naive Bayes)
回归(Regression)
预测连续的数值。
示例:
- 房价预测
- 股票价格预测
- 销售额预测
常用算法:
- 线性回归(Linear Regression)
- 多项式回归(Polynomial Regression)
- 岭回归(Ridge Regression)
- Lasso回归
2. 无监督学习(Unsupervised Learning)
从没有标签的数据中发现隐藏的模式和结构。
特点:
- 无标签数据
- 探索数据结构
- 发现隐藏模式
主要任务:
聚类(Clustering)
将相似的数据点分组。
示例:
- 客户细分
- 基因序列分析
- 图像分割
常用算法:
- K-Means
- 层次聚类(Hierarchical Clustering)
- DBSCAN
降维(Dimensionality Reduction)
减少数据的维度,保留重要信息。
示例:
- 数据可视化
- 特征选择
- 噪声减少
常用算法:
- 主成分分析(PCA)
- t-SNE
- 线性判别分析(LDA)
关联规则学习(Association Rule Learning)
发现变量之间的关系。
示例:
- 购物篮分析
- 推荐系统
- 网页点击分析
常用算法:
- Apriori算法
- FP-Growth
3. 强化学习(Reinforcement Learning)
通过与环境交互,学习如何采取行动以最大化累积奖励。
特点:
- 智能体与环境交互
- 通过奖励和惩罚学习
- 序列决策问题
示例:
- 游戏AI(AlphaGo)
- 自动驾驶
- 机器人控制
- 推荐系统优化
核心概念:
- 智能体(Agent)
- 环境(Environment)
- 状态(State)
- 动作(Action)
- 奖励(Reward)
核心概念
1. 数据集划分
Python |
---|
| # 典型的数据集划分
训练集(Training Set):60-80%
验证集(Validation Set):10-20%
测试集(Test Set):10-20%
|
作用:
- 训练集:训练模型参数
- 验证集:调整超参数,模型选择
- 测试集:最终性能评估
2. 偏差与方差
偏差(Bias):
- 模型预测值与真实值之间的差异
- 高偏差导致欠拟合
方差(Variance):
- 模型对训练数据变化的敏感性
- 高方差导致过拟合
偏差-方差权衡:
3. 损失函数
回归问题:
- 均方误差(MSE):MSE = (1/n) * Σ(y_true - y_pred)²
- 平均绝对误差(MAE):MAE = (1/n) * Σ|y_true - y_pred|
分类问题:
- 交叉熵损失:-Σ y_true * log(y_pred)
- 0-1损失:错误分类的数量
4. 梯度下降
优化算法,用于最小化损失函数。
Python |
---|
| # 梯度下降更新规则
θ = θ - α * ∇J(θ)
|
其中:
- θ:模型参数
- α:学习率
- ∇J(θ):损失函数的梯度
变种:
- 批量梯度下降(Batch GD)
- 随机梯度下降(SGD)
- 小批量梯度下降(Mini-batch GD)
常用算法
1. 线性回归
原理:
假设输入特征与输出之间存在线性关系。
数学表达:
Text Only |
---|
| y = β₀ + β₁x₁ + β₂x₂ + ... + βₙxₙ + ε
|
Python实现示例:
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# 创建模型
model = LinearRegression()
# 训练模型
model.fit(X_train, y_train)
# 预测
y_pred = model.predict(X_test)
# 评估
mse = mean_squared_error(y_test, y_pred)
|
2. 逻辑回归
原理:
使用逻辑函数(sigmoid)进行二分类。
数学表达:
Text Only |
---|
| p = 1 / (1 + e^(-z))
其中 z = β₀ + β₁x₁ + β₂x₂ + ... + βₙxₙ
|
Python实现示例:
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
# 创建模型
model = LogisticRegression()
# 训练模型
model.fit(X_train, y_train)
# 预测
y_pred = model.predict(X_test)
# 评估
accuracy = accuracy_score(y_test, y_pred)
print(classification_report(y_test, y_pred))
|
3. 决策树
原理:
通过一系列if-else条件来做决策。
优点:
- 易于理解和解释
- 不需要数据预处理
- 可以处理数值和分类特征
缺点:
- 容易过拟合
- 对数据变化敏感
Python实现示例:
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt
# 创建模型
model = DecisionTreeClassifier(max_depth=5, random_state=42)
# 训练模型
model.fit(X_train, y_train)
# 可视化决策树
plt.figure(figsize=(15, 10))
plot_tree(model, feature_names=feature_names, class_names=class_names, filled=True)
plt.show()
|
4. 随机森林
原理:
集成多个决策树,通过投票或平均来做最终决策。
优点:
- 减少过拟合
- 处理缺失值
- 提供特征重要性
Python实现示例:
Python |
---|
| from sklearn.ensemble import RandomForestClassifier
# 创建模型
model = RandomForestClassifier(n_estimators=100, random_state=42)
# 训练模型
model.fit(X_train, y_train)
# 特征重要性
feature_importance = model.feature_importances_
|
5. K-Means聚类
原理:
将数据分为k个簇,使簇内距离最小,簇间距离最大。
算法步骤:
1. 随机初始化k个中心点
2. 将每个点分配到最近的中心
3. 更新中心点位置
4. 重复步骤2-3直到收敛
Python实现示例:
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
# 创建模型
kmeans = KMeans(n_clusters=3, random_state=42)
# 训练模型
kmeans.fit(X)
# 获取聚类标签
labels = kmeans.labels_
centers = kmeans.cluster_centers_
# 可视化
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis')
plt.scatter(centers[:, 0], centers[:, 1], c='red', marker='x', s=200)
plt.show()
|
数据预处理
1. 数据清洗
处理缺失值:
Python |
---|
| import pandas as pd
from sklearn.impute import SimpleImputer
# 删除缺失值
df_cleaned = df.dropna()
# 填充缺失值
imputer = SimpleImputer(strategy='mean') # 'median', 'most_frequent'
X_imputed = imputer.fit_transform(X)
|
处理异常值:
Python |
---|
| # 使用IQR方法检测异常值
Q1 = df['column'].quantile(0.25)
Q3 = df['column'].quantile(0.75)
IQR = Q3 - Q1
# 定义异常值边界
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
# 过滤异常值
df_filtered = df[(df['column'] >= lower_bound) & (df['column'] <= upper_bound)]
|
2. 特征缩放
标准化(Z-score normalization):
Python |
---|
| from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
|
归一化(Min-Max scaling):
Python |
---|
| from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_normalized = scaler.fit_transform(X)
|
3. 编码分类变量
独热编码(One-Hot Encoding):
Python |
---|
| from sklearn.preprocessing import OneHotEncoder
import pandas as pd
# 使用pandas
df_encoded = pd.get_dummies(df, columns=['category_column'])
# 使用sklearn
encoder = OneHotEncoder(sparse=False)
X_encoded = encoder.fit_transform(X_categorical)
|
标签编码(Label Encoding):
Python |
---|
| from sklearn.preprocessing import LabelEncoder
encoder = LabelEncoder()
y_encoded = encoder.fit_transform(y)
|
模型评估
分类指标
准确率(Accuracy):
Python |
---|
| accuracy = (TP + TN) / (TP + TN + FP + FN)
|
精确率(Precision):
Python |
---|
| precision = TP / (TP + FP)
|
召回率(Recall):
F1分数:
Python |
---|
| f1 = 2 * (precision * recall) / (precision + recall)
|
混淆矩阵:
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | from sklearn.metrics import confusion_matrix, classification_report
import seaborn as sns
# 计算混淆矩阵
cm = confusion_matrix(y_test, y_pred)
# 可视化
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.title('Confusion Matrix')
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
plt.show()
# 详细报告
print(classification_report(y_test, y_pred))
|
回归指标
均方误差(MSE):
Python |
---|
| from sklearn.metrics import mean_squared_error
mse = mean_squared_error(y_test, y_pred)
|
均方根误差(RMSE):
Python |
---|
| import numpy as np
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
|
平均绝对误差(MAE):
Python |
---|
| from sklearn.metrics import mean_absolute_error
mae = mean_absolute_error(y_test, y_pred)
|
R²分数:
Python |
---|
| from sklearn.metrics import r2_score
r2 = r2_score(y_test, y_pred)
|
特征工程
1. 特征选择
单变量选择:
Python |
---|
| from sklearn.feature_selection import SelectKBest, f_classif
selector = SelectKBest(score_func=f_classif, k=10)
X_selected = selector.fit_transform(X, y)
|
递归特征消除:
Python |
---|
| from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression
estimator = LogisticRegression()
selector = RFE(estimator, n_features_to_select=10)
X_selected = selector.fit_transform(X, y)
|
2. 特征创建
多项式特征:
Python |
---|
| from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(degree=2, include_bias=False)
X_poly = poly.fit_transform(X)
|
交互特征:
Python |
---|
| # 手动创建交互特征
df['feature1_x_feature2'] = df['feature1'] * df['feature2']
|
过拟合与欠拟合
识别过拟合和欠拟合
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | from sklearn.model_selection import validation_curve
import matplotlib.pyplot as plt
# 验证曲线
train_scores, val_scores = validation_curve(
estimator, X, y, param_name='max_depth',
param_range=range(1, 21), cv=5
)
# 绘制学习曲线
plt.plot(range(1, 21), train_scores.mean(axis=1), label='Training Score')
plt.plot(range(1, 21), val_scores.mean(axis=1), label='Validation Score')
plt.xlabel('Max Depth')
plt.ylabel('Score')
plt.legend()
plt.show()
|
解决方法
过拟合:
- 增加训练数据
- 特征选择
- 正则化
- 早停法
- 集成方法
欠拟合:
- 增加模型复杂度
- 添加特征
- 减少正则化
- 增加训练时间
交叉验证
K折交叉验证
Python |
---|
| from sklearn.model_selection import cross_val_score, KFold
# K折交叉验证
kfold = KFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(model, X, y, cv=kfold, scoring='accuracy')
print(f"平均准确率: {scores.mean():.3f} (+/- {scores.std() * 2:.3f})")
|
分层交叉验证
Python |
---|
| from sklearn.model_selection import StratifiedKFold
# 分层K折交叉验证(保持类别比例)
skfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(model, X, y, cv=skfold, scoring='accuracy')
|
实践工具
1. Python库
核心库:
- NumPy:数值计算
- Pandas:数据处理
- Matplotlib/Seaborn:数据可视化
- Scikit-learn:机器学习算法
深度学习:
- TensorFlow:Google开发的深度学习框架
- PyTorch:Facebook开发的深度学习框架
- Keras:高级神经网络API
其他有用库:
- XGBoost:梯度提升算法
- LightGBM:微软开发的梯度提升框架
- CatBoost:Yandex开发的梯度提升库
2. 开发环境
Jupyter Notebook:
Bash |
---|
| pip install jupyter
jupyter notebook
|
Google Colab:
- 免费的云端Jupyter环境
- 提供免费GPU/TPU
- 预装常用机器学习库
Anaconda:
Bash |
---|
| # 安装Anaconda
# 创建虚拟环境
conda create -n ml_env python=3.8
conda activate ml_env
conda install scikit-learn pandas numpy matplotlib seaborn
|
3. 数据集资源
内置数据集:
Python |
---|
| from sklearn.datasets import load_iris, load_boston, load_wine
# 加载鸢尾花数据集
iris = load_iris()
X, y = iris.data, iris.target
|
在线数据集:
- Kaggle:竞赛和数据集平台
- UCI ML Repository:经典机器学习数据集
- Google Dataset Search:谷歌数据集搜索
- AWS Open Data:亚马逊开放数据
学习路径
初学者路径
- 数学基础
- 线性代数
- 概率统计
-
微积分基础
-
编程基础
- Python基础语法
- NumPy和Pandas
-
数据可视化
-
机器学习概念
- 监督学习vs无监督学习
- 训练、验证、测试
-
过拟合和欠拟合
-
实践项目
- 房价预测(回归)
- 鸢尾花分类(分类)
- 客户细分(聚类)
进阶路径
- 高级算法
- 集成学习
- 神经网络基础
-
深度学习入门
-
特征工程
- 特征选择和创建
- 降维技术
-
文本和图像特征提取
-
模型优化
- 超参数调优
- 模型集成
-
自动机器学习(AutoML)
-
实际应用
- 推荐系统
- 自然语言处理
- 计算机视觉
实际应用
1. 推荐系统
协同过滤:
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
# 用户-物品评分矩阵
user_item_matrix = np.array([
[5, 3, 0, 1],
[4, 0, 0, 1],
[1, 1, 0, 5],
[1, 0, 0, 4],
[0, 1, 5, 4]
])
# 计算用户相似度
user_similarity = cosine_similarity(user_item_matrix)
print("用户相似度矩阵:")
print(user_similarity)
|
2. 文本分类
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
# 创建文本分类管道
text_classifier = Pipeline([
('tfidf', TfidfVectorizer(stop_words='english')),
('classifier', MultinomialNB())
])
# 训练模型
text_classifier.fit(X_train_text, y_train)
# 预测
predictions = text_classifier.predict(X_test_text)
|
3. 时间序列预测
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | import pandas as pd
from sklearn.linear_model import LinearRegression
# 创建时间特征
def create_time_features(df, date_column):
df[date_column] = pd.to_datetime(df[date_column])
df['year'] = df[date_column].dt.year
df['month'] = df[date_column].dt.month
df['day'] = df[date_column].dt.day
df['dayofweek'] = df[date_column].dt.dayofweek
return df
# 滑动窗口特征
def create_lag_features(df, target_column, lags):
for lag in lags:
df[f'{target_column}_lag_{lag}'] = df[target_column].shift(lag)
return df
|
4. 异常检测
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13 | from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import StandardScaler
# 标准化数据
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# 异常检测
isolation_forest = IsolationForest(contamination=0.1, random_state=42)
anomalies = isolation_forest.fit_predict(X_scaled)
# -1表示异常,1表示正常
print(f"检测到 {sum(anomalies == -1)} 个异常点")
|
总结
机器学习是一个广阔而深入的领域,本文档提供了入门所需的基础知识和实践指导。学习机器学习需要:
- 扎实的数学基础:线性代数、概率统计、微积分
- 编程技能:Python、数据处理、可视化
- 理论理解:算法原理、模型评估、特征工程
- 实践经验:项目实战、问题解决、持续学习
记住,机器学习是一个实践性很强的领域,理论学习必须与实际项目相结合。建议从简单的项目开始,逐步提高复杂度,在实践中加深理解。
最后更新时间:2024年