cumulative-sum

using LINQ to find the cumulative sum of an array of numbers in C#

心不动则不痛 提交于 2019-11-30 07:22:07
问题 I have a csv string containing doubles (e.g "0.3,0.4,0.3"), and I want to be able to output a double array containing the cumulative sum of these numbers (e.g [0.3,0.7,1.0]). So far, I have double[] probabilities = textBox_f.Text.Split(new char[]{','}).Select(s => double.Parse(s)).ToArray(); which gives the numbers as an array, but not the cumulative sum of the numbers. Is there any way to continue this expression to get what I want, or do I need to use iteration to create a new array from

Cumulative sum in Spark

岁酱吖の 提交于 2019-11-29 23:26:57
问题 I want to do cumulative sum in Spark. Here is the register table (input): +---------------+-------------------+----+----+----+ | product_id| date_time| ack|val1|val2| +---------------+-------------------+----+----+----+ |4008607333T.upf|2017-12-13:02:27:01|3-46| 53| 52| |4008607333T.upf|2017-12-13:02:27:03|3-47| 53| 52| |4008607333T.upf|2017-12-13:02:27:08|3-46| 53| 52| |4008607333T.upf|2017-12-13:02:28:01|3-47| 53| 52| |4008607333T.upf|2017-12-13:02:28:07|3-46| 15| 1| +---------------+------

How do I do a conditional sum which only looks between certain date criteria

*爱你&永不变心* 提交于 2019-11-29 08:43:39
Say I have data that looks like date, user, items_bought, event_number 2013-01-01, x, 2, 1 2013-01-02, x, 1, 2 2013-01-03, x, 0, 3 2013-01-04, x, 0, 4 2013-01-04, x, 1, 5 2013-01-04, x, 2, 6 2013-01-05, x, 3, 7 2013-01-06, x, 1, 8 2013-01-01, y, 1, 1 2013-01-02, y, 1, 2 2013-01-03, y, 0, 3 2013-01-04, y, 5, 4 2013-01-05, y, 6, 5 2013-01-06, y, 1, 6 to get the cumulative sum per user per data point I was doing data.frame(cum_items_bought=unlist(tapply(as.numeric(data$items_bought), data$user, FUN = cumsum))) output from this looks like date, user, items_bought 2013-01-01, x, 2 2013-01-02, x, 3

Cumulating value of current row + sum of previous rows

こ雲淡風輕ζ 提交于 2019-11-29 03:25:14
How would you do to transform a Column in a table from this: ColumnA ColumnB 2 a 3 b 4 c 5 d 1 a to this: ColumnA ColumnB 3 a 6(=3+3) b 10(=4+3+3) c 15(=5+4+3+3) d I'm interested to see esp. what method you would pick. Mahmoud Gamal Like this: ;WITH cte AS ( SELECT ColumnB, SUM(ColumnA) asum FROM @t gROUP BY ColumnB ), cteRanked AS ( SELECT asum, ColumnB, ROW_NUMBER() OVER(ORDER BY ColumnB) rownum FROM cte ) SELECT (SELECT SUM(asum) FROM cteRanked c2 WHERE c2.rownum <= c1.rownum), ColumnB FROM cteRanked c1; This should give you: ColumnA ColumnB 3 a 6 b 10 c 15 d Here is a live demo I'd

using LINQ to find the cumulative sum of an array of numbers in C#

别等时光非礼了梦想. 提交于 2019-11-29 03:07:12
I have a csv string containing doubles (e.g "0.3,0.4,0.3"), and I want to be able to output a double array containing the cumulative sum of these numbers (e.g [0.3,0.7,1.0]). So far, I have double[] probabilities = textBox_f.Text.Split(new char[]{','}).Select(s => double.Parse(s)).ToArray(); which gives the numbers as an array, but not the cumulative sum of the numbers. Is there any way to continue this expression to get what I want, or do I need to use iteration to create a new array from the array I already have? var input=new double[]{ ... } double sum=0; var output=input .Select(w=>sum+=w)

Why is my inclusive scan code 2x faster on CPU than on a GPU?

十年热恋 提交于 2019-11-28 14:43:20
I wrote a short CUDA program that uses the highly-optimized CUB library to demonstrate that one core from an old, quad-core Intel Q6600 processor (all four are supposedly capable of ~30 GFLOPS/sec) can do an inclusive scan (or cumulative/prefix sum if you rather) on 100,000 elements faster than an Nvidia 750 Ti (supposedly capable of 1306 GFLOPS/sec of single precision). Why is this the case? The source code is: #include "cuda_runtime.h" #include "device_launch_parameters.h" #include <cub/cub.cuh> #include <stdio.h> #include <time.h> #include <algorithm> #define gpuErrchk(ans) { gpuAssert((ans

Creating a cumulative step graph in R

眉间皱痕 提交于 2019-11-28 01:18:31
问题 Say I have this example data frame set.seed(12345) n1 <- 3 n2 <- 10 n3 <- 60 times <- seq(0, 100, 0.5) individual <- c(rep(1, n1), rep(2, n2), rep(3, n3)) events <- c(sort(sample(times, n1)), sort(sample(times, n2)), sort(sample(times, n3))) df <- data.frame(individual = individual, events = events) Which gives > head(df, 10) individual events 1 1 72.0 2 1 75.5 3 1 87.5 4 2 3.0 5 2 14.5 6 2 16.5 7 2 32.0 8 2 45.5 9 2 50.0 10 2 70.5 I would like to plot a cumulative step graph of the events so

cumulative plot using ggplot2

删除回忆录丶 提交于 2019-11-27 23:01:34
I'm learning to use ggplot2 and am looking for the smallest ggplot2 code that reproduces the base::plot result below. I've tried a few things and they all ended up being horrendously long, so I'm looking for the smallest expression and ideally would like to have the dates on the x-axis (which are not there in the plot below). df = data.frame(date = c(20121201, 20121220, 20130101, 20130115, 20130201), val = c(10, 5, 8, 20, 4)) plot(cumsum(rowsum(df$val, df$date)), type = "l") Try this: ggplot(df, aes(x=1:5, y=cumsum(val))) + geom_line() + geom_point() Just remove geom_point() if you don't want

Vector of cumulative sums in R

大憨熊 提交于 2019-11-27 19:35:13
问题 I'm trying to get a vector of cumulative sums, that is, I have: # 500 Samples from the U(0,1) Distribution U<-runif(500,0,1) # Empty Vector of length 500 F<-rep(0,500) # Fill the vector with f(U(k)) for ( i in 1:500 ){ F[i] <- sqrt(1-U[i]^2) } # Another Empty Vector of length 500 I<-rep(0,500) # Fill the second empty vector with the sums of F for ( i in 1:500 ){ I[i]<-cumsum(F[1]:F[i]) } The last line of code is the problem, I want 'I' to be a vector such that I[1] = F[1], I[n] = F[1] + F[2]

cumulative plot using ggplot2

拟墨画扇 提交于 2019-11-27 17:48:03
问题 I'm learning to use ggplot2 and am looking for the smallest ggplot2 code that reproduces the base::plot result below. I've tried a few things and they all ended up being horrendously long, so I'm looking for the smallest expression and ideally would like to have the dates on the x-axis (which are not there in the plot below). df = data.frame(date = c(20121201, 20121220, 20130101, 20130115, 20130201), val = c(10, 5, 8, 20, 4)) plot(cumsum(rowsum(df$val, df$date)), type = "l") 回答1: Try this: