[英]how can I make an array that each element of is a random image?
I want to make an array in python that has about 40000 elements and each element is a random image with size 28x28. 我想在python中制作一个数组,其中包含约40000个元素,每个元素都是大小为28x28的随机图像。 I use this code but it produces the following error.
我使用此代码,但是会产生以下错误。 I am a beginner in python.
我是python的初学者。
wtrain=np.zeros((40000,28,28,1))
for i in range(40000):
w_main = np.random.randint(2,size=(1,4,4,1))
w_main=w_main.astype(np.float32)
w_expand=np.zeros((1,28,28,1),dtype='float32')
w_expand[:,0:4,0:4]=w_main
w_expand.reshape(1,28,28,1)
wtrain[i,:,:,:]=w_expand
the error 错误
All input arrays (x) should have the same number of samples.
所有输入数组(x)都应具有相同数量的样本。 Got array shapes: [(49999, 28, 28, 1), (1, 28, 28, 1)]
得到的数组形状:[(49999,28,28,1),(1,28,28,1)]
what is the problem? 问题是什么? how can I add these random images to wtrain?
如何将这些随机图像添加到训练中? Thanks
谢谢
I change my code to this: 我将代码更改为此:
wtrain=[]
for i in range(2):
w_main = np.random.randint(2,size=(1,4,4,1))
w_main=w_main.astype(np.float32)
w_expand=np.zeros((1,28,28,1),dtype='float32')
w_expand[:,0:4,0:4]=w_main
w_expand.reshape(1,28,28,1)
wtrain.append(w_expand)
Make a zeros
array of the required shape - I used (3,7,7) instead of your (40000,28,28). 制作所需形状的
zeros
数组-我用(3,7,7)代替了(40000,28,28)。
a = np.zeros((3,7,7))
Make an array of random numbers with a shape of your spec - the first dimension size is the same as the zeros
array 制作一个形状符合您要求的随机数数组-第一维尺寸与
zeros
数组相同
b = np.random.randint(0,255, size=(3,4,4))
Assign the random values to the zeros array using indexing on the left-hand-side of the assignment to put those values where you want 使用赋值左侧的索引将随机值分配给zeros数组,以将这些值放在所需的位置
a[:,:4,:4] = b
Result 结果
In [20]: a[0,...]
Out[20]:
array([[ 241., 228., 176., 194., 0., 0., 0.],
[ 185., 240., 219., 175., 0., 0., 0.],
[ 206., 82., 32., 137., 0., 0., 0.],
[ 58., 181., 242., 168., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.]])
In [21]: a[1,...]
Out[21]:
array([[ 25., 19., 251., 89., 0., 0., 0.],
[ 204., 25., 72., 176., 0., 0., 0.],
[ 189., 37., 33., 49., 0., 0., 0.],
[ 72., 168., 68., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.]])
In [22]: a[2,...]
Out[22]:
array([[ 74., 228., 186., 133., 0., 0., 0.],
[ 147., 83., 194., 205., 0., 0., 0.],
[ 34., 185., 21., 6., 0., 0., 0.],
[ 14., 245., 46., 154., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.]])
Add another dimension by reshaping 通过重塑添加另一个尺寸
In [26]: x,y,z = a.shape
In [27]: c = a.reshape((x,y,z,1))
In [28]: c.shape
Out[28]: (3, 7, 7, 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.