def apply_pca (X, standardize=True): # Standardize if standardize: X = (X - X.mean(axis=0)) / X.std(axis=0) # Create principal components pca = PCA () X_pca = pca.fit_transform(X) # Convert to dataframe component_names = [f"PC{i+1}" for i in range (X_pca.shape [1])] X_pca = pd.DataFrame (X_pca, columns=component_names) # Create loadings loadings = pd. DataFrame( pca.components_.T, # transpose the matrix of loadings columns=component_names, # so the columns are the principal components index=X.columns, # and the rows are the original features ) return pca, X_pca, loadings def plot_variance(pca, width=8, dpi=100): # Create figure fig, axs = plt.subplots (1, 2) n = pca.n_components_ grid = np.arange(1, n + 1) # Explained variance evrpca.explained_variance_ratio_ axs[0].bar (grid, evr) axs[0].set( xlabel="Component", title="% Explained Variance", ylim-(0.0, 1.0) ) # Cumulative Variance cv = np.cumsum (evr) axs[1].plot(np.r_[0, grid], np.r_[0, cv], "o-") axs[1].set( xlabel="Component", title="% Cumulative Variance", ylim-(0.0, 1.0) ) # Set up figure fig.set(figwidth=8, dpi=100) return axs

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Please help explain this code snippet

in the second function, why are we taking transpose.

also ax[1] plot is confusing. please explain this clearly and maybe with example

def apply_pca (X, standardize=True):
# Standardize
if standardize:
X = (X X.mean(axis=0)) / X.std (axis=0)
# Create principal components
pca = PCA ()
X_pca = pca.fit_transform(X)
# Convert to dataframe
component_names = [f"PC{i+1}" for i in range (X_pca.shape [1])]
X_pca = pd.DataFrame (X_pca, columns=component_names)
# Create loadings
loadings = pd. DataFrame (
pca.components_.T, # transpose the matrix of loadings
columns=component_names, # so the columns are the principal components
index=X.columns, # and the rows are the original features
)
return pca, X_pca, loadings
def plot_variance (pca, width=8, dpi=100):
# Create figure
fig, axs = plt.subplots (1, 2)
n = pca.n_components_
grid = np.arange(1, n + 1)
# Explained variance
evrpca.explained_variance_ratio_
axs[0].bar (grid, evr)
axs[0].set(
xlabel="Component", title="% Explained Variance", ylim=(0.0, 1.0)
)
# Cumulative Variance
cv = np.cumsum(evr)
axs[1].plot(np.r_[0, grid], np.r_[0, cv], "o-")
xlabel="Component", title="% Cumulative Variance", ylim-(0.0, 1.0)
)
# Set up figure
fig. set (figwidth=8, dpi=100)
return axs
axs[1].set(
Transcribed Image Text:def apply_pca (X, standardize=True): # Standardize if standardize: X = (X X.mean(axis=0)) / X.std (axis=0) # Create principal components pca = PCA () X_pca = pca.fit_transform(X) # Convert to dataframe component_names = [f"PC{i+1}" for i in range (X_pca.shape [1])] X_pca = pd.DataFrame (X_pca, columns=component_names) # Create loadings loadings = pd. DataFrame ( pca.components_.T, # transpose the matrix of loadings columns=component_names, # so the columns are the principal components index=X.columns, # and the rows are the original features ) return pca, X_pca, loadings def plot_variance (pca, width=8, dpi=100): # Create figure fig, axs = plt.subplots (1, 2) n = pca.n_components_ grid = np.arange(1, n + 1) # Explained variance evrpca.explained_variance_ratio_ axs[0].bar (grid, evr) axs[0].set( xlabel="Component", title="% Explained Variance", ylim=(0.0, 1.0) ) # Cumulative Variance cv = np.cumsum(evr) axs[1].plot(np.r_[0, grid], np.r_[0, cv], "o-") xlabel="Component", title="% Cumulative Variance", ylim-(0.0, 1.0) ) # Set up figure fig. set (figwidth=8, dpi=100) return axs axs[1].set(
Expert Solution
steps

Step by step

Solved in 5 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY