import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sns from sklearn import datasets from sklearn import manifold from sklearn.metrics import accuracy_score # 获取数据 data = datasets.fetch_openml('mnist_784', version=1, return_X_y=True) pixel_values, targets = data targets = targets.astype(int) pixel_array = pixel_values.to_numpy() single_image = pixel_array[1, :].reshape(28, 28) # 展示图像 plt.imshow(single_image, cmap='gray') plt.show()
def visualize_tsne(X, y): plt.figure(figsize=(10, 8)) sns.scatterplot(x=X[:, 0], y=X[:, 1], hue=y, palette=sns.color_palette("hls", 10), legend="full", alpha=0.8) plt.title('t-SNE visualization of MNIST data') plt.show()
# 使用 t-SNE 进行降维 tsne = manifold.TSNE(n_components=2, random_state=42, perplexity=25) transformed_data = tsne.fit_transform(pixel_array)