《Python机器学习基础教程》书中代码的问题

最近在看《Python机器学习基础教程》这本书,书中的一个例子里有如下代码,其中加粗的代码画出了三条线,将图中的数据点划分成了三类。主要不明白的地方是for循环语句中line这个变量起的作用是什么,同时-(line * coef[0] + intercept) / coef[1]这个计算是怎么来的?谢谢指点!

from sklearn.datasets import make_blobs
from sklearn.svm import LinearSVC
import mglearn 
import numpy as np
import matplotlib.pyplot as plt

X, y = make_blobs(random_state=42)
mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
plt.xlabel("Feature 0")
plt.ylabel("Feature 1")
plt.legend(["Class 0", "Class 1", "Class 2"])
linear_svm = LinearSVC().fit(X, y)

line = np.linspace(-15, 15)
for coef, intercept, color in zip(linear_svm.coef_, linear_svm.intercept_,
 ['b', 'r', 'g']):
 plt.plot(line, -(line * coef[0] + intercept) / coef[1], c=color)

plt.ylim(-10, 15)
plt.xlim(-10, 8)
plt.xlabel("Feature 0")
plt.ylabel("Feature 1")
plt.legend(['Class 0', 'Class 1', 'Class 2', 'Line class 0', 'Line class 1',
 'Line class 2'], loc=(1.01, 0.3))
阅读 791
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题