Search code examples
python-3.xpandasdataframepca

Dimension Reduction Using varimax rotation and method used is Principal . to find component score coefficient matrix?


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import decomposition
from sklearn import datasets
from sklearn.preprocessing import scale
from sklearn.decomposition import PCA
from sklearn.impute import SimpleImputer
from factor_analyzer import FactorAnalyzer
from sklearn.preprocessing import StandardScaler

df = pd.read_excel('excel path')
df.drop(['DISTRICT'],axis=1,inplace=True)
numCOl = df
numCorr = numCOl.corr()
print(numCorr.round(3))
from factor_analyzer.factor_analyzer import calculate_bartlett_sphericity

chi_square_value,p_value=calculate_bartlett_sphericity(df)
chi_square_value, p_value

#performing kaiser mayer test (KO test)
from factor_analyzer.factor_analyzer import calculate_kmo
kmo_all,kmo_model = calculate_kmo(df)
kmo_model

fa = FactorAnalyzer()
fa.fit(df)

ev,v = fa.get_eigenvalues()
print(ev)

# ploting the graph
plt.scatter(range(1,df.shape[1]+1),ev)
plt.plot(range(1,df.shape[1]+1),ev)
plt.title('Screen Plot')
plt.xlabel('Number of Factors')
plt.ylabel('Eigenvalue')
plt.grid()
plt.show()

fa = FactorAnalyzer(n_factors=4,method='principal',rotation='varimax')
fa.fit(df)
data_1 = pd.DataFrame(fa.loadings_,index=df.columns)

data_1.round(3)

here all my code which gives me rotated component matrix but i'm looking for component score coefficient matrix??? Dimension Reduction Using varimax rotation and method used is Principal . to find component score coefficient matrix???


Solution

  • I got the solution of this --

    1. After above steps find cumalities
    Print(cumalities.round(3))
    
    1. Component score Coefficent Matrix
    pcScore = np.linalg.inv(numCorr).dot(data_1)
    New_Data = pd.DataFrame(pcScore,index=df.columns)
    Print(New_Data.round(3))
    

    enter image description here