Page 11
single_image = pixel_values[1, :].reshape(28, 28)
plt.imshow(single_image, cmap='gray')
用这个代码运行是会出错的,应该改为以下代码:
pixel_np = pixel_values.iloc[1, :].values
single_image = pixel_np.reshape(28, 28)
plt.imshow(single_image, cmap='gray')
从头开始到这里的完整代码如下:
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
%matplotlib inline
data = datasets.fetch_openml(
'mnist_784',
version=1,
return_X_y=True
)
pixel_values, targets = data
targets = targets.astype(int)
pixel_np = pixel_values.iloc[1, :].values
single_image = pixel_np.reshape(28, 28)
plt.imshow(single_image, cmap='gray')