matlab - 使用高斯核绘制逻辑回归的决策曲线

标签 matlab machine-learning octave

我尝试过使用具有多项式特征的逻辑回归,幸运的是它对我来说工作得很好,而且我还能够绘制决策曲线。我已将 map_feature 函数用于多项式特征。 (我引用了安德鲁教授关于正则化逻辑回归的笔记):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) 上应用梯度下降后得到的成本函数图是:

enter image description here

因此我得到了新的 theta 值:

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

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

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

感谢您的帮助。谢谢

最佳答案

所引用的安德鲁的 note实际上包含了如何绘制决策边界的一个很好的例子。另请参阅this堆栈溢出帖子。基本步骤如下:

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

在下面的示例代码中,我们假设一个 2d 特征空间,每个特征空间的范围从 -1 到 200。我们选择步长大小 1.5,然后对于网格中的每个点,我们将模型称为预测器 -- map_feature(u,v) x theta 获取分数。最后通过调用matlab中的contour函数绘制绘图。

Plotting the decision boundary here will be trickier than plotting the best-fit curve in linear regression. You will need to plot the $\theta^T x = 0$ line implicity, by plotting a contour. This can be done by evaluating $\theta^Tx$ over a grid of points representing the original $u$ and $v$ inputs, and then plotting the line where $\theta^Tx$ evaluates to zero. The plot implementation for Matlab/Octave is given below.

% 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)

enter image description here

关于matlab - 使用高斯核绘制逻辑回归的决策曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25947117/

相关文章:

matlab - 相机校准库

python - 我需要帮助将列表转换为 pandas 数据框

matlab - 如何从元胞数组中删除一组特定的行?

64-bit - Windows 上的 64 位版本的 Octave

r - 在 Matlab 中是否有等同于 R 的负索引?

python - np.int16 和 int16 matlab 之间的区别?

perl - Perl 和 Matlab 之间的数据/IO 通信

python - 如何将 2D 列表 python 转换为一个列表并连接它们

python - Keras 序列模型输入层

parallel-processing - Octave 并行计算