我正在学习线性判别分析并使用scikit-learn模块。我很困惑" coef _"来自LinearDiscriminantAnalysis类的属性。据我所知,这些是判别函数系数(sklearn称它们为权重向量)。由于应该有(n_classes-1)判别函数,我希望coef_属性是一个具有形状的数组(n_components,n_features),而是打印一个(n_classes,n_features)数组。下面是使用sklearn的Iris数据集示例的示例。由于有3个类和2个组件,我希望print(lda.coef_)给我一个2x4数组而不是3x4数组......
也许我错误地解释了权重向量是什么,也许它们是分类函数的系数?
如何在每个判别式/规范函数中获得每个变量的系数?
screenshot of jupyter notebook
此处的代码:
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
import numpy as np
iris = datasets.load_iris()
X = iris.data
y = iris.target
target_names = iris.target_names
lda = LinearDiscriminantAnalysis(n_components=2,store_covariance=True)
X_r = lda.fit(X, y).transform(X)
plt.figure()
for color, i, target_name in zip(colors, [0, 1, 2], target_names):
plt.scatter(X_r2[y == i, 0], X_r2[y == i, 1], alpha=.8, color=color,
label=target_name)
plt.legend(loc='best', shadow=False, scatterpoints=1)
plt.xlabel('Function 1 (%.2f%%)' %(lda.explained_variance_ratio_[0]*100))
plt.ylabel('Function 2 (%.2f%%)' %(lda.explained_variance_ratio_[1]*100))
plt.title('LDA of IRIS dataset')
print(lda.coef_)
#output -> [[ 6.24621637 12.24610757 -16.83743427 -21.13723331]
# [ -1.51666857 -4.36791652 4.64982565 3.18640594]
# [ -4.72954779 -7.87819105 12.18760862 17.95082737]]