回顾
代码
import numpy as np
import pandas as pd
from factor_analyzer import FactorAnalyzer
import matplotlib.pyplot as plt
import seaborn as sns
from typing import TYPE_CHECKING
df = pd.read_csv("bfi.csv")
df.drop(['Unnamed: 0', 'gender', 'education', 'age'], axis=1, inplace=True)
df.dropna(inplace=True)
if TYPE_CHECKING:
print(df.head())
if TYPE_CHECKING:
from factor_analyzer.factor_analyzer import calculate_bartlett_sphericity
chi_square_value, p_value = calculate_bartlett_sphericity(df)
print(chi_square_value, p_value)
if TYPE_CHECKING:
from factor_analyzer.factor_analyzer import calculate_kmo
kmo_all, kmo_model = calculate_kmo(df)
print(kmo_model)
if TYPE_CHECKING:
fa = FactorAnalyzer(25, rotation=None)
fa.fit(df)
ev, v = fa.get_eigenvalues()
plt.scatter(range(1, df.shape[1] + 1), ev)
plt.plot(range(1, df.shape[1] + 1), ev)
plt.title('Scree Plot')
plt.xlabel('Factors')
plt.ylabel('Eigenvalue')
plt.grid()
plt.show()
if TYPE_CHECKING:
fa = FactorAnalyzer(6, rotation="varimax")
fa.fit(df)
df_cm = pd.DataFrame(np.abs(fa.loadings_), index=df.columns)
plt.figure(figsize=(14, 14))
ax = sns.heatmap(df_cm, annot=True, cmap="BuPu")
ax.yaxis.set_tick_params(labelsize=15)
plt.title('Factor Analysis', fontsize='xx-large')
plt.ylabel('Sepal Width', fontsize='xx-large')
plt.show(dpi=500)
fa = FactorAnalyzer(5, rotation="varimax")
fa.fit(df)
if TYPE_CHECKING:
df_cm = pd.DataFrame(np.abs(fa.loadings_), index=df.columns)
plt.figure(figsize=(14, 14))
ax = sns.heatmap(df_cm, annot=True, cmap="BuPu")
ax.yaxis.set_tick_params(labelsize=15)
plt.title('Factor Analysis', fontsize='xx-large')
plt.ylabel('Sepal Width', fontsize='xx-large')
plt.show(dpi=500)
print(fa.get_factor_variance())
分析(代码块D)
分析代码块(E)
分析(代码块G)
总结
简单来说,该因子组合解释了42.36%的累计贡献
如果您看到这篇文章有收获或者有不同的意见,欢迎点赞或者评论。
群:9843285
丁。