Created
September 24, 2015 22:04
-
-
Save aconz2/0f50ecd02f186be6e56a to your computer and use it in GitHub Desktop.
3D plot of making a 2D non-linearly-seperable problem (XOR) into a 3D linearly seperable one with feature mapping.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from matplotlib import pyplot as plt | |
from mpl_toolkits.mplot3d import Axes3D | |
import numpy as np | |
fig = plt.figure() | |
ax = fig.add_subplot(111, projection='3d') | |
# original data | |
# not linearly separable in 2D | |
x = np.array([[1, 1], [-1, -1], [-1, 1], [1, -1]]) | |
y = np.array([-1, -1, 1, 1]) | |
# feature mapping to produce x3 = x1 | x2 | |
# add feature x3 | x3 computed here | | |
x = np.hstack([x, np.array([x[:, 0] | x[:, 1]]).T]) | |
plus = x[y == 1] | |
minus = x[y == -1] | |
ax.scatter(plus[:, 0], plus[:, 1], plus[:, 2], c='b') | |
ax.scatter(minus[:, 0], minus[:, 1], minus[:, 2], c='r') | |
# these control the position and norm of the plane | |
point = np.array([0, 0, 0.5]) | |
normal = np.array([-1, -1, 2]) | |
# this is copy paste for plotting a plane | |
# a plane is a*x+b*y+c*z+d=0 | |
# [a,b,c] is the normal. Thus, we have to calculate | |
# d and we're set | |
d = -point.dot(normal) | |
xx, yy = np.meshgrid(np.linspace(-1, 1, 10), np.linspace(-1, 1, 10)) | |
# calculate corresponding z | |
z = (-normal[0] * xx - normal[1] * yy + d) * 1. /normal[2] | |
# plot the surface | |
ax.plot_surface(xx, yy, z, alpha=0.4) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment