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 7029661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:34:06+00:00 2026-05-28T00:34:06+00:00

I would like to create a function that returns a numpy array if one

  • 0

I would like to create a function that returns a numpy array if one is given, or a multi-dimensional numpy array if that is given. For example:

import numpy as np;
def running_average(data,windowSize):
    dShape = np.shape(data);
    if(len(dShape)==1):
        raOut = np.zeros(len(data));
        rSum = 0.0;
        for row,value in enumerate(data):
            if row<windowSize:
                rSum+=float(value);
            else:
                rSum=rSum-data[row-windowSize]+value;
            raOut[row]=rSum/windowSize;
    else:
        raOut = np.zeros(dShape);
        for col in xrange(dShape[1]):
            rSum=0.0;
            for row,value in enumerate(data[:,col]):
                if row<windowSize:
                    rSum+=float(value);
                else:
                    rSum=rSum-data[row-windowSize,col]+value;
                raOut[row,col]=rSum/windowSize;

    return raOut;

But there must be a good test to do so I don’t have to essentially repeat myself in the if and the else statement.

I am newer to python, what is the prefferred method?

  • 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-28T00:34:07+00:00Added an answer on May 28, 2026 at 12:34 am

    How about something like:

    def running_avg(data, ws):
        tmp = np.cumsum(data, axis=-1, dtype='float')
        ra = (tmp[..., ws:] - tmp[..., :-ws]) / ws
        return ra
    

    This will take the average on the last axis, if you wanted to get really clever you could have the function take an axis argument and take the average on an arbitrary axis.

    UPDATE

    I believe this version is consistent with your code above.

    def running_avg(data, ws):
        ra = np.cumsum(data, axis=-1, dtype='float') / ws
        ra[..., ws:] = ra[..., ws:] - ra[..., :-ws]
        return ra
    

    For your more general question, using numpys builtin functions, such as cumsum helps because they already do that, but if you do have to loop you can use A = np.zeros(A.shape) to get an array the same shape as the input and then use A[…, i] to always operate on the last dimension or A[…, i, :] to always operate on the second to last dimension and so on. Also sometimes people do data = np.roll(data, axis) to move axis to the beginning then you use A[i] to operative on the first dimension and move the axis back if you need to.

    UPDATE 2:

    I just remembered why the following is a very bad idea (at least in this case):

    ra[..., ws:] -= ra[..., :-ws]
    

    You should use this instead:

    ra[..., ws:] = ra[..., ws:] - ra[..., :-ws]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So basically I would like to create a function that when alerted, returns the
I would like to create a fast lightweight function in C language that returns
I would like to create a function that returns a vector of numbers a
In Bash, I would like to create a function that returns the filename of
I would like to create a function (call it fcreate) that when given a
I would like to create a function that returns a concatinated string of a
I would like to create a function that inserts a record and returns its
I would like to create a function that will return list of type that
I would like to create a function that checks if a numeric value passed
I would like to create an auto increment function that allows me to maintain

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.