Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

no method match fit while running PCA #71

Closed
xiuliren opened this issue Sep 7, 2018 · 4 comments
Closed

no method match fit while running PCA #71

xiuliren opened this issue Sep 7, 2018 · 4 comments

Comments

@xiuliren
Copy link

xiuliren commented Sep 7, 2018

I am running the example code from documentation, but get some error.
I removed the DataArray to since it was not defined in Julia 0.7 packages.

using 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, iris[1:2:end,1:4])'
Xtr_labels = convert(Array, iris[1:2:end,5])

# split other half to testing set
Xte = convert(Array,iris[2:2:end,1:4])'
Xte_labels = convert(Array,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 = transform(M, Xte)

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

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

# visualize first 3 principal components in 3D interacive plot
p = scatter(setosa[1,:],setosa[2,:],setosa[3,:],marker=:circle,linewidth=0)
scatter!(versicolor[1,:],versicolor[2,:],versicolor[3,:],marker=:circle,linewidth=0)
scatter!(virginica[1,:],virginica[2,:],virginica[3,:],marker=:circle,linewidth=0)
plot!(p,xlabel="PC1",ylabel="PC2",zlabel="PC3")

I am getting the following errors:

MethodError: no method matching fit(::Type{PCA}, ::LinearAlgebra.Adjoint{Float64,Array{Float64,2}}; maxoutdim=3)
Closest candidates are:
  fit(!Matched::Type{Histogram}, ::Any...; kwargs...) at /usr/people/jingpeng/.julia/packages/StatsBase/NzjNi/src/hist.jl:340
  fit(!Matched::StatisticalModel, ::Any...) at /usr/people/jingpeng/.julia/packages/StatsBase/NzjNi/src/statmodels.jl:151 got unsupported keyword argument "maxoutdim"
  fit(!Matched::Type{Distributions.Beta}, ::AbstractArray{T<:Real,N} where N) where T<:Real at /usr/people/jingpeng/.julia/packages/Distributions/y4rh9/src/univariate/continuous/beta.jl:129 got unsupported keyword argument "maxoutdim"
  ...

Stacktrace:
 [1] top-level scope at In[38]:19
@xiuliren
Copy link
Author

xiuliren commented Sep 7, 2018

it turns out that the transposed array is not an array any more! I have to convert it to Array again!

julia> a = rand(5,3)'                                 
3×5 LinearAlgebra.Adjoint{Float64,Array{Float64,2}}:  
 0.165982  0.118656  0.541775  0.515358   0.737706    
 0.20481   0.443253  0.199402  0.507814   0.694907    
 0.427047  0.831928  0.75973   0.0487185  0.595516    
using MultivariateStats, RDatasets, Plots
plotly() # using plotly for 3D-interacive graphing

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

# split half to training set
@show iris[1:2:end,1:4]
Xtr = convert(Array, iris[1:2:end,1:4])' |> Array
@show Xtr
@show typeof(Xtr)
@show size(Xtr)
Xtr_labels = convert(Array, iris[1:2:end,5])

# split other half to testing set
Xte = convert(Array,iris[2:2:end,1:4])' |> Array
Xte_labels = convert(Array,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 = transform(M, Xte)

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

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

# visualize first 3 principal components in 3D interacive plot
p = scatter(setosa[1,:],setosa[2,:],setosa[3,:],marker=:circle,linewidth=0)
scatter!(versicolor[1,:],versicolor[2,:],versicolor[3,:],marker=:circle,linewidth=0)
scatter!(virginica[1,:],virginica[2,:],virginica[3,:],marker=:circle,linewidth=0)
plot!(p,xlabel="PC1",ylabel="PC2",zlabel="PC3")

I still got some other error though:

UndefVarError: covm not defined

Stacktrace:
 [1] #fit#10(::Symbol, ::Int64, ::Float64, ::Nothing, ::Function, ::Type{PCA}, ::Array{Float64,2}) at /usr/people/jingpeng/.julia/packages/MultivariateStats/wGpiN/src/pca.jl:151
 [2] (::getfield(StatsBase, Symbol("#kw##fit")))(::NamedTuple{(:maxoutdim,),Tuple{Int64}}, ::typeof(fit), ::Type{PCA}, ::Array{Float64,2}) at ./none:0
 [3] top-level scope at In[77]:26

@xiuliren
Copy link
Author

xiuliren commented Sep 7, 2018

checked the code, not sure why there is such an error. I switched to svd method, it works for me now.

@wildart
Copy link
Collaborator

wildart commented Sep 9, 2018

Switch to master branch until a new version release.

@wildart
Copy link
Collaborator

wildart commented Sep 12, 2018

New version released, JuliaLang/METADATA.jl#17965

@wildart wildart closed this as completed Sep 12, 2018
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants