Объясни что в следующем коде происходит. import numpy as np from minisom import MiniSom…
- User
Объясни что в следующем коде происходит. import numpy as np from minisom import MiniSom from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import classification_report from keras.datasets import mnist import matplotlib.pyplot as plt import tkinter as tk import PIL from PIL import Image, ImageDraw, ImageOps
Load MNIST data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
Normalize and flatten the original images
x_train_orig = x_train.astype(np.float32).reshape(-1, 28 * 28) / 255.0 x_test_orig = x_test.astype(np.float32).reshape(-1, 28 * 28) / 255.0
Function for binarizing images
def binarize_images(images, threshold=0.5): images_bin = images.astype(np.float32) / 255.0 images_bin = (images_bin > threshold).astype(float) return images_bin
def resize_image(image, scale_factor): # Convert numpy array back to image format img = Image.fromarray((image * 255).astype(np.uint8)) # Calculate new size and resize the image new_size = (int(img.width * scale_factor), int(img.height * scale_factor)) resized_img = img.resize(new_size, resample=Image.LANCZOS) # Center crop (or pad with zeros) to 28x28 resized_img = ImageOps.pad(resized_img, (28, 28), method=Image.LANCZOS, color='black') return np.array(resized_img) / 255.0
Create scaled binary images
scale_factors = [0.5, 0.8, 1.2, 2.0] x_train_bin = binarize_images(x_train) x_test_bin = binarize_images(x_test)
Generate resized images for each scale factor
x_train_scaled = [] y_train_scaled = []
for factor in scale_factors: for img, label in zip(x_train_bin, y_train): resized_img = resize_image(img, factor) x_train_scaled.append(resized_img.flatten()) y_train_scaled.append(label)
x_train_scaled = np.array(x_train_scaled) y_train_scaled = np.array(y_train_scaled)
Concatenate original, binary, and scaled binary images
x_train_combined = np.vstack((x_train_orig, x_train_bin.reshape(-1, 28 * 28), x_train_scaled)) y_train_combined = np.concatenate((y_train, y_train, y_train_scaled))
Similarly, prepare test set (only for verification, normally you should use an unseen dataset)
x_test_scaled = []
for factor in scale_factors: for img in x_test_bin: resized_img = resize_image(img, factor) x_test_scaled.append(resized_img.flatten())
x_test_scaled = np.array(x_test_scaled)
x_test_combined = np.vstack((x_test_orig, x_test_bin.reshape(-1, 28 * 28), x_test_scaled)) y_test_combined = np.concatenate((y_test, y_test, np.repeat(y_test, len(scale_factors))))
Initialize and train the self-organizing map
som = MiniSom(x=10, y=10, input_len=28 * 28, sigma=1.0, learning_rate=0.5)
Scaling data
scaler = MinMaxScaler() x_train_scaled = scaler.fit_transform(x_train_combined) x_test_scaled = scaler.transform(x_test_combined)
- Assistant
- User
Как работает SOM ?
- Assistant
Карта самоорганизации (Self-Organizing Map, SOM) — это вид нейронной сети, предложенный Теуво Кохоненом. SOM используется для визуализации и кластеризации многомерных данных на двумерной сетке. Вот как она обычно работает: