Skip to content
This repository was archived by the owner on May 23, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ name = "LearnBase"
uuid = "7f8f8fb0-2700-5f03-b4bd-41f8cfc144b6"
version = "0.5.0"

[deps]
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[compat]
StatsBase = "0.32, 0.33"
julia = "1.0"

[extras]
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "SparseArrays", "StatsBase"]
test = ["Test", "SparseArrays"]
3 changes: 2 additions & 1 deletion src/LearnBase.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module LearnBase

using StatsBase: nobs

# AGGREGATION MODES
include("aggmode.jl")

# VIEW AND ITERATORS
include("iteration.jl")

# OBSERVATION DIMENSIONS
export default_obsdim, getobs, getobs!
include("observation.jl")

# LEARNING COSTS (e.g. loss & penalty)
Expand Down
7 changes: 4 additions & 3 deletions src/observation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ implement `StatsBase.nobs`.
Let's see how to implement a dataset interface for a dataset
represented by an array:
```julia
using LearnBase, StatsBase
using LearnBase

function LearnBase.getobs(x::AbstractArray{T,N}, idx; obsdim=default_obsdim(x)) where {T,N}
_idx = ntuple(i-> i == obsdim ? idx : Colon(), N)
return x[_idx...]
end

StatsBase.nobs(x::AbstractArray; obsdim=default_obsdim(x)) = size(x, obsdim)
# LearnBase imports nobs from StatsBase
LearnBase.nobs(x::AbstractArray; obsdim=default_obsdim(x)) = size(x, obsdim)

X = rand(2,3)

Expand All @@ -61,7 +62,7 @@ LearnBase.getobs(t::Tuple, idx) = getobs.(t, Ref(idx))

# Assume all elements have the same nummber of observations.
# It would be safer to check explicitely though.
StatsBase.nobs(t::Tuple) = nobs(t[1])
LearnBase.nobs(t::Tuple) = nobs(t[1])

# A dataset with 3 observations, each with 2 input features
X, Y = rand(2, 3), rand(3)
Expand Down
6 changes: 4 additions & 2 deletions test/observation.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using LearnBase: getobs, nobs, default_obsdim

@test typeof(LearnBase.getobs) <: Function
@test typeof(LearnBase.getobs!) <: Function
@test typeof(LearnBase.gettargets) <: Function
Expand All @@ -9,7 +11,7 @@
_idx = ntuple(i-> i == obsdim ? idx : Colon(), N)
return x[_idx...]
end
StatsBase.nobs(x::AbstractArray; obsdim=default_obsdim(x)) = size(x, obsdim)
LearnBase.nobs(x::AbstractArray; obsdim=default_obsdim(x)) = size(x, obsdim)

a = rand(2,3)
@test nobs(a) == 3
Expand All @@ -22,7 +24,7 @@
LearnBase.getobs(t::Tuple, idx) = getobs.(t, Ref(idx))
# Assume all elements have the same nummber of observations.
# It would be safer to check explicitely though.
StatsBase.nobs(t::Tuple) = nobs(t[1])
LearnBase.nobs(t::Tuple) = nobs(t[1])

# A dataset with 3 observations, each with 2 input features
X, Y = rand(2, 3), rand(3)
Expand Down