当前位置:首页>学习笔记>Python机器学习基础教程学习笔记

Python机器学习基础教程学习笔记

  • 2026-03-14 14:51:35
Python机器学习基础教程学习笔记

Python机器学习基础教程 - 学习笔记


第1章 引言

核心思想: 机器学习是从数据中提取知识的方法,Python配合scikit-learn库提供了简单高效的机器学习实现途径。

1.1 机器学习是什么

是什么? 从数据中提取知识的交叉学科领域,也称为预测分析或统计学习。

为什么需要? 传统"if/else"规则系统存在两个缺点:

  • 逻辑只适用于单一领域,任务变化需要重写整个系统
  • 需要对人类专家的决策过程有深刻理解

作用? 自动从数据中学习规律,用于推荐系统、图像识别、医疗诊断等。


1.2 为何选择Python

是什么? 机器学习领域最常用的编程语言之一。

为什么需要?

  • 简单易学,语法清晰
  • 拥有丰富的科学计算库(NumPy、SciPy、pandas、matplotlib)
  • scikit-learn提供了统一的机器学习接口

1.3 scikit-learn

是什么? Python中最广泛使用的机器学习库。

作用?

  • 提供统一的API设计(fit/predict/transform)
  • 包含分类、回归、聚类、降维等算法
  • 提供模型选择和评估工具

对应代码:

# 基本导入import numpy as npimport matplotlib.pyplot as pltimport pandas as pdimport mglearn# 检查版本import sklearnprint("scikit-learn version: {}".format(sklearn.__version__))

1.4 第一个应用:鸢尾花分类

是什么? 经典的机器学习入门案例,根据鸢尾花的测量数据预测品种。

问题类型: 监督学习、分类问题、三分类

对应代码:

from sklearn.datasets import load_irisfrom sklearn.model_selection import train_test_splitfrom sklearn.neighbors import KNeighborsClassifier# 加载数据iris_dataset = load_iris()# 查看数据print("Keys of iris_dataset: \n{}".format(iris_dataset.keys()))print("Target names: {}".format(iris_dataset['target_names']))print("Feature names: \n{}".format(iris_dataset['feature_names']))print("Shape of data: {}".format(iris_dataset['data'].shape))# 划分训练集和测试集X_train, X_test, y_train, y_test = train_test_split(    iris_dataset['data'], iris_dataset['target'], random_state=0)# 构建模型knn = KNeighborsClassifier(n_neighbors=1)knn.fit(X_train, y_train)# 预测与评估y_pred = knn.predict(X_test)print("Test set score: {:.2f}".format(knn.score(X_test, y_test)))

第2章 监督学习

核心思想: 监督学习从带标签数据中学习映射关系,关键是理解模型复杂度与泛化能力之间的权衡。

2.1 分类与回归

是什么?

  • 分类
    :预测离散类别标签
  • 回归
    :预测连续数值

对比表格:

特性
分类
回归
输出类型
离散类别
连续数值
典型算法
k-NN、决策树、SVM、朴素贝叶斯
线性回归、决策树回归、k-NN回归
评估指标
准确率、F1分数、AUC
R²、MSE、MAE

2.2 泛化、过拟合与欠拟合

是什么?

  • 泛化
    :模型在新数据上的表现能力
  • 过拟合
    :模型过于复杂,记住训练数据但泛化能力差
  • 欠拟合
    :模型过于简单,无法捕捉数据规律

为什么需要理解? 这是模型调参的核心依据。

[由此引出:模型复杂度选择]→ 通过训练集和测试集性能对比判断→ 使用交叉验证评估泛化能力→ 通过正则化控制模型复杂度

模型复杂度与精度的关系:

情况
训练集精度
测试集精度
说明
欠拟合
模型过于简单
合适
模型复杂度适中
过拟合
很高
模型过于复杂

2.3 k近邻算法(k-NN)

是什么? 最简单的机器学习算法,通过找到最近的k个邻居进行预测。

为什么需要? 作为基准模型,易于理解和实现。

作用? 分类任务采用投票法,回归任务采用平均值。

局限?

  • 预测速度慢(需要计算与所有训练点的距离)
  • 对高维数据效果差(维度灾难)
  • 对特征缩放敏感

[由此引出:特征缩放的重要性]→ 使用StandardScaler、MinMaxScaler等进行预处理

