PCA example in docs throws a matrix index error (BoundsError)

Open
#179 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
julia
Domain
documentation

Research direction

Start by running the PCA documentation example through the Yte grouping expressions and inspect the resulting matrix and label dimensions alongside the reported stack trace. Locate the PCA example in the documentation, update the example so it runs without the BoundsError, and rerun it to confirm the three label groups are produced.

Written by the indexing model from the issue text.

Description

I run the code in the example (bar a minor change as discussed in https://github.com/JuliaStats/MultivariateStats.jl/issues/166 ) and I get a matrix index error:

This is the code (till the error)

sing MultivariateStats, RDatasets, Plots
plotly() # using plotly for 3D-interacive graphing

# load iris dataset
iris = dataset("datasets", "iris")

# split half to training set
Xtr = convert(Array,DataArray(iris[1:2:end,1:4]))'
Xtr_labels = convert(Array,DataArray(iris[1:2:end,5]))

# split other half to testing set
Xte = convert(Array,DataArray(iris[2:2:end,1:4]))'
Xte_labels = convert(Array,DataArray(iris[2:2:end,5]))

# suppose Xtr and Xte are training and testing data matrix,
# with each observation in a column

# train a PCA model, allowing up to 3 dimensions
M = fit(PCA, Xtr; maxoutdim=3)

# apply PCA model to testing set
Yte = MultivariateStats.transform(M, Xte)  # this is my change to avoid confusing usage of `transform` keyword

# reconstruct testing observations (approximately)
Xr = reconstruct(M, Yte)

The above code is OK. The following gives error in the Yte[:,Xte_labels.== expression.

# group results by testing set labels for color coding
setosa = Yte[:,Xte_labels.=="setosa"]
versicolor = Yte[:,Xte_labels.=="versicolor"]
virginica = Yte[:,Xte_labels.=="virginica"]

This is the error for the first line:

BoundsError: attempt to access 2×4 Matrix{Float64} at index [1:2, 75-element BitVector]

Stacktrace:
 [1] throw_boundserror(A::Matrix{Float64}, I::Tuple{Base.Slice{Base.OneTo{Int64}}, Base.LogicalIndex{Int64, BitVector}})
   @ Base ./abstractarray.jl:691
 [2] checkbounds
   @ ./abstractarray.jl:656 [inlined]
 [3] _getindex
   @ ./multidimensional.jl:838 [inlined]
 [4] getindex(::Matrix{Float64}, ::Function, ::BitVector)
   @ Base ./abstractarray.jl:1218
 [5] top-level scope
   @ In[11]:2
 [6] eval
   @ ./boot.jl:373 [inlined]
 [7] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1196
Dominant language
Julia
Stars
387
Forks
83
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JuliaStats/MultivariateStats.jl

All issues in JuliaStats/MultivariateStats.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.