如何在scikit-learn中获取LDA的组件?

时间:2021-04-04 23:50:55

When using PCA in sklearn, it's easy to get out the components:

在sklearn中使用PCA时,可以轻松获取组件:

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

But I can't for the life of me figure out how to get the components out of LDA, as there is no components_ attribute. Is there a similar attribute in sklearn lda?

但我不能为我的生活弄清楚如何从LDA中获取组件,因为没有components_属性。 sklearn lda中是否有类似的属性?

4 个解决方案

#1


7  

In the case of PCA, the documentation is clear. The pca.components_ are the eigenvectors.

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

In the case of LDA, we need the lda.scalings_ attribute.

在LDA的情况下,我们需要lda.scalings_属性。

Example using iris data and sklearn:

使用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)   

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()

Verify that the lda.scalings_ are the eigenvectors:

验证lda.scalings_是特征向量:

print(lda.scalings_)
print(lda.transform(np.identity(4)))

Results

结果

如何在scikit-learn中获取LDA的组件?

#2


4  

There is an coef_ Attribute that probably contains what you are looking for. It should be documented. As this is a linear decision function, coef_ is probably the right name in the sklearn naming scheme.

有一个coef_属性,可能包含您要查找的内容。它应该记录在案。由于这是一个线性决策函数,因此coef_可能是sklearn命名方案中的正确名称。

You can also directly use the transform method to project data to the new space.

您还可以直接使用transform方法将数据投影到新空间。

#3


1  

My reading of the code is that the coef_ attribute is used to weight each of the components when scoring a sample's features against the different classes. scaling is the eigenvector and xbar_ is the mean. In the spirit of UTSL, here's the source for the decision function: https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/lda.py#L188

我对代码的解读是,coef_属性用于在针对不同类对样本的特征进行评分时对每个组件进行加权。缩放是特征向量,xbar_是均值。本着UTSL的精神,这里是决策功能的来源:https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/lda.py#L188

#4


0  

In PCA, the transform operation uses self.components_.T (see the code):

在PCA中,转换操作使用self.components_.T(参见代码):

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

In LDA, the transform operation uses self.scalings_ (see the code):

在LDA中,转换操作使用self.scalings_(参见代码):

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


Note the .T which transposes the array in the PCA, and not in LDA:

注意.T将数组转换为PCA,而不是LDA:

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

#1


7  

In the case of PCA, the documentation is clear. The pca.components_ are the eigenvectors.

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

In the case of LDA, we need the lda.scalings_ attribute.

在LDA的情况下,我们需要lda.scalings_属性。

Example using iris data and sklearn:

使用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)   

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()

Verify that the lda.scalings_ are the eigenvectors:

验证lda.scalings_是特征向量:

print(lda.scalings_)
print(lda.transform(np.identity(4)))

Results

结果

如何在scikit-learn中获取LDA的组件?

#2


4  

There is an coef_ Attribute that probably contains what you are looking for. It should be documented. As this is a linear decision function, coef_ is probably the right name in the sklearn naming scheme.

有一个coef_属性,可能包含您要查找的内容。它应该记录在案。由于这是一个线性决策函数,因此coef_可能是sklearn命名方案中的正确名称。

You can also directly use the transform method to project data to the new space.

您还可以直接使用transform方法将数据投影到新空间。

#3


1  

My reading of the code is that the coef_ attribute is used to weight each of the components when scoring a sample's features against the different classes. scaling is the eigenvector and xbar_ is the mean. In the spirit of UTSL, here's the source for the decision function: https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/lda.py#L188

我对代码的解读是,coef_属性用于在针对不同类对样本的特征进行评分时对每个组件进行加权。缩放是特征向量,xbar_是均值。本着UTSL的精神,这里是决策功能的来源:https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/lda.py#L188

#4


0  

In PCA, the transform operation uses self.components_.T (see the code):

在PCA中,转换操作使用self.components_.T(参见代码):

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

In LDA, the transform operation uses self.scalings_ (see the code):

在LDA中,转换操作使用self.scalings_(参见代码):

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


Note the .T which transposes the array in the PCA, and not in LDA:

注意.T将数组转换为PCA,而不是LDA:

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