-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuage.py
52 lines (41 loc) · 1.19 KB
/
nuage.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
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import NMF
from cos_nmf import COSNMF
# Générer des données de nuage de points aléatoires
X = np.random.rand(100, 2)
plt.scatter(X[:, 0], X[:, 1])
plt.show()
# Appliquer COSNMF
#W, H = COSNMF(X, X.shape[0])
# Appliquer NMF de Sklearn
model = NMF(n_components=2, init='random', random_state=0)
W = model.fit_transform(X)
H = model.components_
# Assigner des couleurs à chaque cluster
colors = ['r', 'g', 'b']
labels = np.argmax(W, axis=1)
colors = [colors[label] for label in labels]
# Afficher le nuage de points avec des couleurs pour chaque cluster
plt.scatter(X[:, 0], X[:, 1], c=colors)
plt.show()
# Créer les données
x = np.random.randn(100)
y = np.random.randn(100)
mask = (x > 0) & (y > 0)
x = x[mask]
y = y[mask]
# Tracer le nuage de points
plt.scatter(x, y)
#plt.title('Nuage de points')
plt.xlabel('Axe X')
plt.ylabel('Axe Y')
z = np.column_stack((x, y))
model = NMF(n_components=2, init='random', random_state=0)
W1 = model.fit_transform(z)
H1 = model.components_
colors = ['r', 'g', 'b']
labels = np.argmax(W1, axis=1)
colors = [colors[label] for label in labels]
plt.scatter(z[:, 0], z[:, 1], c=colors)
plt.show()