对应代码:

from sklearn.neighbors import KNeighborsClassifier, KNeighborsRegressor# k-NN分类clf = KNeighborsClassifier(n_neighbors=3)clf.fit(X_train, y_train)print("Test set accuracy: {:.2f}".format(clf.score(X_test, y_test)))# k-NN回归reg = KNeighborsRegressor(n_neighbors=3)reg.fit(X_train, y_train)print("Test set R^2: {:.2f}".format(reg.score(X_test, y_test)))

k值选择的影响:

k值
模型复杂度
特点
决策边界复杂,容易过拟合
决策边界平滑,可能欠拟合

2.4 线性模型

是什么? 通过特征的线性组合进行预测的模型。

为什么需要? 简单、快速、可解释性强,是首选的基准模型。

2.4.1 线性回归

公式:

对应代码:

from sklearn.linear_model import LinearRegression, Ridge, Lasso# 普通线性回归lr = LinearRegression()lr.fit(X_train, y_train)print("Coefficients: {}".format(lr.coef_))print("Intercept: {}".format(lr.intercept_))# Ridge回归(L2正则化)ridge = Ridge(alpha=1.0)ridge.fit(X_train, y_train)# Lasso回归(L1正则化)lasso = Lasso(alpha=1.0)lasso.fit(X_train, y_train)

2.4.2 线性分类

Logistic回归:

公式:

线性SVM:

对应代码:

from sklearn.linear_model import LogisticRegressionfrom sklearn.svm import LinearSVC# Logistic回归logreg = LogisticRegression(C=1.0)logreg.fit(X_train, y_train)# 线性SVMlinear_svm = LinearSVC(C=1.0)linear_svm.fit(X_train, y_train)

正则化对比:

方法
正则化类型
特点
Ridge
L2
系数趋向于小但不为零
Lasso
L1
产生稀疏解,部分系数为零
ElasticNet
L1+L2
结合两者优点

正则化参数C:

C值
正则化强度
模型复杂度

2.5 朴素贝叶斯

是什么? 基于贝叶斯定理的分类器,假设特征之间相互独立。

为什么需要? 训练速度快,适用于高维稀疏数据(如文本)。

作用? 作为基准模型,常用于文本分类。

局限? 假设特征独立,实际中很少成立。

对应代码:

from sklearn.naive_bayes import GaussianNB, MultinomialNB, BernoulliNB# 高斯朴素贝叶斯(连续特征)gnb = GaussianNB()gnb.fit(X_train, y_train)# 多项式朴素贝叶斯(计数特征)mnb = MultinomialNB(alpha=0.1)mnb.fit(X_train, y_train)

2.6 决策树

是什么? 通过一系列if/else问题对数据进行划分的模型。

为什么需要? 易于理解和可视化,不需要特征缩放。

作用? 分类和回归任务,特征重要性分析。

局限?

  • 容易过拟合
  • 泛化能力差
  • 对数据微小变化敏感

[由此引出:决策树集成方法]→ 随机森林→ 梯度提升树

对应代码:

from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor# 决策树分类tree = DecisionTreeClassifier(max_depth=4, random_state=0)tree.fit(X_train, y_train)# 特征重要性print("Feature importances:\n{}".format(tree.feature_importances_))# 决策树回归tree_reg = DecisionTreeRegressor(max_depth=4)tree_reg.fit(X_train, y_train)

预剪枝参数:

参数
作用
max_depth
树的最大深度
max_leaf_nodes
最大叶节点数
min_samples_leaf
叶节点最小样本数

2.7 决策树集成

是什么? 合并多个决策树构建更强大的模型。

为什么需要? 单棵决策树容易过拟合,集成可以提高泛化能力。

2.7.1 随机森林

是什么? 多棵决策树的集合,每棵树使用随机特征子集。

作用? 减少过拟合,提高泛化能力。

对应代码:

from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor# 随机森林分类forest = RandomForestClassifier(n_estimators=100, random_state=0)forest.fit(X_train, y_train)# 随机森林回归forest_reg = RandomForestRegressor(n_estimators=100)forest_reg.fit(X_train, y_train)

2.7.2 梯度提升树

是什么? 串行训练多棵树,每棵树纠正前一棵树的错误。

作用? 通常比随机森林精度更高。

局限? 训练速度慢,需要更多参数调节。

对应代码:

