Python Forum
How to modify and get the same picture
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to modify and get the same picture
#1



May I know how to modify my Python programming thus I can get the same picture as refer to the attached file




from scipy import exp
from scipy.linalg import eigh
from scipy.spatial.distance import pdist, squareform
from sklearn.datasets import make_moons
from sklearn.decomposition import PCA       
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

def rbf_kernel_pca(X, gamma, n_components):
    """
    RBF kernel PCA implementation.
    Parameters
    ------------
    X: {NumPy ndarray}, shape = [n_samples, n_features]
    gamma: float
    Tuning parameter of the RBF kernel
    n_components: int
    Number of principal components to return
    Returns
    ------------
    X_pc: {NumPy ndarray}, shape = [n_samples, k_features]
    Projected dataset
    """

    sq_dists = pdist(X, 'sqeuclidean')
    # Convert pairwise distances into a square matrix.
    mat_sq_dists = squareform(sq_dists)
    # Compute the symmetric kernel matrix.
    K = exp(-gamma * mat_sq_dists)
    # Center the kernel matrix.
    N = K.shape[0]
    one_n = np.ones((N,N)) / N
    K = K - one_n.dot(K) - K.dot(one_n) + one_n.dot(K).dot(one_n)
    # Obtaining eigenpairs from the centered kernel matrix
    # numpy.eigh returns them in sorted order
    eigvals, eigvecs = eigh(K)
    # Collect the top k eigenvectors (projected samples)
    X_pc = np.column_stack((eigvecs[:, -i] for i in range(1, n_components + 1)))
    return X_pc

X, y = make_moons(n_samples=100, random_state=123)

from matplotlib.ticker import FormatStrFormatter
fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(8,5))

X_kpca = rbf_kernel_pca(X, gamma=0.1, n_components=2)

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[0].set_xlim([-0.6, 0.6])
ax[0].set_ylim([-0.3, 0.3])
ax[0].set_xlabel('PC1')
ax[0].set_ylabel('PC2')
plt.title('gamma=0.1')

X_kpca = rbf_kernel_pca(X, gamma=15, n_components=2)

ax[1].scatter(X_kpca[y==0, 0], X_kpca[y==0, 1], color='red', marker='^', alpha=0.5)
ax[1].scatter(X_kpca[y==1, 0], X_kpca[y==1, 1], color='blue', marker='o', alpha=0.5)
ax[1].set_xlim([-0.4, 0.4])
ax[1].set_ylim([-0.3, 0.3])
ax[1].set_xlabel('PC1')
ax[1].set_ylabel('PC2')
plt.title('gamma=15')

X_kpca = rbf_kernel_pca(X, gamma=150, n_components=2)

ax[2].scatter(X_kpca[y==0, 0], X_kpca[y==0, 1], color='red', marker='^', alpha=0.5)
ax[2].scatter(X_kpca[y==1, 0], X_kpca[y==1, 1], color='blue', marker='o', alpha=0.5)
ax[2].set_xlim([-0.2, 0.2])
ax[2].set_ylim([-0.3, 0.3])
ax[2].set_xlabel('PC1')
ax[2].set_ylabel('PC2')
plt.title('gamma=150')

plt.tight_layout()
plt.show()


Please see the image file -




[Image: 4FPec.jpg]





Please help me on this case





Reply
#2
hi

i can help u
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo How to find axiom of this picture DerbyHarrington 2 773 Mar-15-2020, 12:02 PM
Last Post: DerbyHarrington
  Why there is no any input of the picture? Tony 2 1,444 May-19-2018, 07:20 AM
Last Post: Tony

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020