ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

期货因子分析(三)

2021-01-21 16:01:43  阅读:358  来源: 互联网

标签:plt df CHECKING fa 因子分析 factor 期货 import


目录


回顾

  1. 期货因子分析(二)
  2. 数据源

代码

import numpy as np
import pandas as pd
from factor_analyzer import FactorAnalyzer
import matplotlib.pyplot as plt

from typing import TYPE_CHECKING

df = pd.read_csv("bfi.csv")
# Dropping unnecessary columns
df.drop(['Unnamed: 0', 'gender', 'education', 'age'], axis=1, inplace=True)
# Dropping missing values rows
df.dropna(inplace=True)

if TYPE_CHECKING: # A
    print(df.head())

if TYPE_CHECKING: # B
    from factor_analyzer.factor_analyzer import calculate_bartlett_sphericity

    # Bartlett's test of sphericity 是用来检测观察到的变量之间是否关联, 如果检测结果在统计学上不显著, 就不能采用因子分析.
    chi_square_value, p_value = calculate_bartlett_sphericity(df)
    print(chi_square_value, p_value)  # 18170.966350869257 0.0
    # p-value=0, 表明观察到的相关矩阵不是一个identity matrix. 

if TYPE_CHECKING: # C
    from factor_analyzer.factor_analyzer import calculate_kmo

    kmo_all, kmo_model = calculate_kmo(df)
    print(kmo_model)  # 0.848539722194922
    # Value of KMO less than 0.6 is considered inadequate.(就是kmo值要大于0.6)

if TYPE_CHECKING:  # D
    # Create factor analysis object and perform factor analysis
    fa = FactorAnalyzer(25, rotation=None)
    fa.fit(df)
    # Check Eigenvalues
    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: # E
    fa = FactorAnalyzer(5, rotation="varimax")
    fa.fit(df)
    """
    FactorAnalyzer(bounds=(0.005, 1), impute='median', is_corr_matrix=False,
            method='minres', n_factors=5, rotation='varimax',
            rotation_kwargs={}, use_smc=True)
    """

    import seaborn as sns

    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")
    # 设置y轴的字体的大小
    ax.yaxis.set_tick_params(labelsize=15)
    plt.title('Factor Analysis', fontsize='xx-large')
    # Set y-axis label
    plt.ylabel('Sepal Width', fontsize='xx-large')
    # plt.savefig('factorAnalysis.png', dpi=500)

    plt.show(dpi=500)
    
fa = FactorAnalyzer(5, rotation="varimax")
fa.fit(df)

if TYPE_CHECKING: # F
    import seaborn as sns

    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")
    # 设置y轴的字体的大小
    ax.yaxis.set_tick_params(labelsize=15)
    plt.title('Factor Analysis', fontsize='xx-large')
    # Set y-axis label
    plt.ylabel('Sepal Width', fontsize='xx-large')
    # plt.savefig('factorAnalysis.png', dpi=500)

    plt.show(dpi=500)

print(fa.get_factor_variance()) # G


分析(代码块D)

陡坡图
分析因子是否有效的方式一般为特征值>1. or 累计贡献>75%
显然从scree plot 可以看到factors_num<=6

分析代码块(E)

在这里插入图片描述

Factor 6 has none of the high loagings for any variable and is not
easily interpretable. Its good if we take only five factors. 所以尝试factors_num=5

分析(代码块G)

在这里插入图片描述

总结

简单来说,该因子组合解释了42.36%的累计贡献

如果您看到这篇文章有收获或者有不同的意见,欢迎点赞或者评论。
群:984328985
丁。

标签:plt,df,CHECKING,fa,因子分析,factor,期货,import
来源: https://blog.csdn.net/a5186050/article/details/112954248

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有