【问题标题】:python LDA scikit learn throws ValueErrorpython LDA scikit学习抛出ValueError
【发布时间】:2023-04-10 12:19:01
【问题描述】:

我有一个带有解释变量的 pd.DataFrame:X 和另一个带有目标变量 y 的数据框。

type(X)
Out[1]: pandas.core.frame.DataFrame

X_num.shape
Out[2]: (1213, 3298)

type(y)

Out[3]: pandas.core.frame.DataFrame

y.shape
Out[4]: (1213, 8)

我想只使用一列 y 来计算 LDA:

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
lda = LDA(n_components=2)
    for col in y:
        X_t = lda.fit(X.copy(), y[col].copy())

y 有一个列名

y[col].name
Out[5]: u'myvarname'

但我总是得到错误

ValueError: Unknown label type: (array([ 0.001, 0.003 ...

我也试过了

X_t = lda.fit(X.copy(), y[col].values.copy())

并得到同样的错误。

根据帮助拟合要求为Y

Y : array-like of response, shape = [n_samples, n_targets]
Target vectors, where n_samples in the number of samples 
and n_targets is the number of response variables.

有人知道我做错了什么吗?

【问题讨论】:

    标签: python scikit-learn lda valueerror


    【解决方案1】:

    线性判别分析是一种分类技术。根据您的错误,您的 Y 值涉及某种浮点值数组:

    array([ 0.001, 0.003 ...
    

    sklearn 不知道如何将其解释为类别标签。你确定你应该使用LDA而不是某种回归吗?

    【讨论】:

    • 太棒了,转换成字符串就可以了。 y 值是类别,但保存为浮点数,因此分类是正确的工具。
    猜你喜欢
    • 2023-03-23
    • 2015-12-22
    • 1970-01-01
    • 2017-12-22
    • 2016-05-23
    • 2018-08-15
    • 2016-01-18
    • 1970-01-01
    • 2018-10-21
    相关资源
    最近更新 更多