Question: import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.preprocessing import StandardScaler from sklearn.decomposition import FactorAnalysis from

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import FactorAnalysis
from sklearn.impute import SimpleImputer
from factor_analyzer import Rotator
# Factor Analysis before rotation
fa = FactorAnalysis(n_components=5, random_state=0)
fa.fit(df_numerical_scaled)
# Factor loadings
loadings = fa.components_
# Plotting heatmap for factor loadings
plt.figure(figsize=(20,7))
sns.heatmap(pd.DataFrame(fa.components_, columns=numerical_columns), annot=True, cmap='coolwarm',
yticklabels=[f'Factor{i+1}' for i in range(fa.n_components)],
xticklabels=numerical_columns)
plt.title('Factor Loadings Before Rotation')
plt.xlabel('Variables')
plt.ylabel('Factors')
plt.xticks(rotation=45, ha="right")
plt.yticks(rotation=0)
plt.tight_layout()
plt.show()
columns = df_numerical.columns
# Applying Varimax rotation
rotator = Rotator(method='varimax')
loadings_rotated = rotator.fit_transform(loadings)
# Plotting the heatmap for rotated factor loadings
plt.figure(figsize=(20,7))
sns.heatmap(pd.DataFrame(loadings_rotated, columns=columns),
annot=True, cmap='coolwarm',
yticklabels=[f'Factor {i+1}' for i in range(loadings_rotated.shape[0])],
xticklabels=columns)
plt.title('Factor Loadings After Varimax Rotation')
plt.xlabel('Variables')
plt.ylabel('Rotated Factors')
plt.xticks(rotation=45, ha="right")
plt.tight_layout()
plt.show()
# Plotting bar plots for factor loadings
# Before rotation
loadings_before_rotation = pd.DataFrame(fa.components_, columns=numerical_columns)
plt.figure(figsize=(12,6))
for i in range(loadings_before_rotation.shape[0]):
plt.bar(numerical_columns, loadings_before_rotation.iloc[i], label=f'Factor{i+1}')
plt.title('Factor Loadings Before Rotation')
plt.xlabel('Variables')
plt.ylabel('Loadings')
plt.xticks(rotation=45, ha="right")
plt.legend()
plt.tight_layout()
plt.show()
# After rotation
from sklearn.decomposition import PCA
pca = PCA(n_components=5)
df_numerical_rotated = pca.fit_transform(df_numerical_scaled)
loadings_after_rotation = pd.DataFrame(pca.components_, columns=numerical_columns)
plt.figure(figsize=(12,6))
for i in range(loadings_after_rotation.shape[0]):
plt.bar(numerical_columns, loadings_after_rotation.iloc[i], label=f'Factor{i+1}')
plt.title('Factor Loadings After Rotation')
plt.xlabel('Variables')
plt.ylabel('Loadings')
plt.xticks(rotation=45, ha="right")
plt.legend()
plt.tight_layout()
plt.show()
Please correct the code for me, only the heatmap for factor loadings (before rotate) is correct.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!