-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathdatasets_synthetic.jl
445 lines (339 loc) · 12.1 KB
/
datasets_synthetic.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
const EXTRA_KW_MAKE = """
* `eltype=Float64`: machine type of points (any subtype of
`AbstractFloat`).
* `rng=Random.GLOBAL_RNG`: any `AbstractRNG` object, or integer to seed a
`MersenneTwister` (for reproducibility).
* `as_table=true`: whether to return the points as a table (true)
or a matrix (false). """
const EXTRA_CLASSIFICATION =
"If `false` the target `y` has integer element type. "
"""
finalize_Xy(X, y, shuffle, as_table, eltype, rng; clf)
Internal function to finalize the `make_*` functions.
"""
function finalize_Xy(X, y, shuffle, as_table, eltype, rng; clf::Bool=true)
# Shuffle the rows if required
if shuffle
X, y = shuffle_rows(X, y; rng=rng)
end
if eltype != Float64
X = convert.(eltype, X)
end
# return as matrix if as_table=false
as_table || return X, y
clf && return MLJBase.table(X), categorical(y)
if length(size(y)) > 1
names = ((x) -> Symbol(string("target", x))).(collect(1:size(y, 2)))
return MLJBase.table(X), MLJBase.table(y; names)
else
clf && return MLJBase.table(X), categorical(y)
return MLJBase.table(X), y
end
end
### CLASSIFICATION TOY DATASETS
"""
runif_ab(rng, n, p, a, b)
Internal function to generate `n` points in `[a, b]ᵖ` uniformly at random.
"""
runif_ab(rng, n, p, a, b) = (b - a) .* rand(rng, n, p) .+ a
"""
X, y = make_blobs(n=100, p=2; kwargs...)
Generate Gaussian blobs for clustering and classification
problems.
### Return value
By default, a table `X` with `p` columns (features) and `n` rows
(observations), together with a corresponding vector of `n`
`Multiclass` target observations `y`, indicating blob membership.
### Keyword arguments
* `shuffle=true`: whether to shuffle the resulting points,
* `centers=3`: either a number of centers or a `c x p` matrix with `c`
pre-determined centers,
* `cluster_std=1.0`: the standard deviation(s) of each blob,
* `center_box=(-10. => 10.)`: the limits of the `p`-dimensional cube
within which the cluster centers are drawn if they are not provided,
$(EXTRA_KW_MAKE*EXTRA_CLASSIFICATION)
### Example
```julia
X, y = make_blobs(100, 3; centers=2, cluster_std=[1.0, 3.0])
```
"""
function make_blobs(n::Integer=100,
p::Integer=2;
shuffle::Bool=true,
centers::Union{<:Integer,Matrix{<:Real}}=3,
cluster_std::Union{<:Real,Vector{<:Real}}=1.0,
center_box::Pair{<:Real,<:Real}=(-10.0 => 10.0),
as_table::Bool=true,
eltype::Type{<:AbstractFloat}=Float64,
rng=Random.GLOBAL_RNG)
# check arguments make sense
if n < 1 || p < 1
throw(ArgumentError("Expected `n` and `p` to be at least 1."))
end
if center_box.first >= center_box.second
throw(ArgumentError(
"Domain for the centers improperly defined expected a pair " *
"`a => b` with `a < b`."))
end
rng = init_rng(rng)
if centers isa Matrix
if size(centers, 2) != p
throw(ArgumentError(
"The centers provided have dimension ($(size(centers, 2))) " *
"that doesn't match the one specified ($(p))."))
end
n_centers = size(centers, 1)
else
# in case the centers aren't provided, draw them from the box
n_centers = centers
centers = runif_ab(rng, n_centers, p, center_box...)
end
if cluster_std isa Vector
if length(cluster_std) != n_centers
throw(ArgumentError(
"$(length(cluster_std)) standard deviations given but there " *
"are $(n_centers) centers."))
end
if any(cluster_std .<= 0)
throw(ArgumentError(
"Cluster(s) standard deviation(s) must be positive."))
end
else
# In case only one std is given, repeat it for each center
cluster_std = fill(cluster_std, n_centers)
end
# split points equally among centers
ni, r = divrem(n, n_centers)
ns = fill(ni, n_centers)
ns[end] += r
# vector of memberships
y = vcat((fill(i, ni) for (i, ni) in enumerate(ns))...)
# Pre-generate random points then modify for each center
X = randn(rng, n, p)
nss = [1, cumsum(ns)...]
# ranges of rows for each center
rows = [nss[i]:nss[i+1] for i in 1:n_centers]
@inbounds for c in 1:n_centers
Xc = view(X, rows[c], :)
# adjust standard deviation
Xc .*= cluster_std[c]
# adjust center
Xc .+= centers[c, :]'
end
return finalize_Xy(X, y, shuffle, as_table, eltype, rng)
end
"""
X, y = make_circles(n=100; kwargs...)
Generate `n` labeled points close to two concentric circles for
classification and clustering models.
### Return value
By default, a table `X` with `2` columns and `n` rows (observations),
together with a corresponding vector of `n` `Multiclass` target
observations `y`. The target is either `0` or `1`, corresponding to
membership to the smaller or larger circle, respectively.
### Keyword arguments
* `shuffle=true`: whether to shuffle the resulting points,
* `noise=0`: standard deviation of the Gaussian noise added to the data,
* `factor=0.8`: ratio of the smaller radius over the larger one,
$(EXTRA_KW_MAKE*EXTRA_CLASSIFICATION)
### Example
```julia
X, y = make_circles(100; noise=0.5, factor=0.3)
```
"""
function make_circles(n::Integer=100;
shuffle::Bool=true,
noise::Real=0.,
factor::Real=0.8,
as_table::Bool=true,
eltype::Type{<:AbstractFloat}=Float64,
rng=Random.GLOBAL_RNG)
# check arguments make sense
if n < 1
throw(ArgumentError("Expected `n` to be at least 1."))
end
if noise < 0
throw(ArgumentError("Noise argument cannot be negative."))
end
if !(0 < factor < 1)
throw(ArgumentError(
"Factor argument must be strictly between 0 and 1."))
end
rng = init_rng(rng)
# Generate points on a 2D circle
θs = runif_ab(rng, n, 1, 0, 2pi)
n0 = div(n, 2)
X = hcat(cos.(θs), sin.(θs))
X[1:n0, :] .*= factor
y = ones(Int, n)
y[1:n0] .= 0
if !iszero(noise)
X .+= noise .* randn(rng, n, 2)
end
return finalize_Xy(X, y, shuffle, as_table, eltype, rng)
end
"""
make_moons(n::Int=100; kwargs...)
Generates labeled two-dimensional points lying close to two
interleaved semi-circles, for use with classification and clustering
models.
### Return value
By default, a table `X` with `2` columns and `n` rows (observations),
together with a corresponding vector of `n` `Multiclass` target
observations `y`. The target is either `0` or `1`, corresponding to
membership to the left or right semi-circle.
### Keyword arguments
* `shuffle=true`: whether to shuffle the resulting points,
* `noise=0.1`: standard deviation of the Gaussian noise added to the data,
* `xshift=1.0`: horizontal translation of the second center with respect to
the first one.
* `yshift=0.3`: vertical translation of the second center with respect
to the first one. $(EXTRA_KW_MAKE*EXTRA_CLASSIFICATION)
### Example
```julia
X, y = make_moons(100; noise=0.5)
```
"""
function make_moons(n::Int=150;
shuffle::Bool=true,
noise::Real=0.1,
xshift::Real=1.0,
yshift::Real=0.3,
as_table::Bool=true,
eltype::Type{<:AbstractFloat}=Float64,
rng=Random.GLOBAL_RNG)
# check arguments make sense
if n < 1
throw(ArgumentError("Expected `n` to be at least 1."))
end
if noise < 0
throw(ArgumentError("Noise argument cannot be negative."))
end
rng = init_rng(rng)
n1 = div(n, 2)
n2 = n - n1
θs = runif_ab(rng, n, 1, 0, pi)
θs[n2+1:end] .*= -1
X = hcat(cos.(θs), sin.(θs))
X[n2+1:end, 1] .+= xshift
X[n2+1:end, 2] .+= yshift
y = ones(Int, n)
y[1:n1] .= 0
if !iszero(noise)
X .+= noise .* randn(rng, n, 2)
end
return finalize_Xy(X, y, shuffle, as_table, eltype, rng)
end
### REGRESSION TOY DATASETS
"""
augment_X(X, fit_intercept)
Given a matrix `X`, append a column of ones if `fit_intercept` is true.
See [`make_regression`](@ref).
"""
function augment_X(X::Matrix{<:Real}, fit_intercept::Bool)
fit_intercept || return X
return hcat(X, ones(eltype(X), size(X, 1)))
end
"""
sparsify!(rng, θ, s)
Make portion `s` of vector `θ` exactly 0.
"""
sparsify!(rng, θ, s) = (θ .*= (rand(rng, length(θ)) .< s))
"""
outlify!(rng, y, s)
Add outliers to portion `s` of vector `y`.
"""
outlify!(rng, y, s) =
(n = length(y); y .+= 20 * randn(rng, n) .* (rand(rng, n) .< s))
const SIGMOID_64 = log(Float64(1)/eps(Float64) - Float64(1))
const SIGMOID_32 = log(Float32(1)/eps(Float32) - Float32(1))
"""
sigmoid(x)
Return the sigmoid computed in a numerically stable way:
``σ(x) = 1/(1+\\exp(-x))``
"""
function sigmoid(x::Float64)
x > SIGMOID_64 && return one(x)
x < -SIGMOID_64 && return zero(x)
return one(x) / (one(x) + exp(-x))
end
function sigmoid(x::Float32)
x > SIGMOID_32 && return one(x)
x < -SIGMOID_32 && return zero(x)
return one(x) / (one(x) + exp(-x))
end
sigmoid(x) = sigmoid(float(x))
"""
make_regression(n, p; kwargs...)
Generate Gaussian input features and a linear response with Gaussian
noise, for use with regression models.
### Return value
By default, a tuple `(X, y)` where table `X` has `p` columns and `n` rows (observations),
together with a corresponding vector of `n` `Continuous` target
observations `y`.
### Keywords
* `intercept=true`: Whether to generate data from a model with
intercept.
* `n_targets=1`: Number of columns in the target.
* `sparse=0`: Proportion of the generating weight vector that is sparse.
* `noise=0.1`: Standard deviation of the Gaussian noise added to the
response (target).
* `outliers=0`: Proportion of the response vector to make as outliers by
adding a random quantity with high variance. (Only applied if
`binary` is `false`.)
* `as_table=true`: Whether `X` (and `y`, if `n_targets > 1`) should be a table or a matrix.
* `eltype=Float64`: Element type for `X` and `y`. Must subtype `AbstractFloat`.
* `binary=false`: Whether the target should be binarized (via a sigmoid).
$EXTRA_KW_MAKE
### Example
```julia
X, y = make_regression(100, 5; noise=0.5, sparse=0.2, outliers=0.1)
```
"""
function make_regression(n::Int=100,
p::Int=2;
n_targets::Int=1,
intercept::Bool=true,
sparse::Real=0,
noise::Real=0.1,
outliers::Real=0,
binary::Bool=false,
as_table::Bool=true,
eltype::Type{<:AbstractFloat}=Float64,
rng=Random.GLOBAL_RNG)
# check arguments make sense
if n < 1 || p < 1
throw(ArgumentError("Expected `n` and `p` to be at least 1."))
end
if n_targets < 1
throw(ArgumentError("Expected `n_targets` to be at least 1."))
end
if !(0 <= sparse < 1)
throw(ArgumentError("Sparsity argument must be in [0, 1)."))
end
if noise < 0
throw(ArgumentError("Noise argument cannot be negative."))
end
if !(0 <= outliers <= 1)
throw(ArgumentError("Outliers argument must be in [0, 1]."))
end
rng = init_rng(rng)
X = augment_X(randn(rng, n, p), intercept)
y_shape = n_targets > 1 ? (n, n_targets) : n
theta_shape = n_targets > 1 ? (p + Int(intercept), n_targets) : (p + Int(intercept))
θ = randn(rng, theta_shape)
sparse > 0 && sparsify!(rng, θ, sparse)
y = X * θ
if !iszero(noise)
y .+= noise .* randn(rng, y_shape)
end
if binary
y = rand(rng, y_shape) .< sigmoid.(y)
else
if !iszero(outliers)
outlify!(rng, y, outliers)
end
end
return finalize_Xy(X[:,1:end-Int(intercept)], y, false,
as_table, eltype, rng; clf=binary)
end