【发布时间】:2020-01-04 18:51:22
【问题描述】:
我购买了 SuperDataScience 的机器学习 A-Z 课程。在逻辑回归中,老师给出了一段可视化代码,我不明白这段代码。
我尝试对此进行调查。我了解一些代码。但其实我有个问题。
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() -1, stop = X_set[:, 0].max()+1, step = 0.01),
np.arange(start = X_set[:, 1].min() -1, stop = X_set[:, 1].max()+1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(("red","green")))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c = ListedColormap(("red","green"))(i), label = j)
plt.xlabel("Age")
plt.ylabel("Salary")
plt.title("Logistic regression prediction")
plt.legend()
plt.show()
- 预测
contourf?我研究了一下,找不到任何东西 - 为什么要重塑预测?有必要吗?
-
ListedColormap(("red","green"))(i)分散? - 标签分散?这是什么?
【问题讨论】:
-
尝试运行代码,看看结果,然后一点一点的改变一些部分,看看对结果的影响。这是学习的唯一途径。
-
@lenik 我已经这样做了。感谢您为大多数问题找到了答案。这是我找不到的最新问题。
-
你的训练集中有两个标签,散点图应该显示集合中的数据,根据标签颜色为绿色或红色,并显示实际标签。
标签: python matplotlib scikit-learn