Skip to content
Advertisement

Create an array of repeating values with numpy

  • Given the following array, where the elements in the array are a value at index [0], and its frequency at index [1].
import numpy as np

a = np.array([[767, 5], [770, 5], [772, 7], [779, 7], [781, 5], [782, 7], [787, 5]])

# values
v = a[:, 0]

array([767, 770, 772, 779, 781, 782, 787])

# frequencies
f = a[:, 1]

array([5, 5, 7, 7, 5, 7, 5])
  • I need an array that is the length of the sum of the frequencies, filled with v, based on their respective frequency.
[767, 767, 767, 767, 767, 770, 770, 770, 770, 770, 772, 772, 772, 772, 772, 772, 772, 779, 779, 779, 779, 779, 779, 779, 781, 781, 781, 781, 781, 782, 782, 782, 782, 782, 782, 782, 787, 787, 787, 787, 787]
  • This can be done with
# a for-loop
ix = list()

for (x, y) in a:
    l = [x] * y
    ix.extend(l)

# a list-comprehension
ix = [v for (x, y) in a for v in [x]*y]
  • How can I do this with a vectorized numpy method?

    • No for-loops
    • No pandas
  • I thought of creating an array of zeros, whose length is the sum of frequencies, but I’m not certain how to fill it.

za = np.zeros(sum(f))

Advertisement

Answer

Use numpy.repeat:

np.repeat(a[:, 0], a[:, 1])
# np.repeat(*a.T) # In case its (n, 2) shaped

Output:

array([767, 767, 767, 767, 767, 770, 770, 770, 770, 770, 772, 772, 772,
       772, 772, 772, 772, 779, 779, 779, 779, 779, 779, 779, 781, 781,
       781, 781, 781, 782, 782, 782, 782, 782, 782, 782, 787, 787, 787,
       787, 787])
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement