cumsum

Compute the cumulative sum of a list until a zero appears

纵然是瞬间 提交于 2019-12-02 20:28:39
I have a (long) list in which zeros and ones appear at random: list_a = [1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1] I want to get the list_b sum of the list up to where 0 appears where 0 appears, retain 0 in the list list_b = [1, 2, 3, 0, 1, 2, 0, 1, 0, 1, 2, 3] I can implement this as follows: list_b = [] for i, x in enumerate(list_a): if x == 0: list_b.append(x) else: sum_value = 0 for j in list_a[i::-1]: if j != 0: sum_value += j else: break list_b.append(sum_value) print(list_b) but the actual list's length is very long. So, I want to improve code for high speed. (if it is not readable) I change

Shiny: calculate cumsum based on dygraphs' RangeSelector

久未见 提交于 2019-12-02 05:24:38
I'm building a shiny app where I want to plot a dataset with one of the variables being a cumulative sum of another variable. The latter needs to be re-calculated every time the start date of dygraphs ' dyRangeSelector changes. Below is a basic code without cumsum calculations. Commented out code is what I tried, with no success. library(shinydashboard) library(stringr) library(zoo) library(dplyr) library(dygraphs) ui <-dashboardPage( dashboardHeader(), dashboardSidebar(), dashboardBody( uiOutput("Ui1") ) ) server <- function(input, output, session) { output$Ui1 <- renderUI({ # date range

cumsum in grouped data with dplyr

萝らか妹 提交于 2019-12-02 04:25:25
问题 I have a data frame df (which can be downloaded here) referred to a register of companies that looks something like this: Provider.ID Local.Authority month year entry exit total 1 1-102642676 Warwickshire 10 2010 2 0 2 2 1-102642676 Bury 10 2010 1 0 1 3 1-102642676 Kent 10 2010 1 0 1 4 1-102642676 Essex 10 2010 1 0 1 5 1-102642676 Lambeth 10 2010 2 0 2 6 1-102642676 East Sussex 10 2010 5 0 5 7 1-102642676 Bristol, City of 10 2010 1 0 1 8 1-102642676 Liverpool 10 2010 1 0 1 9 1-102642676

MATLAB: Moving Integrator (Sum) Window with Varying Size, Based on a Condition

故事扮演 提交于 2019-12-02 03:07:56
问题 I'd like to define the start and end indices of a moving integrator (sum) window based on the cumulative sum of the values of array . Each window should have a cumulative sum of less than or equal to a threshold. The end_index of this window is uniformly moving forward by 1 for the future windows, however the start_index is dependent on the values of array . start_index could move forward, stay the same or backwards (negative values), hence the size of this moving window is not fixed. For

MATLAB: Moving Integrator (Sum) Window with Varying Size, Based on a Condition

空扰寡人 提交于 2019-12-02 01:00:47
I'd like to define the start and end indices of a moving integrator (sum) window based on the cumulative sum of the values of array . Each window should have a cumulative sum of less than or equal to a threshold. The end_index of this window is uniformly moving forward by 1 for the future windows, however the start_index is dependent on the values of array . start_index could move forward, stay the same or backwards (negative values), hence the size of this moving window is not fixed. For example: array = [ 1 0 2 1 1 2 0 0 1 2 0 1 0 1 1] ; With the start_index = 1 , the cumulative sum of array

cumsum in grouped data with dplyr

懵懂的女人 提交于 2019-12-02 00:36:57
I have a data frame df (which can be downloaded here ) referred to a register of companies that looks something like this: Provider.ID Local.Authority month year entry exit total 1 1-102642676 Warwickshire 10 2010 2 0 2 2 1-102642676 Bury 10 2010 1 0 1 3 1-102642676 Kent 10 2010 1 0 1 4 1-102642676 Essex 10 2010 1 0 1 5 1-102642676 Lambeth 10 2010 2 0 2 6 1-102642676 East Sussex 10 2010 5 0 5 7 1-102642676 Bristol, City of 10 2010 1 0 1 8 1-102642676 Liverpool 10 2010 1 0 1 9 1-102642676 Merton 10 2010 1 0 1 10 1-102642676 Cheshire East 10 2010 2 0 2 11 1-102642676 Knowsley 10 2010 1 0 1 12 1

Matlab, alternative to creating an extra vector?

家住魔仙堡 提交于 2019-12-01 11:25:32
If I have this I get an error sum(vector) == cumsum(vector)(length(vector)) >> Error: ()-indexing must appear last in an index expression. I know I can just do: Vec1 = cumsum(mat); sum(mat) == Vec1(length(mat)) which will return a logical 1. Is there an alternative to get everything on a single line? Well, if you are absolutely determined to do it in one line, sum(vec) == subsref(cumsum(vec),struct('type','()','subs',{{numel(vec)}})) But this is a borderline abuse of subsref , which is generally used for overloading the subscripting operators (i.e. {} , () , . ) in custom classes. 来源: https:/

Multiple cumulative sum within a numpy array

混江龙づ霸主 提交于 2019-12-01 10:53:54
I'm sort of newbie in numpy so I'm sorry if this question was already asked. I'm looking for a vectorization solution which enable to run multiple cumsum of different size within a one dimension numpy array. my_vector=np.array([1,2,3,4,5]) size_of_groups=np.array([3,2]) I would like something like np.cumsum.group(my_vector,size_of_groups) [1,3,6,4,9] I do not want a solution with loops. Either numpy functions or numpy operations. Not sure about numpy, but pandas can do this pretty easily with a groupby + cumsum : import pandas as pd s = pd.Series(my_vector) s.groupby(s.index.isin(size_of

pandas rolling cumsum over the trailing n elements

一曲冷凌霜 提交于 2019-12-01 06:49:00
问题 Using pandas, what is the easiest way to calculate a rolling cumsum over the previous n elements, for instance to calculate trailing three days sales: df = pandas.Series(numpy.random.randint(0,10,10), index=pandas.date_range('2020-01', periods=10)) df 2020-01-01 8 2020-01-02 4 2020-01-03 1 2020-01-04 0 2020-01-05 5 2020-01-06 8 2020-01-07 3 2020-01-08 8 2020-01-09 9 2020-01-10 0 Freq: D, dtype: int64 Desired output: 2020-01-01 8 2020-01-02 12 2020-01-03 13 2020-01-04 5 2020-01-05 6 2020-01-06

What is the inverse of the numpy cumsum function?

為{幸葍}努か 提交于 2019-11-30 22:05:47
问题 If I have z = cumsum( [ 0, 1, 2, 6, 9 ] ) , which gives me z = [ 0, 1, 3, 9, 18 ] , how can I get back to the original array [ 0, 1, 2, 6, 9 ] ? 回答1: z[1:] -= z[:-1].copy() Short and sweet, with no slow Python loops. We take views of all but the first element ( z[1:] ) and all but the last ( z[:-1] ), and subtract elementwise. The copy makes sure we subtract the original element values instead of the values we're computing. (On NumPy 1.13 and up, you can skip the copy call.) 回答2: You can use