Python-sklearn中的规范判别函数

2024-09-28 05:21:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习线性判别分析和使用scikit学习模块。我被LinearDiscriminantAnalysis类的“coef_u”属性搞糊涂了。据我所知,这些是判别函数系数(sklearn称之为权重向量)。因为应该有(n_classes-1)个判别函数,所以我希望coef_u属性是一个具有形状(n_components,n_features)的数组,但是它打印的是一个(n_classes,n_features)数组。下面是一个使用sklearn的Iris数据集示例的示例。因为有3个类和2个组件,所以我希望打印(lda.系数_)给我一个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]]

Tags: fromimportiristarget属性namesplt数组

热门问题