【问题标题】:How do I get the components for LDA in scikit-learn?如何在 scikit-learn 中获取 LDA 的组件?
【发布时间】:2012-12-08 00:39:57
【问题描述】:

在 sklearn 中使用 PCA 时,很容易取出组件:

from sklearn import decomposition
pca = decomposition.PCA(n_components=n_components)
pca_data = pca.fit(input_data)
pca_components = pca.components_

但我一生都无法弄清楚如何从 LDA 中取出组件,因为没有 components_ 属性。 sklearn lda中是否有类似的属性?

【问题讨论】:

  • 您能确认以下哪个答案是正确的吗?我想使用components_ 在 LDA 双图上绘制加载向量。你用sklearn 做过这个吗?
  • @agartland 我发布了一个扩展答案,解释了一切。

标签: python machine-learning scikit-learn


【解决方案1】:

我对代码的阅读是,coef_ 属性用于在针对不同类别对样本的特征进行评分时对每个组件进行加权。 scaling 是特征向量,xbar_ 是平均值。本着 UTSL 的精神,这里是决策函数的来源: https://github.com/scikit-learn/scikit-learn/blob/6f32544c51b43d122dfbed8feff5cd2887bcac80/sklearn/discriminant_analysis.py#L166

【讨论】:

    【解决方案2】:

    对于 PCA,文档很清楚。 pca.components_ 是特征向量。

    对于 LDA,我们需要lda.scalings_ 属性。


    使用 iris 数据和 sklearn 的可视化示例:

    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn import datasets
    import pandas as pd
    from sklearn.preprocessing import StandardScaler
    from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
    
    
    iris = datasets.load_iris()
    X = iris.data
    y = iris.target
    #In general it is a good idea to scale the data
    scaler = StandardScaler()
    scaler.fit(X)
    X=scaler.transform(X)
    
    lda = LinearDiscriminantAnalysis()
    lda.fit(X,y)
    x_new = lda.transform(X)   
    

    验证 lda.scalings_ 是特征向量:

    print(lda.scalings_)
    print(lda.transform(np.identity(4)))
    
    [[-0.67614337  0.0271192 ]
     [-0.66890811  0.93115101]
     [ 3.84228173 -1.63586613]
     [ 2.17067434  2.13428251]]
    
    [[-0.67614337  0.0271192 ]
     [-0.66890811  0.93115101]
     [ 3.84228173 -1.63586613]
     [ 2.17067434  2.13428251]]
    

    此外,这里还有一个有用的函数来绘制双标图并进行视觉验证:

    def myplot(score,coeff,labels=None):
        xs = score[:,0]
        ys = score[:,1]
        n = coeff.shape[0]
    
        plt.scatter(xs ,ys, c = y) #without scaling
        for i in range(n):
            plt.arrow(0, 0, coeff[i,0], coeff[i,1],color = 'r',alpha = 0.5)
            if labels is None:
                plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, "Var"+str(i+1), color = 'g', ha = 'center', va = 'center')
            else:
                plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, labels[i], color = 'g', ha = 'center', va = 'center')
    
    plt.xlabel("LD{}".format(1))
    plt.ylabel("LD{}".format(2))
    plt.grid()
    
    #Call the function. 
    myplot(x_new[:,0:2], lda.scalings_) 
    plt.show()
    

    结果

    【讨论】:

    • 如果投反对票的人发表评论解释原因,我将不胜感激。
    【解决方案3】:

    在 PCA 中,变换操作使用self.components_.T(参见the code):

        X_transformed = np.dot(X, self.components_.T)
    

    在 LDA 中,变换操作使用self.scalings_(参见the code):

        X_new = np.dot(X, self.scalings_)
    


    注意.T,它在 PCA 中转置数组,而不是在 LDA 中:

    • PCA:components_ : array, shape (n_components, n_features)
    • LDA:scalings_ : array, shape (n_features, n_classes - 1)

    【讨论】:

      猜你喜欢
      • 2017-10-27
      • 2020-11-11
      • 1970-01-01
      • 2016-02-27
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 2020-06-21
      • 2015-09-15
      相关资源
      最近更新 更多