《Python机器学习基础教程》第二章监督学习2.17 深度解析:决策函数在分类任务中的关键作用与实战应用 - , decision_function[:5])
2.17 深度解析:决策函数在分类任务中的关键作用与实战应用
#mermaid-svg-RKPacibEyJgB4zgD {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-RKPacibEyJgB4zgD .error-icon{fill:#552222;}#mermaid-svg-RKPacibEyJgB4zgD .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-RKPacibEyJgB4zgD .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-RKPacibEyJgB4zgD .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-RKPacibEyJgB4zgD .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-RKPacibEyJgB4zgD .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-RKPacibEyJgB4zgD .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-RKPacibEyJgB4zgD .marker{fill:#333333;stroke:#333333;}#mermaid-svg-RKPacibEyJgB4zgD .marker.cross{stroke:#333333;}#mermaid-svg-RKPacibEyJgB4zgD svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-RKPacibEyJgB4zgD .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-RKPacibEyJgB4zgD .cluster-label text{fill:#333;}#mermaid-svg-RKPacibEyJgB4zgD .cluster-label span{color:#333;}#mermaid-svg-RKPacibEyJgB4zgD .label text,#mermaid-svg-RKPacibEyJgB4zgD span{fill:#333;color:#333;}#mermaid-svg-RKPacibEyJgB4zgD .node rect,#mermaid-svg-RKPacibEyJgB4zgD .node circle,#mermaid-svg-RKPacibEyJgB4zgD .node ellipse,#mermaid-svg-RKPacibEyJgB4zgD .node polygon,#mermaid-svg-RKPacibEyJgB4zgD .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-RKPacibEyJgB4zgD .node .label{text-align:center;}#mermaid-svg-RKPacibEyJgB4zgD .node.clickable{cursor:pointer;}#mermaid-svg-RKPacibEyJgB4zgD .arrowheadPath{fill:#333333;}#mermaid-svg-RKPacibEyJgB4zgD .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-RKPacibEyJgB4zgD .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-RKPacibEyJgB4zgD .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-RKPacibEyJgB4zgD .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-RKPacibEyJgB4zgD .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-RKPacibEyJgB4zgD .cluster text{fill:#333;}#mermaid-svg-RKPacibEyJgB4zgD .cluster span{color:#333;}#mermaid-svg-RKPacibEyJgB4zgD div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-RKPacibEyJgB4zgD :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
2.17 决策函数的作用
2.17.1 决策函数的基本概念
2.17.2 决策函数在分类任务中的作用
2.17.3 常见分类器的决策函数
2.17.4 实战应用与代码示例
目录
2.17 决策函数的作用
2.17.1 决策函数的基本概念2.17.2 决策函数在分类任务中的作用2.17.3 常见分类器的决策函数2.17.4 实战应用与代码示例
2.17.1 决策函数的基本概念
决策函数是分类器输出的一个实数值,表示分类器对某个样本属于某一类别的置信度。对于二分类问题,决策函数通常是一个正负值,正值表示倾向于正类,负值表示倾向于负类。对于多分类问题,决策函数可以输出多个值,每个值对应一个类别。
数学公式
假设我们有一个二分类问题,决策函数 (f(x)) 可以表示为:
f
(
x
)
=
w
T
x
+
b
f(x) = w^T x + b
f(x)=wTx+b
其中 (w) 是权重向量,(b) 是偏置项,(x) 是输入特征向量。
图解
决策函数的基本概念可以用以下图示来说明:
#mermaid-svg-cJ2boKLUbzm6TKRc {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-cJ2boKLUbzm6TKRc .error-icon{fill:#552222;}#mermaid-svg-cJ2boKLUbzm6TKRc .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-cJ2boKLUbzm6TKRc .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-cJ2boKLUbzm6TKRc .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-cJ2boKLUbzm6TKRc .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-cJ2boKLUbzm6TKRc .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-cJ2boKLUbzm6TKRc .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-cJ2boKLUbzm6TKRc .marker{fill:#333333;stroke:#333333;}#mermaid-svg-cJ2boKLUbzm6TKRc .marker.cross{stroke:#333333;}#mermaid-svg-cJ2boKLUbzm6TKRc svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-cJ2boKLUbzm6TKRc .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-cJ2boKLUbzm6TKRc .cluster-label text{fill:#333;}#mermaid-svg-cJ2boKLUbzm6TKRc .cluster-label span{color:#333;}#mermaid-svg-cJ2boKLUbzm6TKRc .label text,#mermaid-svg-cJ2boKLUbzm6TKRc span{fill:#333;color:#333;}#mermaid-svg-cJ2boKLUbzm6TKRc .node rect,#mermaid-svg-cJ2boKLUbzm6TKRc .node circle,#mermaid-svg-cJ2boKLUbzm6TKRc .node ellipse,#mermaid-svg-cJ2boKLUbzm6TKRc .node polygon,#mermaid-svg-cJ2boKLUbzm6TKRc .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-cJ2boKLUbzm6TKRc .node .label{text-align:center;}#mermaid-svg-cJ2boKLUbzm6TKRc .node.clickable{cursor:pointer;}#mermaid-svg-cJ2boKLUbzm6TKRc .arrowheadPath{fill:#333333;}#mermaid-svg-cJ2boKLUbzm6TKRc .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-cJ2boKLUbzm6TKRc .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-cJ2boKLUbzm6TKRc .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-cJ2boKLUbzm6TKRc .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-cJ2boKLUbzm6TKRc .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-cJ2boKLUbzm6TKRc .cluster text{fill:#333;}#mermaid-svg-cJ2boKLUbzm6TKRc .cluster span{color:#333;}#mermaid-svg-cJ2boKLUbzm6TKRc div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-cJ2boKLUbzm6TKRc :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
决策函数的基本概念
定义
数学公式
2.17.2 决策函数在分类任务中的作用
决策函数在分类任务中扮演着至关重要的角色。它不仅帮助模型做出最终的分类决策,还提供了模型对预测结果的信心程度。具体来说,决策函数的作用包括:
分类决策:通过设定阈值(通常是0),决策函数可以将样本划分为不同的类别。置信度估计:决策函数的绝对值可以反映模型对预测结果的信心程度。绝对值越大,信心越高。异常检测:决策函数可以帮助识别那些远离决策边界的样本,这些样本可能是异常值。主动学习:通过选择那些决策函数接近阈值的样本进行标注,可以更有效地提高模型的性能。
图解
决策函数在分类任务中的作用可以用以下图示来说明:
#mermaid-svg-VE5TcOXXctwBdXSt {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-VE5TcOXXctwBdXSt .error-icon{fill:#552222;}#mermaid-svg-VE5TcOXXctwBdXSt .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-VE5TcOXXctwBdXSt .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-VE5TcOXXctwBdXSt .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-VE5TcOXXctwBdXSt .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-VE5TcOXXctwBdXSt .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-VE5TcOXXctwBdXSt .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-VE5TcOXXctwBdXSt .marker{fill:#333333;stroke:#333333;}#mermaid-svg-VE5TcOXXctwBdXSt .marker.cross{stroke:#333333;}#mermaid-svg-VE5TcOXXctwBdXSt svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-VE5TcOXXctwBdXSt .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-VE5TcOXXctwBdXSt .cluster-label text{fill:#333;}#mermaid-svg-VE5TcOXXctwBdXSt .cluster-label span{color:#333;}#mermaid-svg-VE5TcOXXctwBdXSt .label text,#mermaid-svg-VE5TcOXXctwBdXSt span{fill:#333;color:#333;}#mermaid-svg-VE5TcOXXctwBdXSt .node rect,#mermaid-svg-VE5TcOXXctwBdXSt .node circle,#mermaid-svg-VE5TcOXXctwBdXSt .node ellipse,#mermaid-svg-VE5TcOXXctwBdXSt .node polygon,#mermaid-svg-VE5TcOXXctwBdXSt .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-VE5TcOXXctwBdXSt .node .label{text-align:center;}#mermaid-svg-VE5TcOXXctwBdXSt .node.clickable{cursor:pointer;}#mermaid-svg-VE5TcOXXctwBdXSt .arrowheadPath{fill:#333333;}#mermaid-svg-VE5TcOXXctwBdXSt .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-VE5TcOXXctwBdXSt .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-VE5TcOXXctwBdXSt .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-VE5TcOXXctwBdXSt .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-VE5TcOXXctwBdXSt .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-VE5TcOXXctwBdXSt .cluster text{fill:#333;}#mermaid-svg-VE5TcOXXctwBdXSt .cluster span{color:#333;}#mermaid-svg-VE5TcOXXctwBdXSt div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-VE5TcOXXctwBdXSt :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
决策函数在分类任务中的作用
分类决策
置信度估计
异常检测
主动学习
2.17.3 常见分类器的决策函数
不同类型的分类器有不同的决策函数形式。下面我们将介绍几种常见分类器的决策函数。
逻辑回归
逻辑回归是一种常用的线性分类器,其决策函数是一个线性组合:
f
(
x
)
=
w
T
x
+
b
f(x) = w^T x + b
f(x)=wTx+b
支持向量机(SVM)
支持向量机(SVM)的决策函数也是一个线性组合:
f
(
x
)
=
w
T
x
+
b
f(x) = w^T x + b
f(x)=wTx+b
随机森林
随机森林是一种集成学习方法,通过多个决策树的投票来做出最终预测。随机森林的决策函数可以通过每个决策树的输出进行平均得到。
神经网络
神经网络的决策函数通常是最后一层的输出。对于二分类问题,常见的激活函数有Sigmoid和ReLU等。
图解
常见分类器的决策函数可以用以下图示来说明:
#mermaid-svg-VW3aMAfEzycL7oIz {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-VW3aMAfEzycL7oIz .error-icon{fill:#552222;}#mermaid-svg-VW3aMAfEzycL7oIz .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-VW3aMAfEzycL7oIz .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-VW3aMAfEzycL7oIz .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-VW3aMAfEzycL7oIz .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-VW3aMAfEzycL7oIz .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-VW3aMAfEzycL7oIz .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-VW3aMAfEzycL7oIz .marker{fill:#333333;stroke:#333333;}#mermaid-svg-VW3aMAfEzycL7oIz .marker.cross{stroke:#333333;}#mermaid-svg-VW3aMAfEzycL7oIz svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-VW3aMAfEzycL7oIz .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-VW3aMAfEzycL7oIz .cluster-label text{fill:#333;}#mermaid-svg-VW3aMAfEzycL7oIz .cluster-label span{color:#333;}#mermaid-svg-VW3aMAfEzycL7oIz .label text,#mermaid-svg-VW3aMAfEzycL7oIz span{fill:#333;color:#333;}#mermaid-svg-VW3aMAfEzycL7oIz .node rect,#mermaid-svg-VW3aMAfEzycL7oIz .node circle,#mermaid-svg-VW3aMAfEzycL7oIz .node ellipse,#mermaid-svg-VW3aMAfEzycL7oIz .node polygon,#mermaid-svg-VW3aMAfEzycL7oIz .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-VW3aMAfEzycL7oIz .node .label{text-align:center;}#mermaid-svg-VW3aMAfEzycL7oIz .node.clickable{cursor:pointer;}#mermaid-svg-VW3aMAfEzycL7oIz .arrowheadPath{fill:#333333;}#mermaid-svg-VW3aMAfEzycL7oIz .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-VW3aMAfEzycL7oIz .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-VW3aMAfEzycL7oIz .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-VW3aMAfEzycL7oIz .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-VW3aMAfEzycL7oIz .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-VW3aMAfEzycL7oIz .cluster text{fill:#333;}#mermaid-svg-VW3aMAfEzycL7oIz .cluster span{color:#333;}#mermaid-svg-VW3aMAfEzycL7oIz div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-VW3aMAfEzycL7oIz :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
常见分类器的决策函数
逻辑回归
支持向量机(SVM)
随机森林
神经网络
2.17.4 实战应用与代码示例
下面我们将通过一些具体的例子来展示如何使用Python和Pandas来实现决策函数的应用。
示例1:数据预处理
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# 读取数据
data = pd.read_csv("iris.csv")
X = data[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']]
y = data['species']
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 数据标准化
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
示例2:逻辑回归
from sklearn.linear_model import LogisticRegression
# 创建逻辑回归模型
model = LogisticRegression()
# 训练模型
model.fit(X_train, y_train)
# 获取决策函数
decision_function = model.decision_function(X_test)
print("决策函数:", decision_function[:5])
示例3:支持向量机(SVM)
from sklearn.svm import SVC
# 创建SVM模型
svm = SVC(kernel='linear')
# 训练模型
svm.fit(X_train, y_train)
# 获取决策函数
decision_function = svm.decision_function(X_test)
print("决策函数:", decision_function[:5])
示例4:随机森林
from sklearn.ensemble import RandomForestClassifier
# 创建随机森林模型
model = RandomForestClassifier(n_estimators=100, random_state=42)
# 训练模型
model.fit(X_train, y_train)
# 获取决策函数
decision_function = model.predict_proba(X_test)
print("决策函数:", decision_function[:5])
示例5:神经网络
from sklearn.neural_network import MLPClassifier
# 创建神经网络模型
model = MLPClassifier(hidden_layer_sizes=(100, 50), max_iter=200, random_state=42)
# 训练模型
model.fit(X_train, y_train)
# 获取决策函数
decision_function = model.predict_proba(X_test)
print("决策函数:", decision_function[:5])
示例6:分类决策
# 设定阈值
threshold = 0.0
# 根据决策函数进行分类
predictions = (decision_function > threshold).astype(int)
print("分类结果:", predictions[:5])
示例7:置信度估计
# 计算置信度
confidence = np.abs(decision_function)
print("置信度:", confidence[:5])
示例8:异常检测
# 找出决策函数绝对值较小的样本
anomaly_indices = np.where(np.abs(decision_function) < 0.1)[0]
anomaly_samples = X_test[anomaly_indices]
print("异常样本:", anomaly_samples)
示例9:主动学习
# 找出决策函数接近阈值的样本
uncertain_indices = np.argsort(np.abs(decision_function))[:5]
uncertain_samples = X_test[uncertain_indices]
print("需要标注的样本:", uncertain_samples)
示例10:综合应用
# 综合应用
# 1. 数据预处理
data = pd.read_csv("iris.csv")
X = data[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']]
y = data['species']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# 2. 训练模型
model = LogisticRegression()
model.fit(X_train, y_train)
# 3. 获取决策函数
decision_function = model.decision_function(X_test)
# 4. 分类决策 拼多多帮助力入口
threshold = 0.0
predictions = (decision_function > threshold).astype(int)
print("分类结果:", predictions[:5])
# 5. 置信度估计
confidence = np.abs(decision_function)
print("置信度:", confidence[:5])
# 6. 异常检测
anomaly_indices = np.where(np.abs(decision_function) < 0.1)[0]
anomaly_samples = X_test[anomaly_indices]
print("异常样本:", anomaly_samples)
# 7. 主动学习
uncertain_indices = np.argsort(np.abs(decision_function))[:5]
uncertain_samples = X_test[uncertain_indices]
print("需要标注的样本:", uncertain_samples)
参考文献
资料名称链接《Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow》https://www.oreilly.com/library/view/hands-on-machine-learning/9781492032632/《Pattern Recognition and Machine Learning》https://www.springer.com/gp/book/9780387310732《The Elements of Statistical Learning》https://web.stanford.edu/~hastie/ElemStatLearn/《Machine Learning: A Probabilistic Perspective》https://mitpress.mit.edu/books/machine-learning-probabilistic-perspective《Scikit-Learn Documentation》https://scikit-learn.org/stable/《Pandas Documentation》https://pandas.pydata.org/pandas-docs/stable/《TensorFlow Documentation》https://www.tensorflow.org/《Keras Documentation》https://keras.io/《PyTorch Documentation》https://pytorch.org/docs/stable/index.html《Deep Learning Book》http://www.deeplearningbook.org/
《Python机器学习基础教程》第二章监督学习2.17 深度解析:决策函数在分类任务中的关键作用与实战应用
文章链接