julia - 使用 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)
解决方案
您有两个通过这种方式解决的问题:
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
。
我希望这有帮助。
推荐阅读
- if-statement - 如何通过 NGINX 中的标头值创建 proxy_pass 联结?
- node.js - Crypto Hmac 在 Node.js 中创建不同的哈希
- javascript - 添加带有函数参数的 EventListener
- python-3.x - Matplotlib 导入错误(预期的 str、bytes 或 os.PathLike 对象,而不是 PosixPath)
- javascript - VUE检测按键和重复按键检测错误
- php - Laravel 不会触发更新功能
- java - 在java中将int分配给字节
- python - Pandas:将列与列表的值进行比较并复制匹配的行
- postgresql - Ansible AWX 中出现意外错误,找不到修复重复条目的解决方案
- javascript - 如何在 jest node.js 中测试 if else 条件