Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

Students also viewed these Databases questions