Converting the decimal range to a Numpy array, with each bit being an element of the array

I created a small function that takes an integer as the value lengthand returns a numpy arraybinary representation of all the 2**lengthintegers in the range [0:2**length-1].

import numpy as np

def get_bitstrings(length):
  # We need to binary-fy 2^length numbers.
  iterations = 2**length
  # Pre-allocate memory.
  the_array = np.zeros((iterations, length))
  # Go through all decimals in the range [0:iterations-1]
  for num in range(iterations):
    # Get binary representation in string format with 'length' zeroes padded
    t_string = '{f_num:0{f_width}b}'.format(f_num=num, f_width=length)
    # Convert to a Python list
    t_list   = list(t_string)
    # Convert to Numpy array and store.
    the_array[num,:] = np.array(t_list)

  return the_array

if __name__ == '__main__':
  var1 = get_bitstrings(2)
  var2 = get_bitstrings(3)
  print('var1:\n{}\n'.format(var1))
  print('var2:\n{}\n'.format(var2))

which gives:

var1:
[[ 0.  0.]
 [ 0.  1.]
 [ 1.  0.]
 [ 1.  1.]]

var2:
[[ 0.  0.  0.]
 [ 0.  0.  1.]
 [ 0.  1.  0.]
 [ 0.  1.  1.]
 [ 1.  0.  0.]
 [ 1.  0.  1.]
 [ 1.  1.  0.]
 [ 1.  1.  1.]]

This process includes obtaining a binary representation of each integer as a string (with 0s filled so that the length is constant in length), converting the string to a Python list, and converting the list to numpy array.

, , , .. bitstring 1010 1x4 numpy array, 1x1. , , .

, , , . , , Python/Numpy.

: MATLAB :

t_length = 5; dc = [0: 2 ^ t_length-1] '; bc = rem (floor (dc * pow2 (- (t_length-1): 0)), 2);

noob, Python/Numpy! , - . :-)

+3
2

NumPy, :

>>> from numpy import arange, newaxis
>>> powers_of_two = 2**arange(4)[::-1]
>>> (arange(2**4)[:, newaxis] & powers_of_two) / powers_of_two
array([[0, 0, 0, 0],
       [0, 0, 0, 1],
       [0, 0, 1, 0],
       [0, 0, 1, 1],
       [0, 1, 0, 0],
       [0, 1, 0, 1],
       [0, 1, 1, 0],
       [0, 1, 1, 1],
       [1, 0, 0, 0],
       [1, 0, 0, 1],
       [1, 0, 1, 0],
       [1, 0, 1, 1],
       [1, 1, 0, 0],
       [1, 1, 0, 1],
       [1, 1, 1, 0],
       [1, 1, 1, 1]])

: 0 15 (arange(2**4)), , (16, 1) ( [:, newaxis]). , (2**arange(4)[::-1]). , "" : arange powers_of_two. NumPy broadcasting slicing , for Python , for .

, , , :

>>> from numpy import arange, newaxis
>>> arange(2**4)[:,newaxis] >> arange(4)[::-1] & 1
array([[0, 0, 0, 0],
       [0, 0, 0, 1],
       [0, 0, 1, 0],
       [0, 0, 1, 1],
       [0, 1, 0, 0],
       [0, 1, 0, 1],
       [0, 1, 1, 0],
       [0, 1, 1, 1],
       [1, 0, 0, 0],
       [1, 0, 0, 1],
       [1, 0, 1, 0],
       [1, 0, 1, 1],
       [1, 1, 0, 0],
       [1, 1, 0, 1],
       [1, 1, 1, 0],
       [1, 1, 1, 1]])

, , , Python timeit profile. length=16 , :

taniyama:~ mdickinson$ python -m timeit -s "from numpy import arange, newaxis" "arange(1<<16)[:, newaxis] >> arange(16)[::-1] & 1"
100 loops, best of 3: 4.08 msec per loop
taniyama:~ mdickinson$ python -m timeit -s "from numpy import arange, newaxis" "(arange(1<<16)[:, newaxis] & 2**arange(16)[::-1]) / 2**arange(16)[::-1]"
10 loops, best of 3: 21.6 msec per loop
+4

- numpy.binary_repr. , int float ( dtype). :

import numpy as np

k = 4
print np.array([list(np.binary_repr(x, k)) for x in range(2**k)], dtype=int)

:

[[0 0 0 0]
 [0 0 0 1]
 [0 0 1 0]
 [0 0 1 1]
 [0 1 0 0]
 [0 1 0 1]
 [0 1 1 0]
 [0 1 1 1]
 [1 0 0 0]
 [1 0 0 1]
 [1 0 1 0]
 [1 0 1 1]
 [1 1 0 0]
 [1 1 0 1]
 [1 1 1 0]
 [1 1 1 1]]

, :

def bitstrings(k):
    binary = [np.binary_repr(item, width=k) for item in range(2**k)]
    return np.array([list(item) for item in binary], dtype=int)
+1

All Articles