| 1 |
""" |
| 2 |
Simple example datasets |
| 3 |
""" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import numpy as np |
| 10 |
import scipy as sp |
| 11 |
from .utils import check_random_state, deprecated |
| 12 |
|
| 13 |
|
| 14 |
def make_1D_gauss(n, m, s): |
| 15 |
"""return a 1D histogram for a gaussian distribution (`n` bins, mean `m` and std `s`) |
| 16 |
|
| 17 |
Parameters |
| 18 |
---------- |
| 19 |
n : int |
| 20 |
number of bins in the histogram |
| 21 |
m : float |
| 22 |
mean value of the gaussian distribution |
| 23 |
s : float |
| 24 |
standard deviation of the gaussian distribution |
| 25 |
|
| 26 |
Returns |
| 27 |
------- |
| 28 |
h : ndarray (`n`,) |
| 29 |
1D histogram for a gaussian distribution |
| 30 |
""" |
| 31 |
x = np.arange(n, dtype=np.float64) |
| 32 |
h = np.exp(-((x - m) ** 2) / (2 * s**2)) |
| 33 |
return h / h.sum() |
| 34 |
|
| 35 |
|
| 36 |
@deprecated() |
| 37 |
def get_1D_gauss(n, m, sigma): |
| 38 |
"""Deprecated see make_1D_gauss""" |
| 39 |
return make_1D_gauss(n, m, sigma) |
| 40 |
|
| 41 |
|
| 42 |
def make_2D_samples_gauss(n, m, sigma, random_state=None): |
| 43 |
r"""Return `n` samples drawn from 2D gaussian :math:`\mathcal{N}(m, \sigma)` |
| 44 |
|
| 45 |
Parameters |
| 46 |
---------- |
| 47 |
n : int |
| 48 |
number of samples to make |
| 49 |
m : ndarray, shape (2,) |
| 50 |
mean value of the gaussian distribution |
| 51 |
sigma : ndarray, shape (2, 2) |
| 52 |
covariance matrix of the gaussian distribution |
| 53 |
random_state : int, RandomState instance or None, optional (default=None) |
| 54 |
If int, random_state is the seed used by the random number generator; |
| 55 |
If RandomState instance, random_state is the random number generator; |
| 56 |
If None, the random number generator is the RandomState instance used |
| 57 |
by `np.random`. |
| 58 |
|
| 59 |
Returns |
| 60 |
------- |
| 61 |
X : ndarray, shape (`n`, 2) |
| 62 |
n samples drawn from :math:`\mathcal{N}(m, \sigma)`. |
| 63 |
""" |
| 64 |
|
| 65 |
generator = check_random_state(random_state) |
| 66 |
if np.isscalar(sigma): |
| 67 |
sigma = np.array( |
| 68 |
[ |
| 69 |
sigma, |
| 70 |
] |
| 71 |
) |
| 72 |
if len(sigma) > 1: |
| 73 |
P = sp.linalg.sqrtm(sigma) |
| 74 |
res = generator.randn(n, 2).dot(P) + m |
| 75 |
else: |
| 76 |
res = generator.randn(n, 2) * np.sqrt(sigma) + m |
| 77 |
return res |
| 78 |
|
| 79 |
|
| 80 |
@deprecated() |
| 81 |
def get_2D_samples_gauss(n, m, sigma, random_state=None): |
| 82 |
"""Deprecated see make_2D_samples_gauss""" |
| 83 |
return make_2D_samples_gauss(n, m, sigma, random_state=None) |
| 84 |
|
| 85 |
|
| 86 |
def make_data_classif(dataset, n, nz=0.5, theta=0, p=0.5, random_state=None, **kwargs): |
| 87 |
"""Dataset generation for classification problems |
| 88 |
|
| 89 |
Parameters |
| 90 |
---------- |
| 91 |
dataset : str |
| 92 |
type of classification problem (see code) |
| 93 |
n : int |
| 94 |
number of training samples |
| 95 |
nz : float |
| 96 |
noise level (>0) |
| 97 |
p : float |
| 98 |
proportion of one class in the binary setting |
| 99 |
random_state : int, RandomState instance or None, optional (default=None) |
| 100 |
If int, random_state is the seed used by the random number generator; |
| 101 |
If RandomState instance, random_state is the random number generator; |
| 102 |
If None, the random number generator is the RandomState instance used |
| 103 |
by `np.random`. |
| 104 |
|
| 105 |
Returns |
| 106 |
------- |
| 107 |
X : ndarray, shape (n, d) |
| 108 |
`n` observation of size `d` |
| 109 |
y : ndarray, shape (n,) |
| 110 |
labels of the samples. |
| 111 |
""" |
| 112 |
generator = check_random_state(random_state) |
| 113 |
|
| 114 |
if dataset.lower() == "3gauss": |
| 115 |
y = np.floor((np.arange(n) * 1.0 / n * 3)) + 1 |
| 116 |
x = np.zeros((n, 2)) |
| 117 |
|
| 118 |
x[y == 1, 0] = -1.0 |
| 119 |
x[y == 1, 1] = -1.0 |
| 120 |
x[y == 2, 0] = -1.0 |
| 121 |
x[y == 2, 1] = 1.0 |
| 122 |
x[y == 3, 0] = 1.0 |
| 123 |
x[y == 3, 1] = 0 |
| 124 |
|
| 125 |
x[y != 3, :] += 1.5 * nz * generator.randn(sum(y != 3), 2) |
| 126 |
x[y == 3, :] += 2 * nz * generator.randn(sum(y == 3), 2) |
| 127 |
|
| 128 |
elif dataset.lower() == "3gauss2": |
| 129 |
y = np.floor((np.arange(n) * 1.0 / n * 3)) + 1 |
| 130 |
x = np.zeros((n, 2)) |
| 131 |
y[y == 4] = 3 |
| 132 |
|
| 133 |
x[y == 1, 0] = -2.0 |
| 134 |
x[y == 1, 1] = -2.0 |
| 135 |
x[y == 2, 0] = -2.0 |
| 136 |
x[y == 2, 1] = 2.0 |
| 137 |
x[y == 3, 0] = 2.0 |
| 138 |
x[y == 3, 1] = 0 |
| 139 |
|
| 140 |
x[y != 3, :] += nz * generator.randn(sum(y != 3), 2) |
| 141 |
x[y == 3, :] += 2 * nz * generator.randn(sum(y == 3), 2) |
| 142 |
|
| 143 |
elif dataset.lower() == "gaussrot": |
| 144 |
rot = np.array( |
| 145 |
[[np.cos(theta), np.sin(theta)], [-np.sin(theta), np.cos(theta)]] |
| 146 |
) |
| 147 |
m1 = np.array([-1, 1]) |
| 148 |
m2 = np.array([1, -1]) |
| 149 |
y = np.floor((np.arange(n) * 1.0 / n * 2)) + 1 |
| 150 |
n1 = np.sum(y == 1) |
| 151 |
n2 = np.sum(y == 2) |
| 152 |
x = np.zeros((n, 2)) |
| 153 |
|
| 154 |
x[y == 1, :] = make_2D_samples_gauss(n1, m1, nz, random_state=generator) |
| 155 |
x[y == 2, :] = make_2D_samples_gauss(n2, m2, nz, random_state=generator) |
| 156 |
|
| 157 |
x = x.dot(rot) |
| 158 |
|
| 159 |
elif dataset.lower() == "2gauss_prop": |
| 160 |
y = np.concatenate((np.ones(int(p * n)), np.zeros(int((1 - p) * n)))) |
| 161 |
x = np.hstack((0 * y[:, None] - 0, 1 - 2 * y[:, None])) + nz * generator.randn( |
| 162 |
len(y), 2 |
| 163 |
) |
| 164 |
|
| 165 |
if ("bias" not in kwargs) and ("b" not in kwargs): |
| 166 |
kwargs["bias"] = np.array([0, 2]) |
| 167 |
|
| 168 |
x[:, 0] += kwargs["bias"][0] |
| 169 |
x[:, 1] += kwargs["bias"][1] |
| 170 |
|
| 171 |
else: |
| 172 |
x = np.array(0) |
| 173 |
y = np.array(0) |
| 174 |
print("unknown dataset") |
| 175 |
|
| 176 |
return x, y.astype(int) |
| 177 |
|
| 178 |
|
| 179 |
@deprecated() |
| 180 |
def get_data_classif(dataset, n, nz=0.5, theta=0, random_state=None, **kwargs): |
| 181 |
"""Deprecated see make_data_classif""" |
| 182 |
return make_data_classif(dataset, n, nz=0.5, theta=0, random_state=None, **kwargs) |