Home > Net >  Julia - no method matching pcacov , PCA
Julia - no method matching pcacov , PCA

Time:11-19

I am trying to apply PCA to reduce dimensionality and noise using Julia language and I got the error message as showed in the picture, could you please help me solving this issue. in any case, is there any other alternative in julia to the same task?

Many thanks Error Message

CodePudding user response:

I can't reproduce your error. But this is how I get the job done via MultivariateStats v0.10.0 package in the case of fitting a PCA mode:

julia> using MultivariateStats

julia> X = rand(5, 100);
       fit(PCA, X, maxoutdim=3)
PCA(indim = 5, outdim = 3, principalratio = 0.6599153346885055)

Pattern matrix (unstandardized loadings):
────────────────────────────────────
         PC1         PC2         PC3
────────────────────────────────────
1  0.201331   -0.0213382   0.0748083
2  0.0394825   0.137933    0.213251
3  0.14079     0.213082   -0.119594
4  0.154639   -0.0585538  -0.0975059
5  0.15221    -0.145161    0.0554158
────────────────────────────────────

Importance of components:
─────────────────────────────────────────────────────────
                                PC1        PC2        PC3
─────────────────────────────────────────────────────────
SS Loadings (Eigenvalues)  0.108996  0.0893847  0.0779532
Variance explained         0.260295  0.21346    0.186161
Cumulative variance        0.260295  0.473755   0.659915
Proportion explained       0.394436  0.323466   0.282098
Cumulative proportion      0.394436  0.717902   1.0
─────────────────────────────────────────────────────────

Consider that rows represent the features and the columns represent the data samples!
Finally, since you asked for other alternatives, I introduce you to the WeightedPCA package. Here is an example:

julia> using WeightedPCA

julia> X = rand(5, 100);
       pc1, pc2, pc3 = wpca.(Ref(collect(eachrow(X))), [1, 2, 3], Ref(UniformWeights()));

CodePudding user response:

It is useful to include an MWE with your question to allow others to reproduce your problem, and also post code blocks rather than screenshots.

That said the error message is telling you that somewhere in the PCA fit an internal function is called which requires an AbstractMatrix{T} and an AbstractVector{T} as an input, which means that the element type of both arguments T needs to be the same. In your case a Matrix{Float64} and a Vector{Real} is being passed. I assume that the Vector{Real} comes from your X input which as your first cell shows is a Matrix{Real}.

This generally indicates an issue in the construction of X, which shouldn't have an abstract element type like Real. Try float.(X) as an input to coerce all elements to Float64.

  • Related