如何在python中绘制感知器决策边界和数据集

时间:2019-05-16 08:20:08

标签: python machine-learning perceptron

我用三层(0,1,2)编写了多层感知器。我想绘制使用Python分类的决策边界和数据集(八个特征长)。 我如何使用一个python库在屏幕上绘制它? 权重函数->矩阵[3] [8] 样本x->向量[8]

#-- Trains the boundary decision, and test it. --#
def perceptron(x, y):
    m = len(x)
    d = len(x[0])
    eta = 0.1

    w = [[0 for k in range(d)] for j in range(3)]

    T = 2500
    for t in range(0, T):
        i = random.randint(0, m - 1)
        v = [float(j) for j in x[i]]
        y_hat = np.argmax(np.dot(w, v))
        if y_hat != y[i]:
            w[y[i]] = np.add(w[y[i]], np.array(v) * eta)
            w[y_hat] = np.subtract(w[y_hat], np.array(v) * eta)

    w_perceptron = w

    #-- Test the decision boundary that we trained. --#
    #-- Prints the loss weight function. --#
    M_perceptron = 0
    for t in range(0, m):
        y_hat = np.argmax(np.dot(w_perceptron, x[t]))
        if y[t] != y_hat:
            M_perceptron = M_perceptron + 1
    return float(M_perceptron) / m


def main():
    y = []
    x = [[]]
    x = readTrain_X(sys.argv[1], x) # Reads data trainning set.
    readTrain_Y(sys.argv[2], y) # Reads right classified training set.

    print(perceptron(x, y))

1 个答案:

答案 0 :(得分:0)

您无法绘制8个特征。您无法可视化8D空间。但是您可以做的是使用PCA / t-SNE到2D进行尺寸缩减以进行可视化。如果可以将其缩减为2D,则可以使用创建值的网格并使用模型返回的概率来可视化决策边界。

参考:Link