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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:23:17+00:00 2026-06-11T21:23:17+00:00

I am doing SPC analysis using numpy / pandas . Part of this is

  • 0

I am doing SPC analysis using numpy/pandas.

Part of this is checking data series against the Nelson rules and the Western Electric rules.

For instance (rule 2 from the Nelson rules): Check if nine (or more) points in a row are on the same side of the mean.

Now I could simply implement checking a rule like this by iterating over the array.

  • But before I do that, I’m checking here on SO if numpy/pandas has a way to do this without iteration?
  • In any case: What is the "numpy-ic" way to implement a check like the one described above?
  • 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-06-11T21:23:19+00:00Added an answer on June 11, 2026 at 9:23 pm

    As I mentioned in a comment, you may want to try using some stride tricks.

    • First, let’s make an array of the size of your anomalies: we can put it as np.int8 to save some space

      anomalies = x - x.mean()
      signs = np.sign(anomalies).astype(np.int8)
      
    • Now for the strides. If you want to consider N consecutive points, you’ll use

      from np.lib.stride_tricks import as_strided
      strided = as_strided(signs, 
                           strides=(signs.itemsize,signs.itemsize), 
                           shape=(signs.shape,N))
      

      That gives us a (x.size, N) rollin array: the first row is x[0:N], the second x[1:N+1]… Of course, the last N-1 rows will be meaningless, so from now on we’ll use

      strided = strided[:-N+1]
      
    • Let’s sum along the rows

      consecutives = strided.sum(axis=-1)
      

      That gives us an array of size (x.size-N+1) of values between -N and +N: we just have to find where the absolute values are N:

      (indices,) = np.nonzero(consecutives == N)
      

      indices is the array of the indices i of your array x for which the values x[i:i+N] are on the same side of the mean…

    Example with x=np.random.rand(10) and N=3

    >>> x = array([ 0.57016436,  0.79360943,  0.89535982,  0.83632245,  0.31046202,
                0.91398363,  0.62358298,  0.72148491,  0.99311681,  0.94852957])
    >>> signs = np.sign(x-x.mean()).astype(np.int8)
    array([-1,  1,  1,  1, -1,  1, -1, -1,  1,  1], dtype=int8)
    >>> strided = as_strided(signs,strides=(1,1),shape=(signs.size,3))
    array([[  -1,    1,    1],
           [   1,    1,    1],
           [   1,    1,   -1],
           [   1,   -1,    1],
           [  -1,    1,   -1],
           [   1,   -1,   -1],
           [  -1,   -1,    1],
           [  -1,    1,    1],
           [   1,    1, -106],
           [   1, -106,  -44]], dtype=int8)
    >>> consecutive=strided[:-N+1].sum(axis=-1)
    array([ 1,  3,  1,  1, -1, -1, -1,  1])
    >>> np.nonzero(np.abs(consecutive)==N)
    (array([1]),)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Doing the getting started of Sinatra. I get this error: ./sinatra.rb:5: undefined method `get'
Doing an ajax get request works as expected using the following code: $.ajax({ type:
Doing code analysis of the project and get the message Reference-counted object is used
Doing something like this: from zipfile import ZipFile #open zip file zipfile = ZipFile('Photo.zip')
doing this in AppDelegate: viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; Is there any problem
Doing this: var _partsCatalog = new DirectoryCatalog(directoryName);//Fully qualified path to our bin folder var
Doing a very simple loop to display various data, for _test in @test ...
Doing a search using django-sphinx gives me results._sphinx that says there were 68 results,
Doing a top shows this top - 22:04:27 up 7:47, 3 users, load average:
Doing this: @resp = Net::HTTP.get_response(api.something.com, /feed/v1/offers.json?#{@params_api_string}) I get this response in @resp: #<Net::HTTPOK:0x7f451e9d3ef0> How

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.