Skip to content
Snippets Groups Projects
Commit 666f3b48 authored by leo.harb's avatar leo.harb
Browse files

code de base terminé

parent 323b1ac4
No related branches found
No related tags found
No related merge requests found
# SOM
Auteurs: Harb Leo & Pavlovich Ivan
## TODO
- [ ] représentation des neurones
- [ ] comprendre le model
- [ ] montrer les résultats (vecteur, pourcentage de classe par neurone, pourcentage de prédiction juste) en fonction de différents paramètres
- [ ] comparer a k-means
- [ ] utiliser iris (pourcentage de classe par neurone et pourcentage de prévision juste)
from sklearn.datasets import load_iris
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import MinMaxScaler
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.utils import shuffle
from collections import Counter
from minisom import MiniSom
import matplotlib.pyplot as plt
from pylab import bone, pcolor, colorbar, plot
def som_iris():
# Charger les données
def load_and_normalize_mnist():
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(-1, 784).astype('float32') / 255.0
X_test = X_test.reshape(-1, 784).astype('float32') / 255.0
return X_train, y_train, X_test, y_test
def load_and_normalize_iris():
iris = load_iris()
data = pd.DataFrame(iris.data, columns=iris.feature_names)
# Normaliser les données
scaler = MinMaxScaler()
data_normalized = scaler.fit_transform(data)
# Définir la taille de la carte (10x10 dans cet exemple)
som_size = (10, 10)
som = MiniSom(som_size[0], som_size[1], 4, sigma=1.0, learning_rate=0.5)
# Initialiser les poids
som.random_weights_init(data_normalized)
# Entraîner la SOM
som.train_random(data_normalized, 1000) # 1000 itérations
# Visualiser la carte
bone() # Créer le squelette de la carte
pcolor(som.distance_map().T) # Afficher les distances entre les neurones
colorbar() # Ajouter une barre de couleur
# Ajouter les étiquettes des points
target_names = iris.target_names
for i, (x, t) in enumerate(zip(data_normalized, iris.target)):
w = som.winner(x) # Trouver le BMU
plot(w[0] + 0.5, w[1] + 0.5, # Positionner le point sur la carte
marker='o', color=['r', 'g', 'b'][t],
markeredgecolor='k', markersize=8)
plt.show()
X, y = shuffle(iris.data, iris.target, random_state=42)
X = MinMaxScaler().fit_transform(X)
return X, y
if __name__ == '__main__':
# Charger MNIST
(X_train, y_train), _ = mnist.load_data()
X_train = X_train.reshape(-1, 784).astype('float32') / 255.0 # Normalisation
def train_som(X_train, som_size=(10, 10), sigma=1.0, learning_rate=0.5, iterations=5000):
som = MiniSom(som_size[0], som_size[1], X_train.shape[1], sigma=sigma, learning_rate=learning_rate)
som.random_weights_init(X_train)
som.train_random(X_train, iterations)
return som
# Créer et entraîner la SOM
som_size = (10, 10) # Carte 10x10
som = MiniSom(som_size[0], som_size[1], 784, sigma=1.0, learning_rate=0.5)
som.random_weights_init(X_train[:1000]) # Initialisation avec un sous-échantillon
som.train_random(X_train[:1000], 1000) # Entraînement
def map_neurons_to_classes(som, X_train, y_train):
bmu_mapping = {}
for x, label in zip(X_train, y_train):
bmu = som.winner(x)
if bmu not in bmu_mapping:
bmu_mapping[bmu] = []
bmu_mapping[bmu].append(label)
bmu_classes = {bmu: Counter(labels).most_common(1)[0][0] for bmu, labels in bmu_mapping.items()}
return bmu_classes
# Afficher le contenu des neurones
def visualize_class_map(bmu_classes, som_size):
plt.figure(figsize=(10, 10))
for i in range(som_size[0]):
for j in range(som_size[1]):
# Obtenir les poids du neurone (i, j)
weights = som.get_weights()[i, j].reshape(28, 28) # Reshape en 28x28
plt.subplot(som_size[0], som_size[1], i * som_size[1] + j + 1)
plt.imshow(weights, cmap='gray') # Afficher comme image en niveaux de gris
label = bmu_classes.get((i, j), '.')
plt.text(i + 0.5, j + 0.5, str(label), color='red' if label != '.' else 'gray', ha='center', va='center', fontsize=12)
plt.xlim([0, som_size[0]])
plt.ylim([0, som_size[1]])
plt.gca().invert_yaxis()
plt.title("Classe majoritaire pour chaque neurone")
plt.show()
# Fonction pour prédire les classes des données de test
def predict(som, X_test, bmu_classes):
return [bmu_classes.get(som.winner(x), -1) for x in X_test]
def visualize_neuron_weights(som, som_size):
plt.figure(figsize=som_size)
for i in range(som_size[0]):
for j in range(som_size[1]):
weights = som.get_weights()[i, j].reshape(28, 28)
plt.subplot(som_size[0], som_size[1], j * som_size[1] + i + 1)
plt.imshow(weights, cmap='gray')
plt.axis('off')
plt.show()
\ No newline at end of file
plt.show()
def evaluate_kmeans(X_train, y_train, X_test, y_test, n_clusters):
kmeans = KMeans(n_clusters=n_clusters, random_state=42)
kmeans.fit(X_train)
cluster_mapping = {}
for cluster in range(n_clusters):
cluster_labels = y_train[kmeans.labels_ == cluster]
if len(cluster_labels) > 0:
cluster_mapping[cluster] = Counter(cluster_labels).most_common(1)[0][0]
y_pred = [cluster_mapping.get(cluster, -1) for cluster in kmeans.predict(X_test)]
return accuracy_score(y_test, y_pred)
if __name__ == "__main__":
dataset = input("Choisissez un dataset (mnist/iris): ").strip().lower()
if dataset == "mnist":
X_train, y_train, X_test, y_test = load_and_normalize_mnist()
som_size = (10, 10)
som = train_som(X_train[:1000], som_size)
bmu_classes = map_neurons_to_classes(som, X_train[:1000], y_train[:1000])
visualize_class_map(bmu_classes, som_size)
y_pred = predict(som, X_test[:1000], bmu_classes)
som_accuracy = accuracy_score(y_test[:1000], y_pred)
print(f"SOM Accuracy: {som_accuracy * 100:.2f}%")
visualize_neuron_weights(som, som_size)
kmeans_accuracy = evaluate_kmeans(X_train[:1000], y_train[:1000], X_test[:1000], y_test[:1000], n_clusters=10)
print(f"KMeans Accuracy: {kmeans_accuracy * 100:.2f}%")
elif dataset == "iris":
X, y = load_and_normalize_iris()
train_size = int(0.8 * len(X))
X_train, y_train = X[:train_size], y[:train_size]
X_test, y_test = X[train_size:], y[train_size:]
# Entraîner la SOM
som_size = (6, 6)
som = train_som(X_train, som_size)
bmu_classes = map_neurons_to_classes(som, X_train, y_train)
visualize_class_map(bmu_classes, som_size)
y_pred = predict(som, X_test, bmu_classes)
som_accuracy = accuracy_score(y_test, y_pred)
print(f"SOM Accuracy: {som_accuracy * 100:.2f}%")
# Évaluer KMeans
kmeans_accuracy = evaluate_kmeans(X_train, y_train, X_test, y_test, n_clusters=3)
print(f"KMeans Accuracy: {kmeans_accuracy * 100:.2f}%")
else:
print("Dataset non reconnu. Choisissez 'mnist' ou 'iris'.")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment