from sklearn.datasets import load_digits
digits = load_digits(n_class=6)
X, y = digits.data, digits.target
n_samples, n_features = X.shape
n_neighbors =30
可视化数字
import matplotlib.pyplot as plt
fig, axs = plt.subplots(nrows=10, ncols=10, figsize=(6,6))for idx, ax inenumerate(axs.ravel()):
ax.imshow(X[idx].reshape((8,8)), cmap=plt.cm.binary)
ax.axis("off")
fig.suptitle("A selection from the 64-dimensional digits dataset", fontsize=16)
可视化的函数
import numpy as np
from matplotlib import offsetbox
from sklearn.preprocessing import MinMaxScaler
defplot_embedding(X, title, ax):# 标准化数据
X = MinMaxScaler().fit_transform(X)
shown_images = np.array([[1.0,1.0]])# 一个二维点,值较大for i