英文:
Linear Decision Boundary in Logistic Regression
问题
这段代码中负责添加额外的线性决策边界(15%,30%等)的代码行是:
left_right = np.array([2.9, 7])
boundary = -(log_reg.coef_[0][0] * left_right + log_reg.intercept_[0]) / log_reg.coef_[0][1]
这些行计算了一个线性决策边界,然后使用 plt.plot(left_right, boundary, "k--", linewidth=3)
将其绘制在图上。这个线性决策边界分割了两个类别,其中一个类别标记为 "Not Iris virginica",另一个标记为 "Iris virginica"。
英文:
Which line(s) of code are responsible for the additional linear decision boundaries, 15%, 30%, etc., in the following code?
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
iris = datasets.load_iris()
print(list(iris.keys()))
X = iris['data'][:,3:] #petal width
y = (iris['target']==2).astype(np.int32) #1 if virginica, else 0
X = iris["data"][:, (2, 3)] # petal length, petal width
y = (iris["target"] == 2).astype(np.int32)
log_reg = LogisticRegression(solver="lbfgs", C=10**10, random_state=42)
log_reg.fit(X, y)
x0, x1 = np.meshgrid(
np.linspace(2.9, 7, 500).reshape(-1, 1),
np.linspace(0.8, 2.7, 200).reshape(-1, 1),
)
X_new = np.c_[x0.ravel(), x1.ravel()]
y_proba = log_reg.predict_proba(X_new)
plt.figure(figsize=(10, 4))
plt.plot(X[y==0, 0], X[y==0, 1], "bs")
plt.plot(X[y==1, 0], X[y==1, 1], "g^")
zz = y_proba[:, 1].reshape(x0.shape)
contour = plt.contour(x0, x1, zz, cmap=plt.cm.brg)
left_right = np.array([2.9, 7])
boundary = -(log_reg.coef_[0][0] * left_right + log_reg.intercept_[0]) / log_reg.coef_[0][1]
plt.clabel(contour, inline=1, fontsize=12)
plt.plot(left_right, boundary, "k--", linewidth=3)
plt.text(3.5, 1.5, "Not Iris virginica", fontsize=14, color="b", ha="center")
plt.text(6.5, 2.3, "Iris virginica", fontsize=14, color="g", ha="center")
plt.xlabel("Petal length", fontsize=14)
plt.ylabel("Petal width", fontsize=14)
plt.axis([2.9, 7, 0.8, 2.7])
plt.show()
I'm working though a ML textbook with undocumented code and this is the one feature I can't figure out the origin of.
答案1
得分: 2
这是从这一行开始的:
contour = plt.contour(x0, x1, zz, cmap=plt.cm.brg)
你可以通过将这行代码从你的代码中注释掉来进行双重检查。
然后,标签(30%,60%,...)是使用上述的 contour
对象绘制的:
plt.clabel(contour, inline=1, fontsize=12)
通过查看 contour
对象进行确认:
{ 'axes': <Axes: xlabel='Petal length', ylabel='Petal width'>,
'levels': array([0. , 0.15, 0.3 , 0.45, 0.6 , 0.75, 0.9 , 1.05]),...
基本思路如下:
首先,使用 Numpy 的 meshgrid
创建 x0
和 x1
,然后使用概率(即 zz
)绘制等高线图。
要深入了解 meshgrid
,你可以参考这个链接:https://www.sharpsightlabs.com/blog/numpy-meshgrid/
英文:
It is from this line
contour = plt.contour(x0, x1, zz, cmap=plt.cm.brg)
You can double-check on this by commenting out that line from your code.
Then, the labels (30%, 60%,...) are drawn using contour
object above:
plt.clabel(contour, inline=1, fontsize=12)
Confirmed by looking into contour
object:
> {'axes': <Axes: xlabel='Petal length', ylabel='Petal width'>,
'levels': array([0. , 0.15, 0.3 , 0.45, 0.6 , 0.75, 0.9 , 1.05]),...
Basically, the idea is as follows.
First, create x0
and x1
using Numpy meshgrid
, then plot a contour with the probability (i.e., zz
).
For further reading on meshgrid, you can refer to this link: https://www.sharpsightlabs.com/blog/numpy-meshgrid/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论