from sklearn.ensemble import GradientBoostingClassifier, GradientBoostingRegressor# 梯度提升分类gbrt = GradientBoostingClassifier(random_state=0)gbrt.fit(X_train, y_train)# 梯度提升回归gbrt_reg = GradientBoostingRegressor(random_state=0)gbrt_reg.fit(X_train, y_train)

集成方法对比:

方法
训练方式
特点
随机森林
并行
鲁棒性好,不易过拟合
梯度提升
串行
精度高,需要调参

2.8 核支持向量机(SVM)

是什么? 通过核函数将数据映射到高维空间进行分类。

为什么需要? 处理非线性可分数据。

作用? 复杂决策边界,适用于中小规模数据集。

局限?

  • 对参数敏感
  • 需要特征缩放
  • 大规模数据集训练慢

对应代码:

from sklearn.svm import SVC, SVR# SVM分类svm = SVC(kernel='rbf', C=1.0, gamma=0.1)svm.fit(X_train, y_train)# SVM回归svm_reg = SVR(kernel='rbf', C=1.0, gamma=0.1)svm_reg.fit(X_train, y_train)

核函数对比:

核函数
适用场景
linear
线性可分数据
rbf
非线性数据(默认)
poly
多项式关系

2.9 神经网络(深度学习)

是什么? 多层感知机,通过非线性激活函数学习复杂模式。

为什么需要? 可以学习非常复杂的非线性关系。

作用? 图像、文本等复杂数据的建模。

局限?

  • 需要大量数据
  • 需要特征缩放
  • 对参数敏感
  • 训练时间长

对应代码:

from sklearn.neural_network import MLPClassifier, MLPRegressor# 多层感知机分类mlp = MLPClassifier(hidden_layer_sizes=[100100],                     random_state=0, max_iter=1000)mlp.fit(X_train, y_train)# 多层感知机回归mlp_reg = MLPRegressor(hidden_layer_sizes=[100],                        random_state=0, max_iter=1000)mlp_reg.fit(X_train, y_train)

2.10 分类器的不确定度估计

是什么? 评估分类器对预测结果的置信程度。

为什么需要? 了解预测的可靠性。

方法:

  • decision_function
    :返回决策函数值
  • predict_proba
    :返回类别概率

对应代码:

# 决策函数print("Decision function:\n{}".format(gbrt.decision_function(X_test)[:6]))# 预测概率print("Predicted probabilities:\n{}".format(gbrt.predict_proba(X_test)[:6]))

2.11 算法选择指南

对比表格:

算法
适用场景
优点
缺点
k-NN
小型数据集
简单,易解释
预测慢,高维效果差
线性模型
大型/高维数据
快速,可解释
只能学习线性关系
朴素贝叶斯
文本/高维数据
极快
假设特征独立
决策树
需要可视化
易解释,无需缩放
容易过拟合
随机森林
通用场景
鲁棒,强大
不适用于高维稀疏数据
梯度提升
追求高精度
精度高
训练慢,需调参
SVM
中小型数据
强大
需缩放,对参数敏感
神经网络
复杂问题
学习复杂模式
需大量数据,训练慢

第3章 无监督学习与预处理

核心思想: 无监督学习从无标签数据中提取知识,常用于数据探索和预处理。

3.1 预处理与缩放

是什么? 对特征进行缩放和变换,使其适合机器学习算法。

为什么需要? 许多算法对特征的尺度敏感(如SVM、神经网络、k-NN)。

对比表格:

方法
公式
特点
StandardScaler
均值为0,方差为1
MinMaxScaler
缩放到[0,1]
RobustScaler
使用中位数和四分位数
对异常值鲁棒
Normalizer
归一化到单位范数
适用于文本数据

对应代码:

from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler# StandardScalerscaler = StandardScaler()X_scaled = scaler.fit_transform(X)# MinMaxScalerscaler = MinMaxScaler()X_scaled = scaler.fit_transform(X)# 重要:训练集和测试集使用相同的缩放scaler = StandardScaler().fit(X_train)X_train_scaled = scaler.transform(X_train)X_test_scaled = scaler.transform(X_test)

3.2 主成分分析(PCA)

是什么? 降维技术,找到数据中方差最大的方向。

为什么需要?

  • 降维可视化
  • 去除噪声
  • 减少特征数量

作用? 将数据投影到低维空间,保留最大方差。

局限? 只捕捉线性关系。

[由此引出:非线性降维方法]→ t-SNE用于可视化→ 核PCA捕捉非线性关系

对应代码:

from sklearn.decomposition import PCA# PCA降维pca = PCA(n_components=2)X_pca = pca.fit_transform(X)# 解释方差比print("Explained variance ratio: {}".format(pca.explained_variance_ratio_))# 选择保留90%方差的组件数pca = PCA(n_components=0.90, whiten=True)X_pca = pca.fit_transform(X_train)X_test_pca = pca.transform(X_test)

3.3 非负矩阵分解(NMF)

是什么? 将数据分解为非负分量的乘积。

为什么需要? 适用于非负数据(如图像、文本),结果具有可解释性。

作用? 提取数据的"部分"或"极值"。

局限? 要求数据必须非负。

对应代码:

from sklearn.decomposition import NMFnmf = NMF(n_components=15, random_state=0)nmf.fit(X_train)X_train_nmf = nmf.transform(X_train)X_test_nmf = nmf.transform(X_test)

3.4 t-SNE

是什么? 流形学习算法,用于高维数据的可视化。

为什么需要? PCA无法捕捉数据的非线性结构。

作用? 将高维数据映射到2D/3D进行可视化。

局限?

  • 不适用于新数据(没有transform方法)
  • 计算量大
  • 结果依赖于随机初始化

对应代码:

from sklearn.manifold import TSNEtsne = TSNE(random_state=42)X_tsne = tsne.fit_transform(X)plt.scatter(X_tsne[:, 0], X_tsne[:, 1], c=y)plt.xlabel("t-SNE feature 0")plt.ylabel("t-SNE feature 1")

3.5 聚类

是什么? 将数据划分为不同的组,每组包含相似的样本。

为什么需要?

  • 数据探索
  • 客户分群
  • 图像分割

3.5.1 k均值聚类

是什么? 将数据划分为k个簇,使簇内样本到中心的距离最小。

作用? 快速聚类,易于实现。

局限?

  • 需要预先指定k值
  • 假设簇是球形
  • 对异常值敏感

对应代码:

from sklearn.cluster import KMeanskmeans = KMeans(n_clusters=3, random_state=0)kmeans.fit(X)# 簇分配labels = kmeans.labels_# 簇中心print("Cluster centers:\n{}".format(kmeans.cluster_centers_))# 预测新数据predictions = kmeans.predict(X_new)# 到簇中心的距离distance_features = kmeans.transform(X)

3.5.2 凝聚聚类

是什么? 自底向上合并最相似的簇,直到达到指定簇数。

作用? 提供层次化的聚类结果。

链接方式:

链接方式
说明
ward
方差增加最小(默认)
average
平均距离最小
complete
最大距离最小

对应代码:

from sklearn.cluster import AgglomerativeClusteringagg = AgglomerativeClustering(n_clusters=3)assignment = agg.fit_predict(X)

3.5.3 DBSCAN

是什么? 基于密度的聚类,不需要预先指定簇数。

为什么需要? 可以识别任意形状的簇,找出异常点。

作用? 发现密集区域,将低密度点标记为噪声。

局限? 对参数eps和min_samples敏感。

对应代码:

from sklearn.cluster import DBSCANdbscan = DBSCAN(eps=0.5, min_samples=5)clusters = dbscan.fit_predict(X)# -1表示噪声点print("Cluster memberships:\n{}".format(clusters))

聚类算法对比:

算法
需要指定簇数
适用形状
特点
k均值
球形
快速,最常用
凝聚聚类
任意
层次化结果
DBSCAN
任意
可识别噪声点

第4章 数据表示与特征工程

核心思想: 数据的表示方式对模型性能有重大影响,特征工程是从原始数据中提取有效特征的过程。

4.1 分类变量

是什么? 取值为离散类别的特征。

为什么需要处理? 机器学习模型需要数值输入。

4.1.1 One-Hot编码

是什么? 将分类变量转换为二进制特征。

作用? 使分类变量可以被模型使用。

局限? 高基数的分类变量会产生大量特征。

对应代码:

from sklearn.preprocessing import OneHotEncoder# One-Hot编码enc = OneHotEncoder(sparse=False)enc.fit(X_categorical)X_onehot = enc.transform(X_categorical)# pandas的get_dummiesimport pandas as pdX_onehot = pd.get_dummies(df, columns=['category'])

