python - 如何在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))
解决方案
您不能绘制 8 个特征。您无法想象 8D 空间。但是您可以做的是使用 PCA/t-SNE 执行降维到 2D 以进行可视化。如果可以将其简化为 2D,则可以使用创建值网格并使用模型返回的概率来可视化决策边界。
参考:链接
推荐阅读
- android - 在android中运行api调用时本地主机IP地址不起作用
- sql - 如何使用合并语句在不更改项目顺序的情况下将批量数据插入表中
- mongodb - 如何配置在更新事件上发送的 debezium 字段(mongo 连接器)
- android - Android - 将数据从 Realm 迁移到 Room
- php - 在codeigniter中更新/替换没有表格的数据
- jenkins - Jenkins Xvfb 插件 - 在构建中获取显示名称偏移量
- excel - 等到加载特定元素
- json - jsonpb,为什么要将int64解码为json,结果是字符串。像 int64 str=10 -->str:"10"
- node.js - 如何使用 express-formidable 包重命名文件
- java - Service vs IntentService 用于数据库远程操作