首页 > 解决方案 > 如何在python中绘制感知器决策边界和数据集

问题描述

我写了多层感知器,使用三层(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))

标签: pythonmachine-learningperceptron

解决方案


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

参考:链接


推荐阅读