如何使用 scikit-learn 从线性判别分析中获取特征向量 [英] How to get eigenvectors from Linear Discriminant Analysis with scikit-learn
问题描述
如何从 scikit-learn 线性判别分析对象中获得基数变化矩阵?
How can one obtain the change-of-basis matrix from a scikit-learn linear discriminant analysis object?
对于具有形状 mxp
(m
样本和 p
特征)和 N 的数组
X
类,缩放矩阵有 p
行和 N-1
列.该矩阵可用于将数据从原始空间变换到线性子空间.
For an array X
with shape m x p
(m
samples and p
features) and N
classes, the scaling matrix has p
rows and N-1
columns. This matrix can be used to transform the data from the original space to the linear subspace.
在艾莉亚的回答后
让我们考虑以下示例:
from sklearn.datasets import make_blobs
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
X, label = make_blobs(n_samples=100, n_features=2, centers=5, cluster_std=0.10, random_state=0)
lda = LDA()
Xlda = lda.fit(X, label)
Xlda.scalings_
#array([[ 7.35157288, 6.76874473],
# [-6.45391558, 7.97604449]])
Xlda.scalings_.shape
#(2, 2)
我希望 scales_ 矩阵形状为 (2,4),因为我有 2 个特征,而 LDA 将提供 5-1 个组件.
I would expect the scalings_ matrix shape to be (2,4) as I have 2 features and the LDA would provide 5-1 components.
推荐答案
让我们调用您的 LinearDiscriminantAnalysis
对象 lda
.您可以通过 lda.scalings_
访问缩放矩阵.描述这一点的文档显示在这里.
Let's call your LinearDiscriminantAnalysis
object lda
. You can access the scaling matrix as lda.scalings_
. The documentation that describes this is shown here.
import sklearn.datasets as ds
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
iris = ds.load_iris()
iris.data.shape
# (150, 4)
len(iris.target_names)
# 3
lda = LDA()
lda.fit(iris.data, iris.target)
lda.scalings_
# array([[-0.81926852, 0.03285975],
# [-1.5478732 , 2.15471106],
# [ 2.18494056, -0.93024679],
# [ 2.85385002, 2.8060046 ]])
lda.scalings_.shape
# (4, 2)
这篇关于如何使用 scikit-learn 从线性判别分析中获取特征向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!