 # generate an array of bit vectors with no repeated columns

• Last Update :
• Techknowledgy :

Observe the binary representation of the numbers from 0 to 7:

000
001
010
011
100
101
110
111

Each row is different! So, all we have to do is convert each row to column. e.g.

arr = [
[0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 1, 1, 0, 0, 1, 1],
[0, 1, 0, 1, 0, 1, 0, 1],
]

You can do something like this:

rows = 3
cols = 8

if 2 ** rows < cols:
print('Not possible')

arr = [
[None] * cols
for _ in range(rows)
]

for col_idx in range(cols):
binary = bin(col_idx)[2: ]
binary = binary.zfill(rows)

for row_idx in range(rows):
arr[row_idx][col_idx] = int(binary[row_idx])

for row in arr:
print(row)

Yours has an issue with this line:

    for _ in range(num_of_one):
train_data[k][np.random.randint(0, input_dim)] = 1

You can achieve this via the magic of binary counting. Each of these columns are a different numbers binary representation. There are some limitations to this, as you would with any solution, where it's impossible to have all unique columns.

d = np.arange(input_dim)
random.shuffle(d)
train_data = (((d[: , None] & (1 << np.arange(batch)))) > 0).astype(float).T
print(train_data)

Your best bet is something like np.unpackbits combined with python's random.sample. random.sample will sample without replacement without creating a list of the input. This means that you can use a range object over arbitrarily large integers without any risk of problems, as long as the sample size fits in memory. np.unpackbits then converts the integers into unique bit sequences. This idea is a concrete implementation of @ScottHunter's answer.

batch_size = number_of_bits
input_size = number_of_samples

First, decide how many bytes you'll need to generate, and the max integer that you'll need to cover the range. Remember, Python supports arbitrary precision integers, so go crazy:

bytes_size = np.ceil(batch_size / 8)
max_int = 1 << batch_size

samples = random.sample(range(max_int), input_size)

Now unpack and you're good to go:

result = np.unpackbits(data, axis = 0)[: batch,: ]

Putting it all together into a single package:

def random_bit_columns(batch_size, input_size):
samples = random.sample(range(1 << batch_size), input_size)
data = np.array([list(x.to_bytes(np.ceil(batch_size / 8), 'little')) for x in samples], dtype = np.uint8).T
result = np.unpackbits(data, axis = 0)[: batch,: ]
return result

Suggestion : 2

Create a matrix and copy its columns into a new array, repeating the first column twice and second column three times.,Create a vector and repeat each of its elements three times into a new vector.,Create a matrix and repeat each element into a 3-by-2 block of a new matrix.,MathWorks is the leading developer of mathematical computing software for engineers and scientists.

v = [1 2 3 4];
u = repelem(v, 3)
u = 1× 12

1 1 1 2 2 2 3 3 3 4 4 4
u = repelem(v, [2 2 3 3])
u = 1× 10

1 1 2 2 3 3 3 4 4 4
A = [1 2;3 4]
A = 2× 2

1 2
3 4

Suggestion : 3

Arrays are multidimensional objects, with matrices being the special case of two-dimensional arrays. ,This matrix could also be created using the array function. The following two-dimensional array is identical to the matrix that we just created (it even has class matrix):,The vector variables that we have looked at so far are one-dimensional objects, since they have length but no other dimensions. Arrays hold multidimensional rectangular data. “Rectangular” means that each row is the same length, and likewise for each column and other dimensions. Matrices are a special case of two-dimensional arrays., You can access slices of a vector by passing an index into square brackets. The rep function creates a vector with repeated elements.

8.5: 4.5 #sequence of numbers from 8.5 down to 4.5
8.5:4.5                #sequence of numbers from 8.5 down to 4.5
# # 8.5 7.5 6.5 5.5 4.5
c(1, 1:3, c(5, 8), 13) #values concatenated into single vector
# # 1 1 2 3 5 8 13
vector("complex", 5)
# # 0 + 0 i 0 + 0 i 0 + 0 i 0 + 0 i 0 + 0 i
vector("logical", 5)
# # FALSE FALSE FALSE FALSE FALSE
seq.int(3, 12) #same as 3: 12
seq.int(3, 12)     #same as 3:12
# # 3 4 5 6 7 8 9 10 11 12
seq.int(3, 12, 2)
# # 3 5 7 9 11
n <- 0
1:n        #not what you might expect!
# # 1 0
seq_len(n)
# # integer(0)
length(1: 5)
length(1:5)
# # 5
length(c(TRUE, FALSE, NA))
# # 3
nchar(sn)
# # 6 5 6 5
poincare <- c(1, 0, 0, 0, 2, 0, 2, 0)  #See http://oeis.org/A051629
length(poincare) <- 3
poincare
# # 1 0 0
c(apple = 1, banana = 2, "kiwi fruit" = 3, 4)
c(apple = 1, banana = 2, "kiwi fruit" = 3, 4)
# # apple banana kiwi fruit
# # 1 2 3 4
x <- 1:4
names(x) <- c("apple", "bananas", "kiwi fruit", "")
x
# # apple bananas kiwi fruit
# # 1 2 3 4
names(1:4)
# # NULL
x < -(1: 5) ^ 2
x <- (1:5) ^ 2
# # 1 4 9 16 25
x[c(TRUE, FALSE, TRUE, FALSE, TRUE)]
# # 1 9 25
x[c(1, -1)]      #This doesn't make sense!
# # Error: only 0 's may be mixed with negative subscripts
x[c(1, NA, 5)]
## one <NA> twenty five
## 1 NA 25
1: 5 + 1
1:5 + 1
# # 2 3 4 5 6
1 + 1:5
# # 2 3 4 5 6
1:5 + 1:7
# # Warning: longer object length is not a multiple of shorter object length
## Warning: longer object length is not a multiple of shorter object length
# # 2 4 6 8 10 7 9

Suggestion : 4

Last Updated : 08 Jul, 2022

Examples:

Input: arr[] = {
1,
5,
1,
10,
12,
10
}
Output: 1 10
1 and 10 appear more than once in given
array.

Input: arr[] = {
50,
40,
50
}
Output: 50

1 10

1 10

Suggestion : 5

length.out may be given in place of times, in which case x is repeated as many times as is necessary to create a vector of this length. If both are given, length.out takes priority and times is ignored. ,. Normally just one of the additional arguments is specified, but if each is specified with either of the other two, its replication is performed first, and then that implied by times or length.out. ,If times consists of a single integer, the result consists of the whole input repeated this many times. If times is a vector of the same length as x (after replication by each), the result consists of x repeated times times, x repeated times times and so on. ,an integer-valued vector giving the (non-negative) number of times to repeat each element if of length length(x), or to repeat the whole vector if of length 1. Negative or NA values are an error. A double vector is accepted, other inputs being coerced to an integer or double vector.

### Usage

rep(x, ...)

rep.int(x, times)

rep_len(x, length.out)

The default behaviour is as if the call was

  rep(x, times = 1, length.out = NA, each = 1)

### Examples

rep(1: 4, 2)
rep(1: 4, each = 2) # not the same.
rep(1: 4, c(2, 2, 2, 2)) # same as second.
rep(1: 4, c(2, 1, 2, 1))
rep(1: 4, each = 2, length.out = 4) # first 4 only.
rep(1: 4, each = 2, length.out = 10) # 8 integers plus two recycled 1 's.
rep(1: 4, each = 2, times = 3) # length 24, 3 complete replications

rep(1, 40 * (1 - .8)) # length 7 on most platforms
rep(1, 40 * (1 - .8) + 1e-7) # better

# # replicate a list
fred < -list(happy = 1: 10, name = "squash")
rep(fred, 5)

# date - time objects
x < -.leap.seconds[1: 3]
rep(x, 2)
rep(as.POSIXlt(x), rep(2, 3))

# # named factor
x < -factor(LETTERS[1: 4]);
names(x) < -letters[1: 4]
x
rep(x, 2)
rep(x, each = 2)
rep.int(x, 2) # no names
rep_len(x, 10)