inbox

复制图片到剪贴板

import addcopyfighandler # 然后 plt.show () 之后按 ^C

NumPy

reshape

v = np.array([1, 2, 4, 5])
print(v.reshape(2, 2))
print(np.reshape(v, (2, 2)))
[[1 2]
 [4 5]]
[[1 2]
 [4 5]]

matplotlib

pyplot

from matplotlib import pyplot as plt

绘制多个图

3 行 2 列,从位置 1 绘制到位置 n

plt.subplot(320 + 1 + i)

例子:

# example of loading the mnist dataset
from tensorflow.keras.datasets import mnist
from matplotlib import pyplot as plt
# load dataset
(trainX, trainy), (testX, testy) = mnist.load_data()
# summarize loaded dataset
print('Train: X=%s, y=%s' % (trainX.shape, trainy.shape))
print('Test: X=%s, y=%s' % (testX.shape, testy.shape))
# plot first few images
for i in range(6):
	# define subplot
	plt.subplot(320 + 1 + i)
	# plot raw pixel data
	plt.imshow(trainX[i], cmap=plt.get_cmap('gray'))
# show the figure
plt.show()

image_up_16397426440b4b21fc.jpg

tensorflow.keras

to_categorical

from tensorflow.keras.utils import to_categorical
my_vec = [1, 2, 3, 4]
print(my_vec)
print(to_categorical(my_vec))

效果

[1, 2, 3, 4]
[[0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]