【问题标题】:Logistic regression: plotting decision boundary from theta逻辑回归:从 theta 绘制决策边界
【发布时间】:2017-07-30 23:46:08
【问题描述】:

我有以下代码:

x1 = np.random.randn(100)
y1 = np.random.randn(100) + 3
x2 = np.random.randn(100) + 3
y2 = np.random.randn(100)
plt.plot(x1, y1, "+", x2, y2, "x")
plt.axis('equal')
plt.show()

结果如下图

我已经实现了自己的逻辑回归,这会返回一个 theta,我想使用这个 theta 来绘制决策边界,但我不知道该怎么做。

X = np.matrix(np.vstack((np.hstack((x1,x2)), np.hstack((y1,y2)))).T)
X = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1)
Y = np.matrix(1.0 * np.hstack((np.zeros(100), np.ones(100)))).T

learning_rate = 0.0001
iterations    = 3000
theta         = np.matrix([[0.5], [0.5], [0.5]])
theta = logistic_regression(theta, X, Y, learning_rate, iterations)

这给了theta =

[[ 0.40377942]
 [ 0.53696461]
 [ 0.1398419 ]]

例如。如何使用它来绘制决策边界?

【问题讨论】:

  • 解释你的logistic_regression函数返回值的含义。
  • @WarrenWeckesser this video 中看到的 theta 值

标签: python numpy logistic-regression


【解决方案1】:

您想要绘制 θTX = 0,其中 X 是包含 (1, x, y) 的向量。也就是说,您要绘制由theta[0] + theta[1]*x + theta[2]*y = 0 定义的线。求解 y:

y = -(theta[0] + theta[1]*x)/theta[2]

所以,类似:

theta = theta[:,0]  # Make theta a 1-d array.
x = np.linspace(-6, 6, 50)
y = -(theta[0] + theta[1]*x)/theta[2]
plt.plot(x, y)

不过,有些地方看起来不对劲,因为您有 theta[1] > 0theta[2] > 0,这会导致直线斜率为负。

【讨论】:

  • 我真的只是自己想通了,然后来到这里,看到我所做的是正确的!调整像learning_rate 这样的参数会产生一个很好的决策边界,所以它肯定有效:)
猜你喜欢
  • 2015-03-31
  • 2020-08-31
  • 2014-11-27
  • 2020-03-28
  • 2017-04-24
  • 2020-05-05
  • 2023-01-04
  • 2018-03-01
相关资源
最近更新 更多