【问题标题】:plot a decision curve for logistic regression with gaussian Kernel用高斯核绘制逻辑回归的决策曲线
【发布时间】:2014-11-14 19:51:45
【问题描述】:

我尝试过使用具有多项式特征的逻辑回归,幸运的是它对我来说工作正常,而且我能够绘制决策曲线。我已将 map_feature 函数用于多项式特征。 (我参考了 Andrew 教授关于正则化逻辑回归的笔记):http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex5/ex5.html

现在我正在尝试使用高斯内核而不是采用多项式特征来实现相同的目标。幸运的是,我的成本函数 (j_theta) 工作正常,并且在每次迭代后都会减小,我得到了最终的 theta 值。 我现在面临的问题是如何在这里绘制决策边界

I am using Octave to develop the algorithms and plot the graphs..

以下是我的数据集大小的详细信息

原始数据集:

Data Set (x):  [20*3] where the first column is the intercept or the bias column

1.00  2.0000   1.0000
1.00  3.0000   1.0000
1.00  4.0000   1.0000
1.00  5.0000   2.0000
1.00  5.0000   3.0000
 .
 .
 .

实施高斯核后具有新特征的数据集

Data set (f) : [20*21] the first column is the intercept column with all values as 1

1.0000e+000  1.0000e+000  6.0653e-001  1.3534e-001  6.7379e-003 . . . . . . . . 
1.0000e+000  6.0653e-001  1.0000e+000  6.0653e-001  8.2085e-002 . . . . . . . .
1.0000e+000  1.3534e-001  6.0653e-001  1.0000e+000  3.6788e-001
1.0000e+000  6.7379e-003  8.2085e-002  3.6788e-001  1.0000e+000
.               .
.               . 
.               .
.               .
.               .

在我的新特征数据集 (f) 上应用梯度下降后得到的成本函数图是:

因此我得到了新的 theta 值:

theta: [21*1]
 3.8874e+000
 1.1747e-001
 3.5931e-002
-8.5937e-005
-1.2666e-001
-1.0584e-001
 .
 .
 .

我现在面临的问题是如何在具有新特征数据集和 theta 值的原始数据集上构建决策曲线。我不知道我该如何进行。

如果我能得到一些可以帮助我解决问题的线索、教程或链接,我会很高兴。

感谢您的帮助。谢谢

【问题讨论】:

    标签: matlab machine-learning octave


    【解决方案1】:

    引用的 Andrew 的note 实际上包含一个非常好的示例,说明如何绘制决策边界。另请参阅 this stackoverflow 帖子。基本步骤如下:

    1. 根据输入数据的范围或特征向量X选择分辨率。
    2. 创建一个由分辨率内的每个点组成的网格。
    3. 访问网格中的每个点,使用您学习的逻辑回归模型预测得分。
    4. 使用分数作为Z 变量(等高线图上的高度),绘制等高线曲线。

    在下面的示例代码中,我们假设 2d 特征空间的范围从 -1 到 200。我们选择 1.5 的步长,然后对于网格中的每个点,我们将模型称为 predictor -- @987654327 @获取积分。最后通过调用matlab中的contour函数绘制绘图。

    在这里绘制决策边界比绘制 线性回归中的最佳拟合曲线。您将需要绘制 $\theta^T x = 0$ 线隐含,通过绘制轮廓。这可以是 通过在代表 原始 $u$ 和 $v$ 输入,然后绘制线 $\theta^Tx$ 的计算结果为零。 下面给出了 Matlab/Octave 的绘图实现。

    % Define the ranges of the grid
    u = linspace(-1, 1.5, 200);
    v = linspace(-1, 1.5, 200);
    
    % Initialize space for the values to be plotted
    z = zeros(length(u), length(v));
    
    % Evaluate z = theta*x over the grid
    for i = 1:length(u)
        for j = 1:length(v)
            % Notice the order of j, i here!
            z(j,i) = map_feature(u(i), v(j))*theta;
        end
    end
    
    % Because of the way that contour plotting works
    % in Matlab, we need to transpose z, or
    % else the axis orientation will be flipped!
    z = z'
    % Plot z = 0 by specifying the range [0, 0]
    contour(u,v,z, [0, 0], 'LineWidth', 2)
    

    【讨论】:

    • 嗨 Greeness,我看到了帖子,实际上该图适用于 2D,但是当我考虑使用高斯内核的逻辑回归或 SVM 时,我的特征变成了训练集的数量。即虽然我的原始特征只有两个,但当我使用高斯核时,我的新特征集转换为训练集的数量。因此我的 theta 是从 m(训练集的数量)维的新特征集中学习的。我想知道是否有什么方法可以将数据转换回 2D 特征集并获得决策边界。
    猜你喜欢
    • 2015-03-31
    • 2014-11-27
    • 2016-08-09
    • 2020-08-31
    • 2017-07-30
    • 2013-06-26
    • 2014-03-16
    • 2021-11-23
    相关资源
    最近更新 更多