Skip to content

Commit 9668e83

Browse files
authored
Merge pull request #83 from JuliaStats/an/smalldata
Allow fitting with a single data point and provide better error
2 parents 8bd2fd9 + 2148389 commit 9668e83

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/Loess.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ function loess(
5252
Base.require_one_based_indexing(xs)
5353
Base.require_one_based_indexing(ys)
5454

55-
if size(xs, 1) != size(ys, 1)
55+
if size(xs, 1) != length(ys)
5656
throw(DimensionMismatch("Predictor and response arrays must of the same length"))
5757
end
58+
if isempty(ys)
59+
throw(ArgumentError("input arrays are empty"))
60+
end
5861

5962
n, m = size(xs)
60-
q = floor(Int, span * n)
63+
q = max(1, floor(Int, span * n))
6164

6265
# TODO: We need to keep track of how we are normalizing so we can
6366
# correctly apply predict to unnormalized data. We should have a normalize

src/kd.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ end
99

1010

1111
struct KDTree{T <: AbstractFloat}
12-
xs::Matrix{T} # A matrix of n, m-dimensional observations
13-
perm::Vector{Int} # permutation of data to avoid modifying xs
14-
root::KDNode{T} # root node
12+
xs::Matrix{T} # A matrix of n, m-dimensional observations
13+
perm::Vector{Int} # permutation of data to avoid modifying xs
14+
root::Union{Nothing, KDNode{T}} # root node
1515
verts::Set{Vector{T}}
16-
bounds::Matrix{T} # Top-level bounding box
16+
bounds::Matrix{T} # Top-level bounding box
1717
end
1818

1919

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,9 @@ end
107107
end
108108
end
109109
end
110+
111+
@testset "small datasets. Issue 82" begin
112+
ft = loess([1.0], [1.0])
113+
@test predict(ft, 1.0) == 1.0
114+
@test_throws ArgumentError loess(Float64[], Float64[])
115+
end

0 commit comments

Comments
 (0)