首页 > 解决方案 > 使用 MultivariateStats 的 Julia 上的 LDA

问题描述

我正在使用 Brunton & Kutz 的“数据驱动科学与工程”一书学习分类方法,但我不只使用 MATLAB 和 Python 代码资源,而是使用 Julia 重写教科书示例,因为这是我的主要编程语言。

我找不到为什么将 MulticlassLDA 模型拟合到数据不起作用,它返回 a DimensionMismatch("Inconsistent array sizes."),但据我所知,我的数组被分派给 fit 函数,如文档中所示。

这是我的代码:

using MAT, LinearAlgebra, Statistics, MultivariateStats

# Load data in MATLAB format. Abailible in http://www.databookuw.com/

dogs = read(matopen("../DATA/dogData_w.mat"),"dog_wave") 
cats = read(matopen("../DATA/catData_w.mat"),"cat_wave")
    
CD = hcat(dogs,cats)

u, s, v = svd(CD .- mean(CD)) #SVD decomposition

xtrain = vcat(v[1:60,2:2:4],v[81:140,2:2:4])   #training data array, dims 120x2
label = Int.(vcat(ones(60),-ones(60)))         #label's vector, length 120
xtest = vcat(v[61:80,2:2:4],v[141:160,2:2:4])  

classf= fit(MulticlassLDA,2,xtrain,label)

标签: juliaclassificationldamulticlass-classification

解决方案


您有两个通过这种方式解决的问题:

label = [fill(1, 60); fill(2, 60)] # labels must range from 1 to n
fit(MulticlassLDA,2,permutedims(xtrain),label) # observations in xtrain must be stored in columns (not rows)

请参阅https://multivariatestatsjl.readthedocs.io/en/stable/index.html中的评论:

这个包中实现的所有方法都采用 JuliaStats 的列优先约定:在数据矩阵中,每一列对应一个样本/观察值,而每一行对应一个特征(变量或属性)。

y以及关于适合https://multivariatestatsjl.readthedocs.io/en/stable/mclda.html#data-analysis的参数的解释:

y– 类别标签的向量,长度为n。的每个元素y必须是 和 之间的1整数nc

我希望这有帮助。


推荐阅读