Moving window sum on a boollean array, with steps.

2024/10/12 20:27:29

I'm struggling with creating a moving window sum function that calculates the number of True values in a given numpy Boolean array my_array, with a window size of n and in jumping steps of s.

For example, consider array my_array = [True, True, False, False, True, False, True]

the sum of a moving window of size n = 2 and with steps s = 2 should yield result = [2, 0, 1, 1], notice that the last window contains only one value.

I was trying my luck with itertools but to no avail.

Any help would be kindly appreciated.

Answer

Since you tagged numpy:

my_array = [True, True, False, False, True, False, True]
n = 2
s = 2
result = np.convolve(my_array, np.ones(n, ), mode='valid')[::s]
https://en.xdnf.cn/q/118164.html

Related Q&A

Python - take the time difference from the first date in a column

Given the date column, I want to create another column diff that count how many days apart from the first date.date diff 2011-01-01 00:00:10 0 2011-01-01 00:00:11 0.000011 …

(Django) Limited ForeignKey choices by Current User

Update Thanks to Michael I was able to get this to work perfectly in my CreateView, but not in the UpdateView. When I try to set a form_class it spits out an improperly configured error. How can I go a…

Parse a custom text file in Python

I have a text to be parsed, this is a concise form of the text.apple {type=fruitvarieties {color=redorigin=usa} }the output should be as shown belowapple.type=fruit apple.varieties.color=red apple.vari…

Logs Dont Overwrite

Im using Pythons logging.config module to configure and use a logging tool in my project.I want my log files to overwrite each time (not append), so I set my YAML configuration file like this:# logging…

How to upload local files to Firebase storage from Jupyter Notebook using Python

Since I guess importing google.cloud.storage might be a very first step to set API connecting the firebase storage, what I did first is to install google-cloud on Ubuntu like this:$ pip install --upgra…

How can scrapy crawl more urls?

as we see:def parse(self, response):hxs = HtmlXPathSelector(response)sites = hxs.select(//ul/li)items = []for site in sites:item = Website()item[name] = site.select(a/text()).extract()item[url] = site.…

Pyplot - shift position of y-axis ticks and its data

Using pyplot, how do I modify my plot to change the vertical position of my yticks? E.g. in my plot above, I want to move Promoter down and CDS up (along with their lines in the plot).For the above pl…

How to exit a Python program or loop via keybind or macro? Keyboardinterrupt not working

I am trying to complete a simple GUI automation program that merely opens a web page and then clicks on a specific spot on the page every 0.2 seconds until I tell it to stop. I want my code to run and …

SKlearn prediction on test dataset with different shape from training dataset shape

Im new to ML and would be grateful for any assistance provided. Ive run a linear regression prediction using test set A and training set A. I saved the linear regression model and would now like to use…

How to eliminate suspicious barcode (like 123456) data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…