コード例 #1
0
ax[0].set_xlabel('PC1')
ax[0].set_ylabel('PC2')

# Set the second subplot properties
ax[1].set_title('Flattened to PC1')
ax[1].set_yticks([])
ax[1].set_xlabel('PC1')
ax[1].set_ylim([-1,1])

plt.show()

# Once again, the linear PCA analysis clearly does not work
# let's try the radial basis function

# Apply the radial basis function
X_kpca, eigvals = rbf_kernel_pca(X, gamma=15, n_components=2)

# Create the scatter plots
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(7,3))
plt.suptitle('RBF Kernel PCA')

ax[0].scatter(X_kpca[y==0, 0], X_kpca[y==0, 1], color='red', marker='^', alpha=0.5, s = 50)
ax[0].scatter(X_kpca[y==1, 0], X_kpca[y==1, 1], color='blue', marker='o', alpha=0.5, s = 50)
ax[1].scatter(X_kpca[y==0, 0], np.zeros((500,1)) + 0.02, color='red', marker='^', alpha=0.5, s = 50)
ax[1].scatter(X_kpca[y==1, 0], np.zeros((500,1)) - 0.02, color='blue', marker='o', alpha=0.5, s = 50)

# Format the first subplot
ax[0].set_xlabel('PC1')
ax[0].set_ylabel('PC2')
ax[0].set_title('PC2 vs PC1')
コード例 #2
0
ax[1].scatter(X_spca[y==1, 0], np.zeros((50,1)) + 0.02, color='red', \
        marker='^', alpha=0.5)
ax[1].scatter(X_spca[y==1, 0], np.zeros((50, 1)) - 0.02, color='blue', \
        marker='o', alpha=0.5)
ax[0].set_xlabel('PC1')
ax[0].set_ylabel('PC1')
ax[1].set_ylim([-1, 1])
ax[1].set_yticks([])
ax[1].set_xlabel('PC1')
plt.suptitle('Attempt at linear classification region')
plt.show()

# Using our radial basis function kernel, create a non-linear transformation matrix
# This will transform the data into some other dimension where it is linearly 
# seperable, and then select the k primary components from this result
X_kpca, eigvals = rbf_kernel_pca(X, gamma=15, n_components=2)
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(7,3))
ax[0].scatter(X_kpca[y==0, 0], X_kpca[y==0, 1], color='red', marker='^', alpha=0.5)
ax[0].scatter(X_kpca[y==1, 0], X_kpca[y==1, 1], color='blue', marker='o', alpha=0.5)
ax[1].scatter(X_kpca[y==0, 0], np.zeros((50, 1)) + 0.02, color='red', marker='^', alpha=0.5)
ax[1].scatter(X_kpca[y==1, 0], np.zeros((50, 1)) - 0.02, color='blue', marker='o', alpha=0.5)
ax[0].set_xlabel('PC1')
ax[1].set_xlabel('PC1')
ax[0].set_ylabel('PC2')
ax[1].set_yticks([])
ax[1].set_ylim([-1, 1])
ax[0].set_title('RBF Principle Component Analysis')
ax[1].set_title('Flattened to only show PC1')
plt.show()

# Grab a random point, put it through the PCA, and be able to get the original point