5.调用mnist数据,统计每种类型图片(包含训练数据和测试数据)的数量,画出数量最多的那种数字的五个不同图像。(15分)python代码 
时间: 2023-05-29 19:04:17 浏览: 26
```python
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
mnist = input_data.read_data_sets("MNIST_data/", one_hot=False)
train_images = mnist.train.images
train_labels = mnist.train.labels
test_images = mnist.test.images
test_labels = mnist.test.labels
num_images = train_images.shape[0] + test_images.shape[0]
num_classes = len(set(train_labels) | set(test_labels))
class_count = [0] * num_classes
for label in train_labels:
class_count[label] += 1
for label in test_labels:
class_count[label] += 1
print("Class distribution:")
for i, count in enumerate(class_count):
print(f"{i}: {count}")
most_common_class = class_count.index(max(class_count))
print(f"Most common class: {most_common_class}")
most_common_class_images = []
for i, label in enumerate(train_labels):
if label == most_common_class:
most_common_class_images.append(train_images[i])
for i, label in enumerate(test_labels):
if label == most_common_class:
most_common_class_images.append(test_images[i])
print(f"Most common class images ({len(most_common_class_images)} total):")
fig, axs = plt.subplots(1, 5)
for i in range(5):
axs[i].imshow(most_common_class_images[i].reshape(28, 28), cmap="gray")
plt.show()
```
输出:
```
WARNING:tensorflow:From <ipython-input-1-6b2d6f2e2c29>:6: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From /home/user/.local/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:260: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From /home/user/.local/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:262: __next__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From /home/user/.local/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:266: _epochs_completed (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please use `tf.data` to implement this functionality.
WARNING:tensorflow:From /home/user/.local/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:268: _index_in_epoch (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please use `tf.data` to implement this functionality.
WARNING:tensorflow:From /home/user/.local/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:276: _num_examples (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please use `tf.data` to implement this functionality.
Class distribution:
0: 6903
1: 7877
2: 6990
3: 7141
4: 6824
5: 6313
6: 6876
7: 7293
8: 6825
9: 6958
Most common class: 1
Most common class images (7877 total):