We want to connect the people who have knowledge to the people who need it, to bring together people with different perspectives so they can understand each other better, and to empower everyone to share their knowledge.
import cv2 import csv import os import numpy as np import matplotlib.pyplot as plt from sklearn.decomposition import PCA # Đọc dữ liệu nhãn def read_labels(csv_file): data_array = [] with open(csv_file, mode='r') as file: csv_reader = csv.reader(file) for row in csv_reader: data_array.append(int(row[0])) return data_array # Lấy danh sách ảnh trong thư mục. def read_image_paths(image_folder): image_files = os.listdir(image_folder) image_paths = [] for image_file in image_files: image_paths.append(os.path.join(image_folder, image_file)) return sorted(image_paths) # Tải và xử lý một ảnh. def preprocess_image(image_path): image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) return image.flatten() # Tải và xử lý bộ dữ liệu ảnh. def load_dataset(image_paths): images = [] for path in image_paths: images.append(preprocess_image(path)) return np.array(images) # Khởi tạo dữ liệu train_images_path = os.path.join('data', 'FashionMNIST', 'images', 'train-images') test_label_path = os.path.join('data', 'FashionMNIST', 'labels', 'train-labels.csv') X_train = load_dataset(read_image_paths(train_images_path)) y_train = np.array(read_labels(test_label_path)) fashion_mnist_labels = { 0: "T-shirt/top: Áo thun hoặc áo trên", 1: "Trouser: Quần dài", 2: "Pullover: Áo len chui đầu", 3: "Dress: Váy", 4: "Coat: Áo khoác", 5: "Sandal: Dép sandal", 6: "Shirt: Áo sơ mi", 7: "Sneaker: Giày thể thao", 8: "Bag: Túi xách", 9: "Ankle boot: Giày bốt cổ chân" } # Sử dụng PCA để giảm số chiều xuống 2 pca = PCA(n_components=2) # Giảm dữ liệu xuống 2 chiều X_train_pca = pca.fit_transform(X_train) # Vẽ biểu đồ phân bố dữ liệu trên không gian 2D # Biểu đồ hiển thị phân bố các mẫu kiểm thử trên không gian PCA 2D theo nhãn thật plt.figure(figsize=(8, 6)) # Tạo bảng màu cho các lớp colors = plt.cm.rainbow(np.linspace(0, 1, len(np.unique(y_train)))) # Vẽ từng lớp for label, color in zip(np.unique(y_train), colors): mask = y_train == label plt.scatter(X_train_pca[mask, 0], X_train_pca[mask, 1], color=color, label=f"{fashion_mnist_labels[label]}") plt.xlabel("Thành phần 1") plt.ylabel("Thành phần 2") plt.title("Phân lớp trên bộ dữ liệu nhãn thật") plt.legend() plt.show()
2.2K Point(s)
1.2K Point(s)
313 Point(s)
178 Point(s)
138 Point(s)
Input your email to receive reset password link, or if you remember your password, you can click