-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
183 lines (162 loc) · 5.66 KB
/
dataset.py
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
from sklearn import datasets
from sklearn.model_selection import train_test_split
import numpy as np
import plotly.graph_objects as go
from plotly.offline import plot
class Kmeans_dataset:
def __init__(
self,
source,
n_clusters,
n_features,
n_samples,
random_state,
cluster_std,
test_size,
):
"""
Create a dataset for KMeans.
Source can be "random blobs", "random moon", "random circle", "iris", "aniso" or "varied"
"""
self.source = source
self.n_clusters = n_clusters
self.n_features = n_features
self.n_samples = n_samples
self.random_state = random_state
self.cluster_std = cluster_std
self.test_size = test_size
self.X_train = None
self.X_test = None
self.y_train = None
self.y_test = None
self.set_dataset()
def set_dataset(self):
if self.source == "random blobs":
X, y = datasets.make_blobs(
n_samples=self.n_samples,
n_features=self.n_features,
centers=self.n_clusters,
random_state=self.random_state,
cluster_std=self.cluster_std,
center_box=(0.0, 1.0),
)
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
X, y, test_size=self.test_size, random_state=self.random_state
)
elif self.source == "random moon":
X, y = datasets.make_moons(
n_samples=self.n_samples,
shuffle=True,
noise=0.1,
random_state=self.random_state,
)
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
X, y, test_size=self.test_size, random_state=self.random_state
)
elif self.source == "random circle":
X, y = datasets.make_circles(
n_samples=self.n_samples,
shuffle=True,
noise=0.1,
random_state=self.random_state,
factor=0.5,
)
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
X, y, test_size=self.test_size, random_state=self.random_state
)
elif self.source == "iris":
iris = datasets.load_iris()
X = iris.data
# keep only 2 features
X = X[:, :2]
y = iris.target
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
X, y, test_size=self.test_size, random_state=self.random_state
)
elif self.source == "aniso":
X, y = datasets.make_blobs(
n_samples=self.n_samples,
random_state=self.random_state,
cluster_std=self.cluster_std,
centers=4, # always 3 clusters
)
transformation = np.array([[0.7, -0.6], [-0.3, 0.8]])
X_aniso = np.dot(X, transformation)
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
X_aniso, y, test_size=self.test_size, random_state=self.random_state
)
elif self.source == "varied":
assert self.n_clusters in [
3,
4,
], "n_clusters must be 3 or 4 for this source"
X, y = datasets.make_blobs(
n_samples=self.n_samples,
centers=self.n_clusters,
cluster_std=[0.11, 0.07, 0.1, 0.04]
if self.n_clusters == 4
else [0.1, 0.05, 0.12],
random_state=self.random_state,
center_box=(0.0, 1.0),
)
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
X, y, test_size=self.test_size, random_state=self.random_state
)
else:
raise ValueError(
"Invalid source, please choose between 'random blobs', 'random moon', 'random circle', "
"'iris', 'aniso' or 'varied'"
)
def get_dataset(self):
return self.X_train, self.X_test, self.y_train, self.y_test
def plot_dataset(self, show_split=True):
"""
Plot the dataset
Parameters
- show_split: bool, default=True : if True, show the split between train and test data (different colors)
"""
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=self.X_train[:, 0],
y=self.X_train[:, 1],
mode="markers",
marker=dict(size=5, color="black" if show_split else "blue"),
name="Points d'entraînement",
)
)
fig.add_trace(
go.Scatter(
x=self.X_test[:, 0],
y=self.X_test[:, 1],
mode="markers",
marker=dict(size=5, color="black"),
name="Points de test",
)
)
fig.update_layout(
showlegend=False,
template="plotly_white",
)
plot(fig, filename="images/kmeans_data.html")
if __name__ == "__main__":
# Parameters
source = "random blobs"
n_clusters = 5
n_features = 2
n_samples = 150
random_state = 303
cluster_std = 0.03
test_size = 0.1
# Chargement des données
dataset = Kmeans_dataset(
source,
n_clusters,
n_features,
n_samples,
random_state,
cluster_std,
test_size,
)
X_train, X_test, y_train, y_test = dataset.get_dataset()
dataset.plot_dataset()