TensorFlpw Keras 深度学习人工智能实践应用 6.3源码

本文通过使用Python的Matplotlib库,展示了如何从MNIST数据集中加载并可视化手写数字图像。代码示例包括了如何使用Keras加载MNIST数据集,设置随机种子以确保结果的可重复性,以及定义函数来绘制单个图像和批量图像及其标签。

 

import numpy as np
import pandas as pd
from keras.utils import np_utils
np.random.seed(10)#随机10

from keras.datasets import mnist

(X_train_image,Y_train_image),(X_test_label,Y_test_label) =mnist.load_data()

print(X_train_image.shape)

import matplotlib.pyplot as plt
def plot_image(image):
    fig =plt.gcf()
    fig.set_size_inches(2,2)
    plt.imshow(image,cmap='binary')
    plt.show()

plot_image(X_train_image[0])

Y_train_image[0]

 

import matplotlib.pyplot as plt
def plot_images_labels_prediction(images,labels,prediction,idx,num=10):
    
    fig =plt.gcf()
    fig.set_size_inches(12,14)
    if num>25:
        num=25
    for i in range(0,num):
        ax =plt.subplot(5,5,1+i)
        ax.imshow(images[idx],cmap='binary')
        title="labels="+str(labels[idx])
        if len(prediction)>0:
             title+=",prediction="+str(prediction[idx])
                
        ax.set_title(title,fontsize=10)
        ax.set_xticks([])
        ax.set_yticks([])
        idx+=1
    plt.show()

 

plot_images_labels_prediction(X_train_image,Y_train_image,[],0)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值