简体   繁体   English

如何制作一个数组,其中每个元素都是随机图像?

[英]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.

相关问题 我如何才能使二维数组的每个元素成为python中的对象? - How can i make each element of a two dimensional array an object in python? 如何切片 numpy 字符串数组的每个元素? - How can I slice each element of a numpy array of strings? 如何将数组的每个元素放入一行而不是 6 - how can I each element of an array into one line instead of 6 如何将 numpy 数组拆分为 python 中的每个元素? 每个数组的最后一个元素将是另一个数组的第一个元素 - How can I split a numpy array into equal elements each in python? The last element of each array will be the first element of the other array 我怎样才能使这个随机6位数 - How can i make this for random 6 digit number 如何让乌龟变成随机颜色? - How can I make the turtle a random color? 将数组的每个元素乘以随机数 - Multiply each element of array by random number 如何制作一个列表或一个字典,每个元素都是一个类实例? - How can I make a list or a dictionary with each element being a class instance? 如何制作一个新列表,该列表具有每个列表的第一个元素但分为 3 个? - How can I make a new list that has the first element of each of the list but split into 3? 如何让我的列表只返回列表中的每个元素一次? - How can I make my list return each element only once in a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM