Home > Enterprise >  Add fixed sequence to each row of a numpy array
Add fixed sequence to each row of a numpy array

Time:10-28

I have the following cycle:

comprehensive_merged_function = np.zeros(shape=(0, 1))

for i in range(256):
    comprehensive_merged_function = np.append(comprehensive_merged_function, utils.dec_to_bin(i, 8))
    
comprehensive_merged_function = np.array([list(s) for s in comprehensive_merged_function]).astype(int)

which is giving me the following array:

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

I would to like to append each row (out of the 256) with 8 times (-1), to obtain the following desired result:

    [[0 0 0 0 0 0 0 0 -1 -1 -1 -1 -1 -1 -1 -1]
     [0 0 0 0 0 0 0 1 -1 -1 -1 -1 -1 -1 -1 -1]
     [0 0 0 0 0 0 1 0 -1 -1 -1 -1 -1 -1 -1 -1]
     ...
     [1 1 1 1 1 1 0 1 -1 -1 -1 -1 -1 -1 -1 -1]
     [1 1 1 1 1 1 1 0 -1 -1 -1 -1 -1 -1 -1 -1]
     [1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1]]

I tried the following:

comprehensive_merged_function = np.zeros(shape=(0, 1))
appending_indexes = np.array([-1, -1, -1, -1, -1, -1, -1, -1])

for i in range(256):
    comprehensive_merged_function = np.append(comprehensive_merged_function, utils.dec_to_bin(i, 8))
    comprehensive_merged_function = np.row_stack(comprehensive_merged_function, appending_indexes)
    
comprehensive_merged_function = np.array([list(s) for s in comprehensive_merged_function]).astype(int)

and I received the following error:

Traceback (most recent call last):
  File "compression comprehensive testing.py", line 262, in <module>
    comprehensive_merged_function = np.row_stack(comprehensive_merged_function, appending_indexes)
  File "<__array_function__ internals>", line 4, in vstack
TypeError: _vhstack_dispatcher() takes 1 positional argument but 2 were given

Of course I tried many other different ways to do this, but the results were worse and I haven`t presenthed them here to avoid overloading the post.

Any help of how can I get the desired result will be greatly appreciated.

CodePudding user response:

You can use np.hstack(arrays) to combine two arrays. For example:

import numpy as np

a = np.arange(25).reshape(5, 5)
b = np.zeros((5, 4), dtype=int) - 1

np.hstack([a, b])

Which results in:

array([[ 0,  1,  2,  3,  4, -1, -1, -1, -1],
       [ 5,  6,  7,  8,  9, -1, -1, -1, -1],
       [10, 11, 12, 13, 14, -1, -1, -1, -1],
       [15, 16, 17, 18, 19, -1, -1, -1, -1],
       [20, 21, 22, 23, 24, -1, -1, -1, -1]])

You need the dtype=int because np.zeros() gives floats by default and combining the arrays will cast everything to floats.

In your case, you could do this:

rows, cols = comprehensive_merged_function.shape
minus_ones = np.zeros((rows, 8), dtype=int) - 1
arr = np.hstack([comprehensive_merged_function, minus_ones])

CodePudding user response:

EDIT - just changed some things, so your code could work. Copy this and it should work

just do add this last line to your code

comprehensive_merged_function = np.zeros(shape=(0, 1), dtype=int)

for i in range(256):
    comprehensive_merged_function = np.append(comprehensive_merged_function, utils.dec_to_bin(i, 8))
    
comprehensive_merged_function = np.c_[comprehensive_merged_function,np.full((256,8), -1)]

  • Related