tensorflow开发 之数字识别统计


点击查看 (人工智能) 系列文章


数据整理

在这里插入图片描述

样本数量统计

在这里插入图片描述

源码

# author: suoxd123@126.com
import tensorflow as tf

# 读取数据
mnist = tf.keras.datasets.mnist
(imgTrain, labelTrain),(imgTest, labelTest) = mnist.load_data(path='mnist.npz')
# author: suoxd123@126.com
# 将2维矩阵变为1维向量
print('source data structure')
print(imgTrain.shape, type(imgTrain))
print(imgTest.shape, type(imgTest))

imgTrain = imgTrain.reshape(60000, 784)
imgTest = imgTest.reshape(10000, 784)

print('data structure after reshape')
print(imgTrain.shape, type(imgTrain))
print(imgTest.shape, type(imgTest))

# 数据归一化处理
imgTrain = imgTrain.astype('float32')
imgTest = imgTest.astype('float32')
imgTrain /= 255
imgTest /= 255
# author: suoxd123@126.com
# 统计当前实际数据
import numpy as np
import matplotlib.pyplot as plt

label, count = np.unique(labelTrain, return_counts=True)

# 打印数值对应数量
print('0-9各数值对应的训练样本个数:')
for m,n in zip(label, count): 
    print("%d:%d;" % (m, n), end='')
# 绘图显示对应数量
fig = plt.figure()
plt.bar(label, count, width = 0.6, align = 'center')
plt.title('0-9 label distruibution')
plt.xlabel('samble data')
plt.ylabel('quantity')
plt.xticks(label)
plt.ylim(0,8000) #设置y轴边界
for m,n in zip(label, count):
    plt.text(m,n, '%d'%n , ha = 'center', va = 'bottom', fontsize = 10)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放羊郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值