4.2 分箱(离散化)

是什么? 将连续特征划分为若干个区间。

为什么需要? 帮助线性模型学习非线性关系。

作用? 将连续特征转换为有序类别。

对应代码:

from sklearn.preprocessing import KBinsDiscretizer# 分箱enc = KBinsDiscretizer(n_bins=10, encode='onehot-dense')X_binned = enc.fit_transform(X)# 结合原始特征和分箱特征X_combined = np.hstack([X, X_binned])

4.3 交互特征与多项式特征

是什么? 通过原始特征的组合创建新特征。

为什么需要? 帮助线性模型学习特征之间的关系。

作用? 增加模型的表达能力。

对应代码:

from sklearn.preprocessing import PolynomialFeatures# 多项式特征poly = PolynomialFeatures(degree=2, include_bias=False)X_poly = poly.fit_transform(X)print("X_poly.shape: {}".format(X_poly.shape))print("Polynomial feature names:\n{}".format(poly.get_feature_names()))

4.4 单变量非线性变换

是什么? 对特征应用数学函数(log、exp、sin等)。

为什么需要? 调节数据的相对比例,使其更接近高斯分布。

常用变换:

变换
适用场景
log
右偏分布的计数数据
exp
左偏分布
sin/cos
周期性数据

对应代码:

# log变换X_log = np.log(X + 1)# exp变换X_exp = np.exp(X)

4.5 自动化特征选择

是什么? 自动选择最有用的特征子集。

为什么需要? 减少特征数量,提高模型可解释性,减少过拟合。

4.5.1 单变量统计

是什么? 基于单变量统计检验选择特征。

对应代码:

from sklearn.feature_selection import SelectKBest, f_classif# 选择k个最佳特征select = SelectKBest(f_classif, k=10)X_selected = select.fit_transform(X_train, y_train)# 查看选中的特征mask = select.get_support()print(mask)

4.5.2 基于模型的特征选择

是什么? 使用监督学习模型评估特征重要性。

对应代码:

from sklearn.feature_selection import SelectFromModelfrom sklearn.ensemble import RandomForestClassifier# 使用随机森林选择特征select = SelectFromModel(    RandomForestClassifier(n_estimators=100, random_state=42),    threshold="median")select.fit(X_train, y_train)X_selected = select.transform(X_train)

4.5.3 迭代特征选择

是什么? 通过迭代构建模型来选择特征。

对应代码:

from sklearn.feature_selection import RFE# 递归特征消除select = RFE(RandomForestClassifier(n_estimators=100, random_state=42),             n_features_to_select=40)select.fit(X_train, y_train)X_selected = select.transform(X_train)

第5章 模型评估与改进

核心思想: 正确评估模型性能是机器学习成功的关键,交叉验证和网格搜索是模型选择和调参的核心工具。

5.1 交叉验证

是什么? 将数据多次划分训练集和验证集进行评估。

为什么需要? 更可靠地评估模型泛化能力。

作用? 减少数据划分的随机性对评估结果的影响。

对应代码:

from sklearn.model_selection import cross_val_score, KFold, StratifiedKFold# 基本交叉验证scores = cross_val_score(LogisticRegression(), X, y, cv=5)print("Cross-validation scores: {}".format(scores))print("Average cross-validation score: {:.2f}".format(scores.mean()))# 分层k折交叉验证(分类问题)stratified_kfold = StratifiedKFold(n_splits=5)scores = cross_val_score(LogisticRegression(), X, y, cv=stratified_kfold)

交叉验证策略对比:

方法
适用场景
KFold
回归问题
StratifiedKFold
分类问题(保持类别比例)
LeaveOneOut
小数据集
ShuffleSplit
大数据集

5.2 网格搜索

是什么? 系统地遍历多种参数组合,寻找最佳参数。

为什么需要? 自动化参数调优过程。

5.2.1 简单网格搜索

对应代码:

from sklearn.model_selection import GridSearchCV# 定义参数网格param_grid = {'C': [0.0010.010.1110100],'gamma': [0.0010.010.1110100]}# 网格搜索grid_search = GridSearchCV(SVC(), param_grid, cv=5)grid_search.fit(X_train, y_train)print("Best parameters: {}".format(grid_search.best_params_))print("Best cross-validation score: {:.2f}".format(grid_search.best_score_))print("Test set score: {:.2f}".format(grid_search.score(X_test, y_test)))

