神经网络(感知器) - 在执行二元分类时可视化决策边界(作为超平面) [英] Neural network (perceptron) - visualizing decision boundary (as a hyperplane) when performing binary classification

查看:35
本文介绍了神经网络(感知器) - 在执行二元分类时可视化决策边界(作为超平面)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个只有一个神经元(3 个输入,二进制输出)的简单神经网络的决策边界可视化.我正在从 Keras NN 模型中提取权重,然后尝试使用 matplotlib 绘制表面平面.不幸的是,超平面没有出现在散点图上的点之间,而是显示在所有数据点的下方(参见输出图像).

我正在使用等式计算超平面的 z 轴z = (d - ax - by)/c 对于定义为 ax + by + cz = d

的超平面

有人可以帮助我正确构建和显示基于 NN 权重的超平面吗?

此处的目标是使用公共数据集 (

由于您的数据不是线性可分的,您需要向网络添加隐藏层,以便了解输入的转换,使其线性可分.不过,绘制决策边界并不那么简单......

I would like to visualize the decision boundary for a simple neural network with only one neuron (3 inputs, binary output). I'm extracting the weights from a Keras NN model and then attempting to draw the surface plane using matplotlib. Unfortunately, the hyperplane is not appearing between the points on the scatter plot, but instead is displaying underneath all the data points (see output image).

I am calculating the z-axis of the hyperplane using the equation z = (d - ax - by) / c for a hyperplane defined as ax + by + cz = d

Could somebody assist me with correctly constructing and displaying a hyperplane based on the NN weights?

The goal here is to classify individuals into two groups (diabetes or no diabetes), based on 3 predictor variables using a public dataset (https://www.kaggle.com/uciml/pima-indians-diabetes-database).

%matplotlib notebook

import pandas as pd
import numpy as np
from keras import models
from keras import layers
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d

EPOCHS = 2

#Data source: https://www.kaggle.com/uciml/pima-indians-diabetes-database
ds = pd.read_csv('diabetes.csv', sep=',', header=0)

#subset and split
X = ds[['BMI', 'DiabetesPedigreeFunction', 'Glucose']]
Y = ds[['Outcome']]

#construct perceptron with 3 inputs and a single output
model = models.Sequential()
layer1 = layers.Dense(1, activation='sigmoid', input_shape=(3,))
model.add(layer1)

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

#train perceptron
history = model.fit(x=X, y=Y, epochs=EPOCHS)

#display accuracy and loss
epochs = range(len(history.epoch))

plt.figure()
plt.plot(epochs, history.history['accuracy'])
plt.xlabel('Epochs')
plt.ylabel('Accuracy')

plt.figure()
plt.plot(epochs, history.history['loss'])
plt.xlabel('Epochs')
plt.ylabel('Loss')

plt.show()

#extract weights and bias from model
weights = model.layers[0].get_weights()[0]
biases = model.layers[0].get_weights()[1]

w1 = weights[0][0] #a
w2 = weights[1][0] #b
w3 = weights[2][0] #c
b = biases[0]      #d

#construct hyperplane: ax + by + cz = d
a,b,c,d = w1,w2,w3,b

x_min = ds.BMI.min()
x_max = ds.BMI.max()

x = np.linspace(x_min, x_max, 100)

y_min = ds.DiabetesPedigreeFunction.min()
y_max = ds.DiabetesPedigreeFunction.max()

y = np.linspace(y_min, y_max, 100)

Xs,Ys = np.meshgrid(x,y)
Zs = (d - a*Xs - b*Ys) / c

#visualize 3d scatterplot with hyperplane
fig = plt.figure(num=None, figsize=(9, 9), dpi=100, facecolor='w', edgecolor='k')
ax = fig.gca(projection='3d')

ax.plot_surface(Xs, Ys, Zs, alpha=0.45)

ax.scatter(ds.BMI, ds.DiabetesPedigreeFunction, ds.Glucose, c=ds.Outcome)

ax.set_xlabel('BMI')
ax.set_ylabel('DiabetesPedigreeFunction')
ax.set_zlabel('Glucose')

解决方案

Your Network is a Logistic Regression model so the equation of the surface is definitely z = (-b-w1x -w2y) / w3 . Your model needs more training. Try increasing the number of epochs (try about 500):

Since your data is not linearly separable you'll need to add hidden layers to your network in order to learn a transformation of your input that makes it linearly separable. Plotting that decision boundary is less straightforward though...

这篇关于神经网络(感知器) - 在执行二元分类时可视化决策边界(作为超平面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