Numpy 101

import numpy as np

>>> x = np.array(12)
>>> x
array(12)
>>> x.dim
0

>>> x = np.array([12, 3, 6, 14])
>>> x
array([12, 3, 6, 14])
>>> x.ndim
1

>>> x = np.array([[1, 3, 4, 5, 3],
                    [2, 3, 5, 6, 43],
                    [24,3, 45, 67, 4]])
>>> x.ndim
2

>>> x = np.array([[[5, 78, 2, 34, 0],
                    [6, 79, 3, 35, 1],
                    [7, 80, 4, 36, 2]],
                    [[5, 78, 2, 34, 0],
                    [6, 79, 3, 35, 1],
                    [7, 80, 4, 36, 2]],
                    [[5, 78, 2, 34, 0],
                    [6, 79, 3, 35, 1],
                    [7, 80, 4, 36, 2]]])
>>> x.ndim
3


from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
>>> print(train_images.shape)
(60000, 28, 28)
>>> print(train_images.dtype)
uint8


digit = train_images[4]


import matplotlib.pyplot as plt
plt.imshow(digit, cmap=plt.cm.binary)
plt.show()


>>> my_slice = train_images[10:100]
>>> print(my_slice.shape)
(90, 28, 28)

>>> my_slice = train_images[10:100, :, :]
>>> print(my_slice.shape)
(90, 28, 28)
>>> my_slice = train_images[10:100, 0:28, 0:28]
>>> print(my_slice.shape)
(90, 28, 28)
>>> my_slice = train_images[:, 14:, 14:]
>>> print(my_slice.shape)
(60000, 14, 14)
>>> my_slice = train_images[:, 7:-7, 7:-7]
>>> print(my_slice.shape)
(60000, 14, 14)


batch = train_images[:128]
batch = train_images[128:256]
batch = train_images[128 * n:128 * (n + 1)]


>>> x = np.array([[0., 5.],
                [3., 6.],
                [4., 7.]])
>>> print(x.shape)
(3, 2)
>>> x = x.reshape((6, 1))
>>> x
array([[0.],
        [5.],
        [3.],
        [6.],
        [4.],
        [7.]])
>>> x = x.reshape((2, 3))
>>> x
array([[0., 5., 3.],
        [6., 4., 7.]])
>>> x = np.zeros((300, 20))
>>> x = np.transpose(x)
>>> print(x.shape)
(20, 300)




猜你喜欢

转载自blog.csdn.net/qq_25527791/article/details/90208090
101