-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmake-a-dataset.py
More file actions
80 lines (62 loc) · 2.44 KB
/
make-a-dataset.py
File metadata and controls
80 lines (62 loc) · 2.44 KB
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
# Creating a synthetic dataset
# We will use Ctrl + I to help us write this code.
import numpy as np
if __name__ == "__main__":
# Ctrl + I
# I would like to generate a 1000 by 10 dataset
# that contains data for binary classification.
# The first column will be 1s and 0s.
# The other columns will be normally distributed.
# Insert some missing values for the
# 9 columns that have the predictor variables.
rng = np.random.default_rng(42)
n, p = 1000, 10
y = rng.binomial(1, 0.5, size=(n, 1))
X = rng.normal(loc=0.0, scale=1.0, size=(n, p - 1))
missing_rate = 0.05
mask = rng.random(size=X.shape) < missing_rate
X[mask] = np.nan
data = np.hstack((y, X))
# Ctrl + I
# The binary target variable will be has_healthy_diet
# and the predictor variables will be names of
# fruits, vegetables, and candy.
col_names = [
"has_healthy_diet",
"apple",
"banana",
"carrot",
"spinach",
"broccoli",
"orange",
"strawberry",
"grapes",
"gummy_candy",
]
assert len(col_names) == p
_original_savetxt = np.savetxt
def _savetxt_override(*args, **kwargs):
kwargs["header"] = ",".join(col_names)
return _original_savetxt(*args, **kwargs)
np.savetxt = _savetxt_override
np.savetxt("nonsensical-dataset.csv", data, delimiter=",", header="y," + ",".join(f"x{i}" for i in range(1, p)), comments="")
# Ctrl + I
# Now I want to create a new target variable based on
# a binary regression formula where the target variable
# is mostly driven by the values in the
# carrot, spinach, and broccoli columns.
# Impute missing predictor values with column means
col_means = np.nanmean(X, axis=0)
X_imputed = np.where(np.isnan(X), col_means, X)
# Coefficients: strong effects for carrot, spinach, broccoli (indices 2,3,4)
coefs = np.array([0.05, 0.05, 1.5, 1.2, 1.3, 0.05, 0.05, 0.05, 0.05])
intercept = -0.2
# Add noise and compute probabilities via logistic link
noise = rng.normal(0.0, 0.5, size=n)
logits = intercept + X_imputed.dot(coefs) + noise
probs = 1.0 / (1.0 + np.exp(-logits))
# Sample new binary target and replace first column
y_new = (rng.random(size=n) < probs).astype(int).reshape(n, 1)
data[:, 0] = y_new.ravel()
# Save the updated dataset
np.savetxt("sensical-dataset.csv", data, delimiter=",", comments="")