【问题标题】:Number of digits in MNIST data in pythonpython中MNIST数据中的位数
【发布时间】:2022-10-25 19:13:29
【问题描述】:

如何找到训练和测试集中每个数字的样本数?此外,如果我想为训练数据中的每个数字 > 可视化至少 9 个图像。 #在Python中加载MNIST数据集

   no_of_different_labels = 10
   image_pixels = image_size * image_size```

#Loading Training Samples

```train_data = np.loadtxt("mnist_train.csv", delimiter=",")
X_train = train_data[:,1:]
t_train = train_data[:,0]```

#Loading Test Samples

```test_data = np.loadtxt("mnist_test.csv", delimiter=",")` 
X_test = test_data[:,1:]
t_test = test_data[:,0]```


Question: How can I find the number of samples per digit in training and test sets? Also if I want to >visualize at least 9 images for each digit in the training data.

【问题讨论】:

    标签: python python-3.x jupyter-notebook mnist anaconda3


    【解决方案1】:

    我认为您可以使用 Numpy 的 unique 函数,并将结果数字和计数压缩到指示数字计数的字典中:

    # y_train is the same as your t_train. It's usual to name label arrays with "y" :-)
    
    # Count elements, and return unique numbers and their counts
    unique, counts = np.unique(y_train, return_counts=True)
    >>> dict(zip(unique, counts))
    {0.0: 5923, 1.0: 6742, 2.0: 5958, 3.0: 6131, 4.0: 5842, 5.0: 5421, 6.0: 5918, 7.0: 6265, 8.0: 5851, 9.0: 5949}
    
    # The same goes for the test set!
    

    此外,如果您想为每个数字可视化一些图像,您可以使用 Python 惊人的 matplotlib 库:-)

    import matplotlib.pyplot as plt
    
    # Reshape digits into 28x28 for Matplotlib to properly print them
    X_train = X_train.reshape(60000,28,28)
    
    # Create a 2D grid of subfigures
    fig, axes = plt.subplots(10, 9, figsize=(15,18))
    
    # i is the digit iterator. j is the samples-per-digit iterator
    for i in range(10):
        # Select digits that equal "i"
        digits = X_train[y_train == i]
        for j in range(9):
            # Select axis, and print the digit on it!
            ax = axes[i, j]
            ax.imshow(digits[j], cmap="gray")
    
    # This will give you the visualization :-)
    plt.show()
    

    我希望这是有帮助的。干杯!

    【讨论】:

      猜你喜欢
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多