5.2.2 随机搜索

是什么? 随机采样参数组合,而非遍历所有组合。

为什么需要? 参数空间大时,网格搜索计算量过大。

对应代码:

from sklearn.model_selection import RandomizedSearchCVfrom scipy.stats import randint# 定义参数分布param_dist = {'C': randint(1100),'gamma': randint(1100)}# 随机搜索random_search = RandomizedSearchCV(SVC(), param_dist, n_iter=20, cv=5)random_search.fit(X_train, y_train)

网格搜索 vs 随机搜索:

方法
适用场景
特点
GridSearchCV
参数空间小
穷举所有组合
RandomizedSearchCV
参数空间大
随机采样,更高效

5.3 评估指标

是什么? 量化模型性能的指标。

为什么需要? 不同问题需要不同的评估标准。

5.3.1 二分类指标

混淆矩阵:

预测负类
预测正类
真实负类
TN
FP
真实正类
FN
TP

重要指标:

准确率

精确率

召回率

F1分数

对应代码:

from sklearn.metrics import (confusion_matrix, accuracy_score,                              precision_score, recall_score, f1_score,                             classification_report, roc_curve, auc)# 混淆矩阵confusion = confusion_matrix(y_test, pred)print("Confusion matrix:\n{}".format(confusion))# 分类报告print(classification_report(y_test, pred,                             target_names=["class 0""class 1"]))# ROC曲线fpr, tpr, thresholds = roc_curve(y_test, model.predict_proba(X_test)[:, 1])plt.plot(fpr, tpr, label="ROC Curve")plt.xlabel("FPR")plt.ylabel("TPR (recall)")plt.legend(loc=4)

不平衡数据的评估:

指标
适用场景
准确率
平衡数据
F1分数
不平衡数据
ROC-AUC
关注排序能力
PR曲线
极度不平衡数据

5.3.2 多分类指标

对应代码:

# 多分类指标print(classification_report(y_test, pred))# 多分类混淆矩阵confusion = confusion_matrix(y_test, pred)

5.3.3 回归指标

指标
公式
特点
决定系数,1为完美预测
MSE
均方误差
MAE
平均绝对误差

对应代码:

from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_errorprint("R^2: {:.2f}".format(r2_score(y_test, pred)))print("MSE: {:.2f}".format(mean_squared_error(y_test, pred)))print("MAE: {:.2f}".format(mean_absolute_error(y_test, pred)))

第6章 算法链与管道

核心思想: 管道将多个处理步骤串联起来,确保训练集和测试集的处理一致,简化工作流程。

6.1 为什么需要管道

是什么? 将数据预处理和模型训练封装为一个对象。

为什么需要?

  • 避免数据泄露(测试集信息泄露到训练过程)
  • 简化交叉验证和网格搜索
  • 确保训练集和测试集使用相同的预处理

[由此引出:管道的重要性]→ 在网格搜索中正确应用预处理→ 简化模型部署


6.2 构建管道

对应代码:

from sklearn.pipeline import Pipelinefrom sklearn.preprocessing import StandardScalerfrom sklearn.svm import SVC# 构建管道pipe = Pipeline([("scaler", StandardScaler()),                  ("svm", SVC())])# 拟合管道pipe.fit(X_train, y_train)# 预测print("Test set score: {:.2f}".format(pipe.score(X_test, y_test)))

6.3 在网格搜索中使用管道

对应代码:

# 定义参数网格param_grid = {'svm__C': [0.0010.010.1110100],'svm__gamma': [0.0010.010.1110100]}# 网格搜索grid = GridSearchCV(pipe, param_grid=param_grid, cv=5)grid.fit(X_train, y_train)print("Best cross-validation accuracy: {:.2f}".format(grid.best_score_))print("Test set score: {:.2f}".format(grid.score(X_test, y_test)))print("Best parameters: {}".format(grid.best_params_))

6.4 便捷的管道创建

对应代码:

from sklearn.pipeline import make_pipeline# 简化语法pipe = make_pipeline(StandardScaler(), SVC())# 查看步骤名称print("Pipeline steps:\n{}".format(pipe.steps))

6.5 访问管道属性

对应代码:

