-
Notifications
You must be signed in to change notification settings - Fork 370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Sparse ModelMatrix support #1040
Changes from all commits
c61a0ec
d46fc37
288552c
e1b068e
4a9a65f
fd12a5d
ff6f706
f94dd83
dd0ae91
db58318
25935c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,8 +48,10 @@ type ModelFrame | |
contrasts::Dict{Symbol, ContrastsMatrix} | ||
end | ||
|
||
type ModelMatrix{T <: @compat(Union{Float32, Float64})} | ||
m::Matrix{T} | ||
typealias AbstractFloatMatrix{T<:AbstractFloat} AbstractMatrix{T} | ||
|
||
type ModelMatrix{T <: AbstractFloatMatrix} | ||
m::T | ||
assign::Vector{Int} | ||
end | ||
|
||
|
@@ -321,9 +323,6 @@ function setcontrasts!(mf::ModelFrame, new_contrasts::Dict) | |
end | ||
setcontrasts!(mf::ModelFrame; kwargs...) = setcontrasts!(mf, Dict(kwargs)) | ||
|
||
asmatrix(a::AbstractMatrix) = a | ||
asmatrix(v::AbstractVector) = reshape(v, (length(v), 1)) | ||
|
||
""" | ||
StatsBase.model_response(mf::ModelFrame) | ||
Extract the response column, if present. `DataVector` or | ||
|
@@ -337,44 +336,46 @@ function StatsBase.model_response(mf::ModelFrame) | |
end | ||
end | ||
|
||
modelmat_cols(v::DataVector) = asmatrix(convert(Vector{Float64}, v.data)) | ||
modelmat_cols(v::Vector) = asmatrix(convert(Vector{Float64}, v)) | ||
## construct model matrix columns from model frame + name (checks for contrasts) | ||
function modelmat_cols(name::Symbol, mf::ModelFrame; non_redundant::Bool = false) | ||
function modelmat_cols{T<:AbstractFloatMatrix}(::Type{T}, name::Symbol, mf::ModelFrame; non_redundant::Bool = false) | ||
if haskey(mf.contrasts, name) | ||
modelmat_cols(mf.df[name], | ||
modelmat_cols(T, mf.df[name], | ||
non_redundant ? | ||
ContrastsMatrix{FullDummyCoding}(mf.contrasts[name]) : | ||
mf.contrasts[name]) | ||
else | ||
modelmat_cols(mf.df[name]) | ||
modelmat_cols(T, mf.df[name]) | ||
end | ||
end | ||
|
||
modelmat_cols{T<:AbstractFloatMatrix}(::Type{T}, v::DataVector) = convert(T, reshape(v.data, length(v), 1)) | ||
modelmat_cols{T<:AbstractFloatMatrix}(::Type{T}, v::Vector) = convert(T, reshape(v, length(v), 1)) | ||
|
||
""" | ||
modelmat_cols(v::PooledDataVector, contrast::ContrastsMatrix) | ||
modelmat_cols{T<:AbstractFloatMatrix}(::Type{T}, v::PooledDataVector, contrast::ContrastsMatrix) | ||
|
||
Construct `ModelMatrix` columns based on specified contrasts, ensuring that | ||
Construct `ModelMatrix` columns of type `T` based on specified contrasts, ensuring that | ||
levels align properly. | ||
""" | ||
function modelmat_cols(v::PooledDataVector, contrast::ContrastsMatrix) | ||
function modelmat_cols{T<:AbstractFloatMatrix}(::Type{T}, v::PooledDataVector, contrast::ContrastsMatrix) | ||
## make sure the levels of the contrast matrix and the categorical data | ||
## are the same by constructing a re-indexing vector. Indexing into | ||
## reindex with v.refs will give the corresponding row number of the | ||
## contrast matrix | ||
reindex = [findfirst(contrast.levels, l) for l in levels(v)] | ||
return contrast.matrix[reindex[v.refs], :] | ||
contrastmatrix = convert(T, contrast.matrix) | ||
return contrastmatrix[reindex[v.refs], :] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This array creation can be extremely slow for sparse There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No idea. Why is it slow? Indexing rows shouldn't be a problem for sparse matrices AFAIK. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not an expert on sparse matrix indexing, but it seems to spend a lot of time sorting... Truncated profile output from a million-row reference vector and 5-column constrast matrix:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm... You could ask on the mailing list for advice about the best algorithm to do this for sparse matrices. I guess working column by column (for |
||
end | ||
|
||
""" | ||
expandcols(trm::Vector) | ||
expandcols{T<:AbstractFloatMatrix}(trm::Vector{T}) | ||
Create pairwise products of columns from a vector of matrices | ||
""" | ||
function expandcols(trm::Vector) | ||
function expandcols{T<:AbstractFloatMatrix}(trm::Vector{T}) | ||
if length(trm) == 1 | ||
asmatrix(convert(Array{Float64}, trm[1])) | ||
trm[1] | ||
else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I can tell, the conversions here (and just above) are redundant since elements of |
||
a = convert(Array{Float64}, trm[1]) | ||
a = trm[1] | ||
b = expandcols(trm[2 : end]) | ||
reduce(hcat, [broadcast(*, a, Compat.view(b, :, j)) for j in 1 : size(b, 2)]) | ||
end | ||
|
@@ -422,8 +423,9 @@ end | |
|
||
|
||
""" | ||
ModelMatrix(mf::ModelFrame) | ||
Create a `ModelMatrix` from the `terms` and `df` members of `mf` | ||
ModelMatrix{T<:AbstractFloatMatrix}(mf::ModelFrame) | ||
Create a `ModelMatrix` of type `T` (default `Matrix{Float64}`) from the | ||
`terms` and `df` members of `mf`. | ||
|
||
This is basically a map-reduce where terms are mapped to columns by `cols` | ||
and reduced by `hcat`. During the collection of the columns the `assign` | ||
|
@@ -437,21 +439,21 @@ If there is an intercept in the model, that column occurs first and its | |
Mixed-effects models include "random-effects" terms which are ignored when | ||
creating the model matrix. | ||
""" | ||
function ModelMatrix(mf::ModelFrame) | ||
@compat function (::Type{ModelMatrix{T}}){T<:AbstractFloatMatrix}(mf::ModelFrame) | ||
dfrm = mf.df | ||
terms = droprandomeffects(dropresponse!(mf.terms)) | ||
|
||
blocks = Matrix{Float64}[] | ||
blocks = T[] | ||
assign = Int[] | ||
if terms.intercept | ||
push!(blocks, ones(size(dfrm, 1), 1)) # columns of 1's is first block | ||
push!(assign, 0) # this block corresponds to term zero | ||
push!(assign, 0) # this block corresponds to term zero | ||
end | ||
|
||
factors = terms.factors | ||
|
||
## Map eval. term name + redundancy bool to cached model matrix columns | ||
eterm_cols = @compat Dict{Tuple{Symbol,Bool}, Array{Float64}}() | ||
eterm_cols = @compat Dict{Tuple{Symbol,Bool}, T}() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't find any reason not to restrict the Array dimension here. Did I miss something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only issue I can think of is the case where a single-column term would give a column vector instead of a one-column matrix. But conversion will probably happen automatically, and tests should catch this. Have you run the tests of GLM.jl on the modified package? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I could tell |
||
## Accumulator for each term's vector of eval. term columns. | ||
|
||
## TODO: this method makes multiple copies of the data in the ModelFrame: | ||
|
@@ -462,7 +464,7 @@ function ModelMatrix(mf::ModelFrame) | |
## "promoted" full-rank versions of categorical columns for non-redundant | ||
## eval. terms: | ||
for (i_term, term) in enumerate(terms.terms) | ||
term_cols = Matrix{Float64}[] | ||
term_cols = T[] | ||
## Pull out the eval terms, and the non-redundancy flags for this term | ||
ff = Compat.view(factors, :, i_term) | ||
eterms = Compat.view(terms.eterms, ff) | ||
|
@@ -471,16 +473,17 @@ function ModelMatrix(mf::ModelFrame) | |
## and storing as necessary) | ||
for (et, nr) in zip(eterms, non_redundants) | ||
if ! haskey(eterm_cols, (et, nr)) | ||
eterm_cols[(et, nr)] = modelmat_cols(et, mf, non_redundant=nr) | ||
eterm_cols[(et, nr)] = modelmat_cols(T, et, mf, non_redundant=nr) | ||
end | ||
push!(term_cols, eterm_cols[(et, nr)]) | ||
end | ||
push!(blocks, expandcols(term_cols)) | ||
append!(assign, fill(i_term, size(blocks[end], 2))) | ||
end | ||
|
||
ModelMatrix{Float64}(reduce(hcat, blocks), assign) | ||
ModelMatrix{T}(reduce(hcat, blocks), assign) | ||
end | ||
ModelMatrix(mf::ModelFrame) = ModelMatrix{Matrix{Float64}}(mf) | ||
|
||
|
||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In what cases can
T
be different fromcontrast.matrix
's type?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
contrast.matrix
is always aMatrix{Float64}
, whileT
is whatever your desired output type is (e.g. anyAbstractFloatMatrix
subtype).