使用 matplotlib 绘制感知器算法

Mas*_*use 4 python machine-learning matplotlib neural-network deep-learning

在我正在学习的 ML 课程中,我有 100 个数据条目,并且我在感知器算法中使用它。\n我想要的是展示这样的情节。

\n\n

在此输入图像描述

\n\n

正如您在上面看到的,我们有由红色和蓝色点表示的数据以及最小化误差的不同计算线。这是我想要的输出。这是我的数据和代码。

\n\n

数据.csv

\n\n
0.78051,-0.063669,1\n0.28774,0.29139,1\n0.40714,0.17878,1\n0.2923,0.4217,1\n0.50922,0.35256,1\n0.27785,0.10802,1\n0.27527,0.33223,1\n0.43999,0.31245,1\n0.33557,0.42984,1\n0.23448,0.24986,1\n0.0084492,0.13658,1\n0.12419,0.33595,1\n0.25644,0.42624,1\n0.4591,0.40426,1\n0.44547,0.45117,1\n0.42218,0.20118,1\n0.49563,0.21445,1\n0.30848,0.24306,1\n0.39707,0.44438,1\n0.32945,0.39217,1\n0.40739,0.40271,1\n0.3106,0.50702,1\n0.49638,0.45384,1\n0.10073,0.32053,1\n0.69907,0.37307,1\n0.29767,0.69648,1\n0.15099,0.57341,1\n0.16427,0.27759,1\n0.33259,0.055964,1\n0.53741,0.28637,1\n0.19503,0.36879,1\n0.40278,0.035148,1\n0.21296,0.55169,1\n0.48447,0.56991,1\n0.25476,0.34596,1\n0.21726,0.28641,1\n0.67078,0.46538,1\n0.3815,0.4622,1\n0.53838,0.32774,1\n0.4849,0.26071,1\n0.37095,0.38809,1\n0.54527,0.63911,1\n0.32149,0.12007,1\n0.42216,0.61666,1\n0.10194,0.060408,1\n0.15254,0.2168,1\n0.45558,0.43769,1\n0.28488,0.52142,1\n0.27633,0.21264,1\n0.39748,0.31902,1\n0.5533,1,0\n0.44274,0.59205,0\n0.85176,0.6612,0\n0.60436,0.86605,0\n0.68243,0.48301,0\n1,0.76815,0\n0.72989,0.8107,0\n0.67377,0.77975,0\n0.78761,0.58177,0\n0.71442,0.7668,0\n0.49379,0.54226,0\n0.78974,0.74233,0\n0.67905,0.60921,0\n0.6642,0.72519,0\n0.79396,0.56789,0\n0.70758,0.76022,0\n0.59421,0.61857,0\n0.49364,0.56224,0\n0.77707,0.35025,0\n0.79785,0.76921,0\n0.70876,0.96764,0\n0.69176,0.60865,0\n0.66408,0.92075,0\n0.65973,0.66666,0\n0.64574,0.56845,0\n0.89639,0.7085,0\n0.85476,0.63167,0\n0.62091,0.80424,0\n0.79057,0.56108,0\n0.58935,0.71582,0\n0.56846,0.7406,0\n0.65912,0.71548,0\n0.70938,0.74041,0\n0.59154,0.62927,0\n0.45829,0.4641,0\n0.79982,0.74847,0\n0.60974,0.54757,0\n0.68127,0.86985,0\n0.76694,0.64736,0\n0.69048,0.83058,0\n0.68122,0.96541,0\n0.73229,0.64245,0\n0.76145,0.60138,0\n0.58985,0.86955,0\n0.73145,0.74516,0\n0.77029,0.7014,0\n0.73156,0.71782,0\n0.44556,0.57991,0\n0.85275,0.85987,0\n0.51912,0.62359,0\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在这是我的代码。第一部分

\n\n
0.78051,-0.063669,1\n0.28774,0.29139,1\n0.40714,0.17878,1\n0.2923,0.4217,1\n0.50922,0.35256,1\n0.27785,0.10802,1\n0.27527,0.33223,1\n0.43999,0.31245,1\n0.33557,0.42984,1\n0.23448,0.24986,1\n0.0084492,0.13658,1\n0.12419,0.33595,1\n0.25644,0.42624,1\n0.4591,0.40426,1\n0.44547,0.45117,1\n0.42218,0.20118,1\n0.49563,0.21445,1\n0.30848,0.24306,1\n0.39707,0.44438,1\n0.32945,0.39217,1\n0.40739,0.40271,1\n0.3106,0.50702,1\n0.49638,0.45384,1\n0.10073,0.32053,1\n0.69907,0.37307,1\n0.29767,0.69648,1\n0.15099,0.57341,1\n0.16427,0.27759,1\n0.33259,0.055964,1\n0.53741,0.28637,1\n0.19503,0.36879,1\n0.40278,0.035148,1\n0.21296,0.55169,1\n0.48447,0.56991,1\n0.25476,0.34596,1\n0.21726,0.28641,1\n0.67078,0.46538,1\n0.3815,0.4622,1\n0.53838,0.32774,1\n0.4849,0.26071,1\n0.37095,0.38809,1\n0.54527,0.63911,1\n0.32149,0.12007,1\n0.42216,0.61666,1\n0.10194,0.060408,1\n0.15254,0.2168,1\n0.45558,0.43769,1\n0.28488,0.52142,1\n0.27633,0.21264,1\n0.39748,0.31902,1\n0.5533,1,0\n0.44274,0.59205,0\n0.85176,0.6612,0\n0.60436,0.86605,0\n0.68243,0.48301,0\n1,0.76815,0\n0.72989,0.8107,0\n0.67377,0.77975,0\n0.78761,0.58177,0\n0.71442,0.7668,0\n0.49379,0.54226,0\n0.78974,0.74233,0\n0.67905,0.60921,0\n0.6642,0.72519,0\n0.79396,0.56789,0\n0.70758,0.76022,0\n0.59421,0.61857,0\n0.49364,0.56224,0\n0.77707,0.35025,0\n0.79785,0.76921,0\n0.70876,0.96764,0\n0.69176,0.60865,0\n0.66408,0.92075,0\n0.65973,0.66666,0\n0.64574,0.56845,0\n0.89639,0.7085,0\n0.85476,0.63167,0\n0.62091,0.80424,0\n0.79057,0.56108,0\n0.58935,0.71582,0\n0.56846,0.7406,0\n0.65912,0.71548,0\n0.70938,0.74041,0\n0.59154,0.62927,0\n0.45829,0.4641,0\n0.79982,0.74847,0\n0.60974,0.54757,0\n0.68127,0.86985,0\n0.76694,0.64736,0\n0.69048,0.83058,0\n0.68122,0.96541,0\n0.73229,0.64245,0\n0.76145,0.60138,0\n0.58985,0.86955,0\n0.73145,0.74516,0\n0.77029,0.7014,0\n0.73156,0.71782,0\n0.44556,0.57991,0\n0.85275,0.85987,0\n0.51912,0.62359,0\n
Run Code Online (Sandbox Code Playgroud)\n\n

当您运行此代码时,您会正确得到

\n\n

在此输入图像描述

\n\n

所以现在我想在同一个图中绘制代表每次迭代最佳函数的线。为此,我评论了 plt.show() 上面的最后一行并做了

\n\n
import numpy as np\nimport pandas as pd\n# Setting the random seed, feel free to change it and see different solutions.\nnp.random.seed(42)\nimport matplotlib.pyplot as plt\n\n\ndef stepFunction(t):\n    return 1 if t >= 0 else 0\n\n\ndef prediction(X, W, b):\n    return stepFunction((np.matmul(X, W) + b)[0])\n\n# TODO: Fill in the code below to implement the perceptron trick.\n# INPUTS\n# data X, the labels y,\n# the weights W (as an array), and the bias b,\n# The function  weights and bias W, b, according to the perceptron algorithm,\n# and return W and b.\n\ndef perceptronStep(X, y, W, b, learn_rate=0.01):\n    for i in range(len(X)):\n        y_hat = prediction(X[i], W, b)\n        if y[i] - y_hat == 1:\n            W[0] += X[i][0] * learn_rate\n            W[1] += X[i][1] * learn_rate\n            b += learn_rate\n        elif y[i] - y_hat == -1:\n            W[0] -= X[i][0] * learn_rate\n            W[1] -= X[i][1] * learn_rate\n            b -= learn_rate\n    return W, b\n\n# This function runs the perceptron algorithm repeatedly on the dataset,\n# and returns a few of the boundary lines obtained in the iterations,\n# for plotting purposes.\n# Feel free to play with the learning rate and the num_epochs,\n# and see your results plotted below.\ndef trainPerceptronAlgorithm(X, y, learn_rate=0.01, num_epochs=25):\n    x_min, x_max = min(X.T[0]), max(X.T[0])\n    y_min, y_max = min(X.T[1]), max(X.T[1])\n    W = np.array(np.random.rand(2, 1))\n    b = np.random.rand(1)[0] + x_max\n    # These are the solution lines that get plotted below.\n    boundary_lines = []\n    for i in range(num_epochs):\n        # In each epoch, we apply the perceptron step.\n        W, b = perceptronStep(X, y, W, b, learn_rate)\n        # Here I have a doubt . Why if y = W0*x1 + W1*x2 + b\n        # So we can get  x2 =y/W1 -(W0*x1)/W1 -b/W1 + y/W1)\n        # If we remove y/W1 we just get intercept and slope\n        # But why we are not using the last term y/W1\n        boundary_lines.append((-W[0] / W[1], -b / W[1]))\n    return boundary_lines\n\n# Get data and plot the points\ndata = pd.read_csv(\'data.csv\', header = None)\nX = data.iloc[:, :2].values\ny = data.iloc[:, -1].values\n\nx1 = X[:, 0]\nx2 = X[:, 1]\ncolor = [\'red\' if value == 1 else \'blue\' for value in y]\nplt.scatter(x1, x2, marker=\'o\', color=color)\nplt.xlabel(\'X1 input feature\')\nplt.ylabel(\'X2 input feature\')\nplt.title(\'Perceptron regression for X1, X2\')\nplt.show()\n
Run Code Online (Sandbox Code Playgroud)\n\n

但这并没有得到预期的结果。\n为什么这没有得到预期的结果?

\n

And*_*dyK 7

错误在于plt.plot(x_lin, (\xce\x981 * x_lin / \xce\x98o))哪里,而不是\xce\x981 * x_lin / \xce\x98o你应该有\xce\x98o * x_lin + \xce\x981

\n\n
fig, ax = plt.subplots(1, 1, figsize=(8,5))\nax.set_xlim(0, 1)\nax.set_ylim(0, 1)\nax.scatter(x1, x2, marker=\'o\', color=color)\nfor i, line in enumerate(boundary_lines):\n    \xce\x98o, \xce\x981  = line\n    if i == len(boundary_lines) - 1:\n        c, ls, lw = \'k\', \'-\', 2\n    else:\n        c, ls, lw = \'g\', \'--\', 1.5\n    ax.plot(x_lin, \xce\x98o * x_lin + \xce\x981, c=c, ls=ls, lw=lw)\nplt.show()\n
Run Code Online (Sandbox Code Playgroud)\n\n

结果:

\n\n

在此输入图像描述

\n