# 访问步骤属性pipe.fit(X_train, y_train)print("StandardScaler mean:\n{}".format(pipe.named_steps["scaler"].mean_))# 访问网格搜索中的属性print("Best estimator:\n{}".format(grid.best_estimator_))print("Scaler step:\n{}".format(grid.best_estimator_.named_steps["scaler"]))print("SVC step:\n{}".format(grid.best_estimator_.named_steps["svm"]))

第7章 处理文本数据

核心思想: 文本数据需要通过特征提取转换为数值表示,词袋模型是最基础也是最有效的方法之一。

7.1 词袋模型

是什么? 将文本表示为单词出现次数的向量。

为什么需要? 机器学习模型需要数值输入。

处理步骤:

  1. 分词
    :将文档划分为单词
  2. 构建词表
    :收集所有出现的单词
  3. 编码
    :计算每个单词的出现次数

局限?

  • 丢失词序信息
  • 丢失语义信息
  • 产生高维稀疏矩阵

[由此引出:改进文本表示]→ 停用词过滤→ tf-idf加权→ n-gram特征


7.2 CountVectorizer

对应代码:

from sklearn.feature_extraction.text import CountVectorizer# 创建向量化器vect = CountVectorizer()vect.fit(text_train)# 转换文本X_train = vect.transform(text_train)print("X_train:\n{}".format(repr(X_train)))# 查看词表feature_names = vect.get_feature_names()print("Number of features: {}".format(len(feature_names)))# 限制词表大小vect = CountVectorizer(min_df=5)  # 至少出现在5个文档中X_train = vect.fit_transform(text_train)

7.3 停用词

是什么? 常见但信息量低的词(如"the"、"is"、"and")。

为什么需要? 减少特征数量,去除噪声。

对应代码:

# 使用停用词vect = CountVectorizer(min_df=5, stop_words="english")X_train = vect.fit_transform(text_train)

7.4 tf-idf

是什么? 词频-逆文档频率,对常见词降权。

公式:

作用? 突出在特定文档中重要但在整个语料库中不常见的词。

对应代码:

from sklearn.feature_extraction.text import TfidfVectorizer# tf-idf向量化vect = TfidfVectorizer(min_df=5)X_train = vect.fit_transform(text_train)

7.5 n-gram

是什么? 考虑连续的n个单词作为特征。

为什么需要? 捕捉局部词序信息。

对应代码:

# 使用n-gramvect = CountVectorizer(ngram_range=(12))  # 1-gram和2-gramX_train = vect.fit_transform(text_train)print("Vocabulary size: {}".format(len(vect.vocabulary_)))

7.6 文本分类完整示例

对应代码:

from sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.linear_model import LogisticRegressionfrom sklearn.pipeline import make_pipelinefrom sklearn.model_selection import GridSearchCV# 构建管道pipe = make_pipeline(TfidfVectorizer(min_df=5),                      LogisticRegression())# 参数网格param_grid = {'logisticregression__C': [0.0010.010.1110]}# 网格搜索grid = GridSearchCV(pipe, param_grid, cv=5)grid.fit(text_train, y_train)print("Best cross-validation score: {:.2f}".format(grid.best_score_))print("Test set score: {:.2f}".format(grid.score(text_test, y_test)))

总结:机器学习工作流程

1. 数据加载与探索   ↓2. 数据预处理(缺失值、编码、缩放)   ↓3. 特征工程(选择、提取、变换)   ↓4. 模型选择(交叉验证比较)   ↓5. 超参数调优(网格搜索/随机搜索)   ↓6. 模型评估(测试集)   ↓7. 模型部署

核心概念速查表

监督学习算法

算法
分类/回归
关键参数
k-NN
两者
n_neighbors
线性回归
回归
alpha(正则化)
Ridge/Lasso
回归
alpha
Logistic回归
分类
C
线性SVM
分类
C
决策树
两者
max_depth
随机森林
两者
n_estimators
梯度提升
两者
n_estimators, learning_rate
神经网络
两者
hidden_layer_sizes

预处理

方法
用途
StandardScaler
标准化(均值为0,方差为1)
MinMaxScaler
缩放到[0,1]
OneHotEncoder
分类变量编码
PolynomialFeatures
多项式特征

模型评估

指标
用途
accuracy
平衡分类问题
precision/recall/f1
不平衡分类问题
ROC-AUC
关注排序能力
回归问题
MSE/MAE
回归问题

