Using numpy repetition simultaneously on arrays with clear multiplicities but with the same size

I have two trival arrays with the same length, tmp_reds and tmp_blues:

npts = 4
tmp_reds = np.array(['red', 'red', 'red', 'red'])
tmp_blues = np.array(['blue', 'blue', 'blue', 'blue'])

I use np.repeat to create plurality:

red_occupations = [1, 0, 1, 2]
blue_occupations = [0, 2, 0, 1]

x = np.repeat(tmp_reds, red_occupations)
y = np.repeat(tmp_blues, blue_occupations)

print(x)
['red' 'red' 'red' 'red']

print(y)
['blue' 'blue' 'blue']

What I want is the following composite element x and y:

desired_array = np.array(['red', 'blue', 'blue', 'red', 'red', 'red', 'blue'])

So request_array is defined as follows:

(1) Multiplicity from the first red_occupations element is applied

(2) Multiplicity from the first blue_occupations element is applied

(3) Multiplicity from the second red_occupations element is applied

(4) Multiplicity from the second element blue_occupations is applied

...

(2 * npts-1) Many of the npts of the red_occupations element apply

(2 * npts) Multiplicity from the npts blue_occupations element is applied

, np.repeat. np.repeat , . - - , , - , np.repeat?

wish_array numpy, zipped for . npts ~ 1e7, .

+4
1

-

# Two 1D color arrays
tmp1 = np.array(['red', 'red', 'red', 'green'])
tmp2 = np.array(['white', 'black', 'blue', 'blue'])

# Multiplicity arrays
color1_occupations = [1, 0, 1, 2]
color2_occupations = [0, 2, 0, 1]

# Stack those two color arrays and two multiplicity arrays separately
tmp12 = np.column_stack((tmp1,tmp2))
color_occupations = np.column_stack((color1_occupations,color2_occupations))

# Use np.repeat to get stacked multiplicities for stacked color arrays
out = np.repeat(tmp12,color_occupations.ravel())

-

In [180]: out
Out[180]: 
array(['red', 'black', 'black', 'red', 'green', 'green', 'blue'], 
      dtype='|S5')
+1

Source: https://habr.com/ru/post/1607645/


All Articles