Home > Blockchain >  No method matching pcacov
No method matching pcacov

Time:11-21

I am trying to apply PCA to reduce dimensionality and noise using Julia language but am getting an error message. Could you please help me to solve this issue.

Are there other alternatives in julia to the perform the same task?

Here's the error message:

julia> X = (train_input)' |> Array;

julia> typeof(X)
Matrix{Real} (alias for Array{Real, 2})

julia> using MultivariateStats, MLJMultivariateStatsInterface

julia> M = fit(PCA, X; maxoutdim = 3)
MethodError: no method matching pcacov(::Matrix{Float64}, ::Vector{Real}; maxoutdim=3, pratio=0.99)
Closest candidates are: 
  pcacov(::AbstractMatrix{T}, ::AbstractVector{T}; maxoutdim, pratio) where T<: Real at C:\Users\USER\.julia\packages\MultivariateStats\rCiqT\src\pca.jl:209

CodePudding user response:

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

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
─────────────────────────────────────────────────────────

julia> typeof(X)
Matrix{Float64} (alias for Array{Float64, 2})

julia> eltype(X)
Float64

As you can see, I used a Matrix with Float64 element types as the input. This is the difference between my input in comparison with yours, I guess. So this might be the problem in your case. Keep in mind 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. This package provides weighted principal component analysis (PCA) for data with samples of heterogeneous quality (heteroscedastic noise). Here is a quick example:

julia> using WeightedPCA

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

In the above, I fitted an equally weighted PCA on the X data and I requested values on 1, 2, and 3 principal components. Using this package, you can even apply specific weights or optimal weights. This package can be installed by pkg> add https://github.com/dahong67/WeightedPCA.jl.

CodePudding user response:

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