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].
JavaScript
  • I need an array that is the length of the sum of the frequencies, filled with v, based on their respective frequency.
JavaScript
  • This can be done with
JavaScript
  • 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.

JavaScript

Advertisement

Answer

Use numpy.repeat:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement