简体   繁体   English

如何从scikit-learn运行和解释Fisher的线性判别分析

[英]How to run and interpret Fisher's Linear Discriminant Analysis from scikit-learn

I am trying to run a Fisher's LDA ( 1 , 2 ) to reduce the number of features of matrix. 我试图运行费舍尔的LDA( 12 )减少矩阵的特征数量。

Basically, correct if I am wrong, given n samples classified in several classes, Fisher's LDA tries to find an axis that projecting thereon should maximize the value J(w), which is the ratio of total sample variance to the sum of variances within separate classes. 基本上,正确如果我错了,给定n类样本分类,Fisher's LDA试图找到一个投影在其上的轴应该最大化值J(w),这是总样本方差与单独的方差之和的比率类。

I think this can be used to find the most useful features for each class. 我认为这可以用来为每个类找到最有用的功能。

I have a matrix X of m features and n samples (m rows, n columns). 我有一个m个特征的矩阵X和n个样本(m行,n列)。

I have a sample classification y, ie an array of n labels, each one for each sample. 我有一个样本分类y,即一个n个标签的数组,每个标签对应一个样本。

Basing on y I want to reduce the number of features to, for example, 3 most representative features. 基于y我想减少功能的数量,例如,3个最具代表性的功能。

Using scikit-learn I tried in this way (following this documentation ): 使用scikit-learn我试过这种方式(遵循本文档 ):

>>> import numpy as np
>>> from sklearn.lda import LDA
>>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
>>> y = np.array([1, 1, 1, 2, 2, 2])
>>> clf = LDA(n_components=3)
>>> clf.fit_transform(X, y)
array([[ 4.],
   [ 4.],
   [ 8.],
   [-4.],
   [-4.],
   [-8.]])

At this point I am a bit confused, how to obtain the most representative features? 此时我有点困惑,如何获得最具代表性的功能?

The features you are looking for are in clf.coef_ after you have fitted the classifier. 在安装分类器后,您正在寻找的功能位于clf.coef_

Note that n_components=3 doesn't make sense here, since X.shape[1] == 2 , ie your feature space only has two dimensions. 请注意, n_components=3在这里没有意义,因为X.shape[1] == 2 ,即您的要素空间只有两个维度。

You do not need to invoke fit_transform in order to obtain coef_ , calling clf.fit(X, y) will suffice. 你不需要调用fit_transform来获得coef_ ,调用clf.fit(X, y)就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM