2

I have an array with size (4,4) that can have values 0 and 1, so I can have 65536 different arrays. I need to produce all these arrays without repeating. I use wt_random=np.random.randint(2, size=(65536,4,4)) but I am worried they are not unique. could you please tell me this code is correct or not and what should I do to produce all possible arrays? Thank you.

3
  • 3
    If you need unique values I would suggest that you create your data structure and fill it with increasing integers. Thereafter, start shuffling the values randomly. Commented Jul 10, 2019 at 11:17
  • 1
    what does it mean? I need to produce 4x4 arrays in python with just 0 or 1. what does increasing integers mean? I have to fill it with 0 or 1 not other numbers. I want to know is it any way to produce these 65536 unique array? Commented Jul 10, 2019 at 11:20
  • Question has nothing to do with keras - kindly do not spam irrelevant tags (removed). Commented Jul 10, 2019 at 13:25

4 Answers 4

1

you could use numpy.meshgrid

output = np.array(np.meshgrid(*[[0,1] for _ in range(16)])).T.reshape(-1, 4,4)

as you need to get all the possible values (number of possible values = 2 ^16 = 65536)

Sign up to request clarification or add additional context in comments.

4 Comments

your suggestion is true too. Thank you.
is it any way to select both of proposed code as the answer?
is it any way to shuffle this final array? for example the first 4x4 array is all 0 and the last one is all 1s. is it any way to reorder them?
No don't worry @david I'm glad I could be of help. you could do np.random.shuffle(output)
1

you can use itertools.product with repeat=16 to generate all patterns, then just reshape them to (4,4).

try this:

import numpy as np
from itertools import product

wt_random = np.array([np.array(p).reshape((4, 4)) for p in product((0, 1), repeat=16)])
np.random.shuffle(wt_random)

print(wt_random.shape)
print(wt_random[1234])

Output: (shows the shape is correct, and an example element)

(65536, 4, 4)
[[0 0 0 0]
 [0 1 0 0]
 [1 1 0 1]
 [0 0 1 0]]

2 Comments

is it any way to shuffle this final array?
@david sure, I edited my answer docs.scipy.org/doc/numpy/reference/generated/…
0

You can loop through n from 1 to 65535, then have the binary equivalent mapped to your array that will be easiest and 100% truly unique and all possibilities included.

Eg for m1->0 : [ [0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0] ] for m2->1 : [ [0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,1] ]

     ----------------------------------------------------
     -----------------------------------------------   upto n

for m65536->65535 : [ [1,1,1,1], [1,1,1,1], [1,1,1,1], [1,1,1,1] ]

2 Comments

this is time-consuming:( is it any way to produce all of them using the built-in function in python?
What I would suggest you is use bin() method to convert to binary for each iteration and then can use for building array. Also if there isn't a need to produce all the arrays at once you can write a function which will return an array of particular index. Between 0-65535
0

If you need all possible arrays in random order, consider enumerating them in any arbitrary deterministic order and then shuffling them to randomize the order. If you don't want all arrays in memory, you could write a function to generate the array at a given position in the deterministic list, then shuffle the positions. Note that Fisher-Yates may not even need a dense representation of the list to shuffle... if you keep track of where the already shuffled entries end up you should have enough.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.