【问题标题】:How to find the top three features of every principal component using pandas?如何使用 pandas 找到每个主成分的前三个特征?
【发布时间】:2020-10-11 01:29:53
【问题描述】:

我正在关注here 给出的解决方案。

但该解决方案从每个主成分中获取argmax() 特征。我想进前三。我该怎么办?

我基本上想知道哪些功能分别对每台 PC 的影响最大。

谢谢。

【问题讨论】:

    标签: python pandas pca


    【解决方案1】:

    您可以使用np.argsortnp.argpartition 获得排序后的索引。按照指示的问题的程序进行

    # With argsort 
    most_important = [np.argsort(np.abs(model.components_[i]))[::-1][:3] for i in range(n_pcs)]
    
    # With argpartition
    most_important = [np.argpartition(np.abs(model.components_[i]), -3)[-3:] for i in range(n_pcs)]
    
    most_important
    >>> [array([4, 1, 0]), array([2, 3, 4])]
    

    然后获取最重要的组件作为列

    initial_feature_names = ['a','b','c','d','e']
    
    # Notices the [::-1] is used to order the component names
    most_important_names = [[initial_feature_names[i] for i in most_important[i][::-1]] for i in range(n_pcs)]
    dic = {'PC{}'.format(i): most_important_names[i] for i in range(n_pcs)}
    pd.DataFrame.from_dict(dic).T
    >>>
        0   1   2
    PC0 e   b   a
    PC1 c   d   e
    

    【讨论】:

      猜你喜欢
      • 2016-10-16
      • 2017-09-20
      • 2013-10-21
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多