【问题标题】:Python matplotlib code which i dont understand what code doPython matplotlib 代码,我不明白代码是做什么的
【发布时间】: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


【解决方案1】:
  1. 多个输入数据点可以具有相同的预测,因此绘制填充轮廓 (contourf) 可以快速了解搜索空间。

  2. 仔细观察,它不是预测,而是输入值。执行此操作是为了确保 X 的维度与我们的训练数据一致。

  3. 这只是为了确保为每个类的数据点绘制不同的颜色。

  4. label 以 classes(y) 的形式给出,以便我们在绘制它们时将其显示为图例。

sklearn 用户指南中的here 中显示了一些类似的示例。

【讨论】:

    猜你喜欢
    • 2019-11-21
    • 2020-03-02
    • 2022-08-08
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2014-04-11
    • 2022-12-05
    相关资源
    最近更新 更多