交叉验证与调参

方法
用途
cross_val_score
交叉验证评估
GridSearchCV
网格搜索
RandomizedSearchCV
随机搜索
Pipeline
封装预处理+模型

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-03-15 19:49:54 HTTP/2.0 GET : https://67808.cn/a/474062.html
  2. 运行时间 : 0.205004s [ 吞吐率:4.88req/s ] 内存消耗:4,532.86kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=8f5d3fc952b36fe6f70d0946427b9ac1
  1. /yingpanguazai/ssd/ssd1/www/no.67808.cn/public/index.php ( 0.79 KB )
  2. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/autoload.php ( 0.17 KB )
  3. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/composer/platform_check.php ( 0.90 KB )
  5. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/composer/autoload_static.php ( 4.90 KB )
  7. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  14. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  15. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  16. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  17. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  18. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  19. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  20. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  21. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  22. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/provider.php ( 0.19 KB )
  23. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  24. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  25. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  26. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/common.php ( 0.03 KB )
  27. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  28. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  29. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/app.php ( 0.95 KB )
  30. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/cache.php ( 0.78 KB )
  31. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/console.php ( 0.23 KB )
  32. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/cookie.php ( 0.56 KB )
  33. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/database.php ( 2.48 KB )
  34. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/filesystem.php ( 0.61 KB )
  36. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/lang.php ( 0.91 KB )
  37. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/log.php ( 1.35 KB )
  38. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/middleware.php ( 0.19 KB )
  39. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/route.php ( 1.89 KB )
  40. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/session.php ( 0.57 KB )
  41. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/trace.php ( 0.34 KB )
  42. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/view.php ( 0.82 KB )
  43. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/event.php ( 0.25 KB )
  44. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  45. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/service.php ( 0.13 KB )
  46. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/AppService.php ( 0.26 KB )
  47. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  48. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  49. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  50. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  51. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  52. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/services.php ( 0.14 KB )
  53. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  54. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  55. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  56. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  57. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  58. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  59. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  60. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  61. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  62. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  63. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  64. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  65. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  66. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  67. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  68. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  69. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  70. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  71. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  72. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  73. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  74. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  75. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  76. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  77. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  78. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  79. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  80. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  81. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  82. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  83. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/Request.php ( 0.09 KB )
  84. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  85. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/middleware.php ( 0.25 KB )
  86. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  87. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  88. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  89. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  90. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  91. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  92. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  93. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  94. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  95. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  96. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  97. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  98. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  99. /yingpanguazai/ssd/ssd1/www/no.67808.cn/route/app.php ( 1.72 KB )
  100. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  101. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  102. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  103. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/controller/Index.php ( 4.81 KB )
  104. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/BaseController.php ( 2.05 KB )
  105. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  106. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  107. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  108. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  109. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  110. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  111. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  112. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  113. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  114. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  115. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  116. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  117. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  118. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  119. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  120. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  121. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  122. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  123. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  124. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  125. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  126. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  127. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  128. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  129. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  130. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  131. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  132. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  133. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  134. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  135. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  136. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  137. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  138. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  139. /yingpanguazai/ssd/ssd1/www/no.67808.cn/runtime/temp/6df755f970a38e704c5414acbc6e8bcd.php ( 12.06 KB )
  140. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.000979s ] mysql:host=127.0.0.1;port=3306;dbname=no_67808;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.001480s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.000750s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.000722s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.001492s ]
  6. SELECT * FROM `set` [ RunTime:0.000731s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.001549s ]
  8. SELECT * FROM `article` WHERE `id` = 474062 LIMIT 1 [ RunTime:0.001353s ]
  9. UPDATE `article` SET `lasttime` = 1773575394 WHERE `id` = 474062 [ RunTime:0.033603s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 65 LIMIT 1 [ RunTime:0.001847s ]
  11. SELECT * FROM `article` WHERE `id` < 474062 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.001068s ]
  12. SELECT * FROM `article` WHERE `id` > 474062 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.007159s ]
  13. SELECT * FROM `article` WHERE `id` < 474062 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.001979s ]
  14. SELECT * FROM `article` WHERE `id` < 474062 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.002647s ]
  15. SELECT * FROM `article` WHERE `id` < 474062 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.001405s ]
0.209072s