gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:56:30 25 4
gpt4 key购买 nike

我尝试过使用具有多项式特征的逻辑回归,幸运的是它对我来说工作得很好,而且我还能够绘制决策曲线。我已将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com