Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3438752
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:13:32+00:00 2026-05-18T08:13:32+00:00

OK, I think this will be fairly simple, but my numpy-fu is not quite

  • 0

OK, I think this will be fairly simple, but my numpy-fu is not quite strong enough. I’ve got a an array A of ints; it’s tiled N times. I want a running count of the number of times each element is used.

For example, the following (I’ve reshaped the array to make the repetition obvious):

[0, 1, 2, 0, 0, 1, 0] \
[0, 1, 2, 0, 0, 1, 0] ...

would become:

[0, 0, 0, 1, 2, 1, 3] \
[4, 2, 1, 5, 6, 3, 7]

This python code does it, albeit inelegantly and slowly:

def running_counts(ar):
    from collections import defaultdict
    counts = defaultdict(lambda: 0)
    def get_count(num):
        c = counts[num]
        counts[num] += 1
        return c
    return [get_count(num) for num in ar]

I can almost see a numpy trick to make this go, but not quite.

Update

Ok, I’ve made improvements, but still rely on the above running_counts method. The following speeds things up and feels right-track-ish to me:

def sample_counts(ar, repititions):
    tile_bins = np.histogram(ar, np.max(ar)+1)[0]
    tile_mult = tile_bins[ar]
    first_steps = running_counts(ar)
    tiled = np.tile(tile_mult, repititions).reshape(repititions, -1)
    multiplier = np.reshape(np.arange(repititions), (repititions, 1))
    tiled *= multiplier
    tiled += first_steps
    return tiled.ravel()

Any elegant thoughts to get rid of running_counts()? Speed is now OK; it just feels a little inelegant.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:13:33+00:00Added an answer on May 18, 2026 at 8:13 am

    Here’s my take on it:

    def countify2(ar):
        ar2 = np.ravel(ar)
        ar3 = np.empty(ar2.shape, dtype=np.int32)
        uniques = np.unique(ar2)
        myarange = np.arange(ar2.shape[0])
        for u in uniques:
            ar3[ar2 == u] = myarange
        return ar3
    

    This method is most effective when there are many more elements than there are unique elements.

    Yes, it is similar to Sven’s, but I really did write it up long before he posted. I just had to run somewhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this is a simple question, but I can not find the answer
I think this is fairly simple, but I'm new to Django and I can't
I think this has to be a very simple question but I am not
Do you think that C# will support something like ??= operator? Instead of this:
I think this is likely to be a generic .NET assembly loading question, but
I have this fairly simple function to compute the mean of elements of a
The task seems fairly simple but I don't know what's the best way to
I have this formula in a function below. It's a fairly simple concept, yet
I know this type of question has been asked before, but I could not
I have some fairly simple state needs (for now). I think I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.