การนับและการสรุปของลำดับหมายเลขบวกและลบ


31

ฉันต้องการเขียนโค้ดเพื่อนับและรวมจำนวนชุดบวกและลบใด ๆ
ตัวเลขมีทั้งบวกหรือลบ (ไม่เป็นศูนย์)
ฉันได้เขียนรหัสด้วยforลูป มีทางเลือกสร้างสรรค์หรือไม่?

ข้อมูล

R

set.seed(100)
x <- round(rnorm(20, sd = 0.02), 3)

หลาม

x = [-0.01, 0.003, -0.002, 0.018, 0.002, 0.006, -0.012, 0.014, -0.017, -0.007,

     0.002, 0.002, -0.004, 0.015, 0.002, -0.001, -0.008, 0.01, -0.018, 0.046]

ลูป

R

sign_indicator <- ifelse(x > 0, 1,-1)
number_of_sequence <- rep(NA, 20)
n <- 1
for (i in 2:20) {
  if (sign_indicator[i] == sign_indicator[i - 1]) {
    n <- n + 1
  } else{
    n <- 1
  }
  number_of_sequence[i] <- n

}
number_of_sequence[1] <- 1

#############################

summation <- rep(NA, 20)

for (i in 1:20) {
  summation[i] <- sum(x[i:(i + 1 - number_of_sequence[i])])
}

หลาม

sign_indicator = [1 if i > 0 else -1 for i in X]

number_of_sequence = [1]
N = 1
for i in range(1, len(sign_indicator)):
    if sign_indicator[i] == sign_indicator[i - 1]:
        N += 1
    else:
        N = 1
    number_of_sequence.append(N)

#############################
summation = []

for i in range(len(X)):
    if number_of_sequence[i] == 1:          
          summation.append(X[i])

    else:
        summation.append(sum(X[(i + 1 - number_of_sequence[i]):(i + 1)]))

ผลลัพธ์

        x n_of_sequence    sum
1  -0.010             1 -0.010
2   0.003             1  0.003
3  -0.002             1 -0.002
4   0.018             1  0.018
5   0.002             2  0.020
6   0.006             3  0.026
7  -0.012             1 -0.012
8   0.014             1  0.014
9  -0.017             1 -0.017
10 -0.007             2 -0.024
11  0.002             1  0.002
12  0.002             2  0.004
13 -0.004             1 -0.004
14  0.015             1  0.015
15  0.002             2  0.017
16 -0.001             1 -0.001
17 -0.008             2 -0.009
18  0.010             1  0.010
19 -0.018             1 -0.018
20  0.046             1  0.046

คำตอบ:


17

โซลูชันอื่น ๆ ดูใช้ได้ แต่คุณไม่จำเป็นต้องใช้คุณสมบัติภาษาที่ซับซ้อนหรือฟังก์ชั่นห้องสมุดสำหรับปัญหาง่ายๆนี้

result, prev = [], None

for idx, cur in enumerate(x):
    if not prev or (prev > 0) != (cur > 0):
        n, summation = 1, cur
    else:
        n, summation = n + 1, summation + cur
    result.append((idx, cur, n, summation))
    prev = cur

อย่างที่คุณเห็นคุณไม่ต้องการsign_indicatorรายการจริงๆสองรายการสำหรับลูปหรือrangeฟังก์ชั่นเหมือนกับในตัวอย่างในส่วนคำถาม

ถ้าคุณต้องการให้ดัชนีเริ่มจาก 1 ให้ใช้enumerate(x, 1)แทนenumerate(x)

หากต้องการดูผลลัพธ์คุณสามารถเรียกใช้รหัสต่อไปนี้

for idx, num, length, summation in result:
     print(f"{idx: >2d} {num: .3f} {length: >2d} {summation: .3f}")

14

ใน R คุณสามารถใช้data.tables rleidเพื่อสร้างกลุ่มที่มีจำนวนชุดบวกและลบจากนั้นสร้างลำดับของแถวในแต่ละกลุ่มและทำผลรวมสะสมของxค่า

library(data.table)
df <- data.table(x)
df[, c("n_of_sequence", "sum") := list(seq_len(.N), cumsum(x)), by = rleid(sign(x))]
df

#         x n_of_sequence    sum
# 1: -0.010             1 -0.010
# 2:  0.003             1  0.003
# 3: -0.002             1 -0.002
# 4:  0.018             1  0.018
# 5:  0.002             2  0.020
# 6:  0.006             3  0.026
# 7: -0.012             1 -0.012
# 8:  0.014             1  0.014
# 9: -0.017             1 -0.017
#10: -0.007             2 -0.024
#11:  0.002             1  0.002
#12:  0.002             2  0.004
#13: -0.004             1 -0.004
#14:  0.015             1  0.015
#15:  0.002             2  0.017
#16: -0.001             1 -0.001
#17: -0.008             2 -0.009
#18:  0.010             1  0.010
#19: -0.018             1 -0.018
#20:  0.046             1  0.046

เราสามารถใช้rleidในdplyrเช่นกันเพื่อสร้างกลุ่มและทำเช่นเดียวกัน

library(dplyr)
df %>%
  group_by(gr = data.table::rleid(sign(x))) %>%
  mutate(n_of_sequence = row_number(), sum = cumsum(x))

2
n_of_sequenceไม่เหมือนกับที่ต้องการ
Iman

@Iman ขออภัยฉันอ่านผิดก่อนหน้านี้ ฉันได้แก้ไขแล้ว
Ronak Shah

10

คุณสามารถคำนวณระยะเวลาในการรันของแต่ละเครื่องหมายโดยใช้rleจากbaseถึงและทำสิ่งนี้

set.seed(0)
z <- round(rnorm(20, sd = 0.02), 3)
run_lengths <- rle(sign(z))$lengths
run_lengths
# [1] 1 1 1 3 1 1 2 2 1 2 2 1 1 1

ที่จะได้รับ n_of_sequence

n_of_sequence <- run_lengths %>% map(seq) %>% unlist
n_of_sequence
# [1] 1 1 1 1 2 3 1 1 1 2 1 2 1 1 2 1 2 1 1 1

ในที่สุดเพื่อให้ได้ผลรวมของลำดับ

start <- cumsum(c(1,run_lengths))
start <- start[-length(start)] # start points of each series 
map2(start,run_lengths,~cumsum(z[.x:(.x+.y-1)])) %>% unlist()
# [1] -0.010  0.003 -0.002  0.018  0.020  0.026 -0.012  0.014 -0.017 -0.024
# [11]  0.002  0.004 -0.004  0.015  0.017 -0.001 -0.009  0.010 -0.018  0.046

6

นี่คือฟังก์ชั่นที่ไม่วนรอบง่ายใน R:

count_and_sum <- function(x)
{
  runs   <- rle((x > 0) * 1)$lengths
  groups <- split(x, rep(1:length(runs), runs))
  output <- function(group) data.frame(x = group, n = seq_along(group), sum = cumsum(group))
  result <- as.data.frame(do.call(rbind, lapply(groups, output)))
  `rownames<-`(result, 1:nrow(result))
}

ดังนั้นคุณสามารถทำได้:

set.seed(100)
x <- round(rnorm(20, sd = 0.02), 3)
count_and_sum(x)
#>         x n    sum
#> 1  -0.010 1 -0.010
#> 2   0.003 1  0.003
#> 3  -0.002 1 -0.002
#> 4   0.018 1  0.018
#> 5   0.002 2  0.020
#> 6   0.006 3  0.026
#> 7  -0.012 1 -0.012
#> 8   0.014 1  0.014
#> 9  -0.017 1 -0.017
#> 10 -0.007 2 -0.024
#> 11  0.002 1  0.002
#> 12  0.002 2  0.004
#> 13 -0.004 1 -0.004
#> 14  0.015 1  0.015
#> 15  0.002 2  0.017
#> 16 -0.001 1 -0.001
#> 17 -0.008 2 -0.009
#> 18  0.010 1  0.010
#> 19 -0.018 1 -0.018
#> 20  0.046 1  0.046

สร้างเมื่อ 2020-02-16 โดยแพ็คเกจ reprex (v0.3.0)


5

นี่คือtidyverseทางออกที่ง่าย...

library(tidyverse) #or just dplyr and tidyr

set.seed(100)
x <- round(rnorm(20, sd = 0.02), 3)

df <- tibble(x = x) %>% 
  mutate(seqno = cumsum(c(1, diff(sign(x)) != 0))) %>% #identify sequence ids
  group_by(seqno) %>%                                  #group by sequences
  mutate(n_of_sequence = row_number(),                 #count row numbers for each group
         sum = cumsum(x)) %>%                          #cumulative sum for each group
  ungroup() %>% 
  select(-seqno)                                       #remove sequence id

df
# A tibble: 20 x 3
        x n_of_sequence     sum
    <dbl>         <int>   <dbl>
 1 -0.01              1 -0.01  
 2  0.003             1  0.003 
 3 -0.002             1 -0.002 
 4  0.018             1  0.018 
 5  0.002             2  0.0200
 6  0.006             3  0.026 
 7 -0.012             1 -0.012 
 8  0.014             1  0.014 
 9 -0.017             1 -0.017 
10 -0.007             2 -0.024 
11  0.002             1  0.002 
12  0.002             2  0.004 
13 -0.004             1 -0.004 
14  0.015             1  0.015 
15  0.002             2  0.017 
16 -0.001             1 -0.001 
17 -0.008             2 -0.009 
18  0.01              1  0.01  
19 -0.018             1 -0.018 
20  0.046             1  0.046 

5

สำหรับงูหลามบางคนจะคิดวิธีแก้ปัญหาโดยใช้ห้องสมุดแพนด้า ในระหว่างนี้นี่เป็นข้อเสนอง่ายๆ:

class Combiner:
    def __init__(self):
        self.index = self.seq_index = self.summation = 0

    def combine(self, value):
        self.index += 1
        if value * self.summation <= 0:
            self.seq_index = 1
            self.summation = value
        else:
            self.seq_index += 1
            self.summation += value
        return self.index, value, self.seq_index, self.summation

c = Combiner()
lst = [c.combine(v) for v in x]

for t in lst:
    print(f"{t[0]:3} {t[1]:7.3f} {t[2]:3} {t[3]:7.3f}")

เอาท์พุท:

  1  -0.010   1  -0.010
  2   0.003   1   0.003
  3  -0.002   1  -0.002
  4   0.018   1   0.018
  5   0.002   2   0.020
  6   0.006   3   0.026
  7  -0.012   1  -0.012
  8   0.014   1   0.014
  9  -0.017   1  -0.017
 10  -0.007   2  -0.024
 11   0.002   1   0.002
 12   0.002   2   0.004
 13  -0.004   1  -0.004
 14   0.015   1   0.015
 15   0.002   2   0.017
 16  -0.001   1  -0.001
 17  -0.008   2  -0.009
 18   0.010   1   0.010
 19  -0.018   1  -0.018
 20   0.046   1   0.046

หากคุณต้องการรายการแยกคุณสามารถทำได้

idxs, vals, seqs, sums = (list(tpl) for tpl in zip(*lst))

หรือหากตัววนซ้ำตกลงเพียง

idxs, vals, seqs, sums = zip(*lst)

(คำอธิบายที่นี่ )


5

สองโซลูชั่นขี้เกียจที่แตกต่างกันในหลามโดยใช้โมดูล itertools

ใช้ itertools.groupby (และสะสม)

from itertools import accumulate, groupby

result = (
    item
    for _, group in groupby(x, key=lambda n: n < 0)
    for item in enumerate(accumulate(group), 1)
)

ใช้ itertools.accumulate พร้อมกับฟังก์ชั่นการสะสมที่กำหนดเอง

from itertools import accumulate

def sign_count_sum(count_sum, value):
    count, prev_sum = count_sum
    same_sign = (prev_sum < 0) is (value < 0)
    if same_sign:
        return count + 1, prev_sum + value
    else:
        return 1, value

result = accumulate(x, sign_count_sum, initial=(0, 0))
next(result)  # needed to skip the initial (0, 0) item

initialโต้แย้งคำหลักที่ถูกเพิ่มเข้ามาในหลาม 3.8 ในรุ่นก่อนหน้านี้คุณสามารถใช้itertools.chainเพื่อเติม (0,0) -tuple:

result = accumulate(chain([(0, 0)], x), sign_count_sum)

ผลลัพธ์เป็นไปตามที่คาดไว้:

for (i, v), (c, s) in zip(enumerate(x), result):
    print(f"{i:3} {v:7.3f} {c:3} {s:7.3f}")
  0  -0.010   1  -0.010
  1   0.003   1   0.003
  2  -0.002   1  -0.002
  3   0.018   1   0.018
  4   0.002   2   0.020
  5   0.006   3   0.026
  6  -0.012   1  -0.012
  7   0.014   1   0.014
  8  -0.017   1  -0.017
  9  -0.007   2  -0.024
 10   0.002   1   0.002
 11   0.002   2   0.004
 12  -0.004   1  -0.004
 13   0.015   1   0.015
 14   0.002   2   0.017
 15  -0.001   1  -0.001
 16  -0.008   2  -0.009
 17   0.010   1   0.010
 18  -0.018   1  -0.018
 19   0.046   1   0.046

5

ฉันแนะนำ R แพ็คเกจนักวิ่งสำหรับการทำงานประเภทนี้ streak_runคำนวณการเกิดขึ้นต่อเนื่องของค่าเดียวกันและsum_runคำนวณผลรวมในหน้าต่างที่ความยาวถูกกำหนดโดยkอาร์กิวเมนต์

นี่คือทางออก:

set.seed(100)
x <- round(rnorm(20, sd = 0.02), 3)

n_of_sequence <- runner::streak_run(x > 0)
sum <- runner::sum_run(x, k = n_of_sequence)

data.frame(x, n_of_sequence, sum)

#         x n_of_sequence    sum
# 1  -0.010             1 -0.010
# 2   0.003             1  0.003
# 3  -0.002             1 -0.002
# 4   0.018             1  0.018
# 5   0.002             2  0.020
# 6   0.006             3  0.026
# 7  -0.012             1 -0.012
# 8   0.014             1  0.014
# 9  -0.017             1 -0.017
# 10 -0.007             2 -0.024
# 11  0.002             1  0.002
# 12  0.002             2  0.004
# 13 -0.004             1 -0.004
# 14  0.015             1  0.015
# 15  0.002             2  0.017
# 16 -0.001             1 -0.001
# 17 -0.008             2 -0.009
# 18  0.010             1  0.010
# 19 -0.018             1 -0.018
# 20  0.046             1  0.046

ต่ำกว่ามาตรฐานเพื่อเปรียบเทียบโซลูชันจริง

set.seed(0)
x <- round(rnorm(10000, sd = 0.02), 3)

library(runner)
runner_streak <- function(x) {
  n_of_sequence <- streak_run(x > 0)
  sum <- sum_run(x, k = n_of_sequence)
}

library(data.table)
dt <- data.table(x)
dt_streak <- function(dt) {
  dt[, c("n_of_sequence", "sum") := list(seq_len(.N), cumsum(x)),rleid(sign(x))]
}

rle_streak <- function(x) {
  run_lengths <- rle(sign(x))$lengths
  run_lengths

  n_of_sequence <- run_lengths %>% map(seq) %>% unlist

  start <- cumsum(c(1,run_lengths))
  start <- start[-length(start)]
  sum <- map2(start,run_lengths,~cumsum(x[.x:(.x+.y-1)])) %>% unlist()
}

library(tidyverse)
df <- tibble(x = x)
tv_streak <- function(x) {
  res <- df %>%
    mutate(seqno = cumsum(c(1, diff(sign(x)) != 0))) %>%
    group_by(seqno) %>%
    mutate(n_of_sequence = row_number(),
           sum = cumsum(x)) %>%
    ungroup() %>% 
    select(-seqno)  
}

count_and_sum <- function(x) {
  runs   <- rle((x > 0) * 1)$lengths
  groups <- split(x, rep(1:length(runs), runs))
  output <- function(group) 
    data.frame(x = group, n = seq_along(group), sum = cumsum(group))
  result <- as.data.frame(do.call(rbind, lapply(groups, output)))
  `rownames<-`(result, 1:nrow(result))
}
microbenchmark::microbenchmark(
  runner_streak(x),
  dt_streak(dt),
  rle_streak(x),
  tv_streak(df),
  count_and_sum(x),
  times = 100L
)


# Unit: milliseconds
#             expr         min          lq        mean      median          uq        max neval
# runner_streak(x)    4.240192    4.833563    6.321697    5.300817    6.543926   14.80221   100
#    dt_streak(dt)    7.648100    8.587887   10.862806    9.650483   11.295488   34.66027   100
#    rle_streak(x)   42.321506   55.397586   64.195692   63.404403   67.813738  167.71444   100
#    tv_streak(df)   31.398885   36.333751   45.141452   40.800077   45.756279  163.19535   100
# count_and_sum(x) 1691.438977 1919.518282 2306.036783 2149.543281 2499.951020 6158.43384   100

1
การวัดเป็นไมโครวินาทีไม่สมเหตุสมผล บางฟังก์ชั่นมีโอเวอร์เฮดเริ่มต้นเป็นไมโครวินาที แต่จะขยายสำหรับชุดข้อมูลขนาดใหญ่ที่ดีกว่าชุดอื่น ๆ ยังdf <- data.table(x)เป็นการคัดลอกข้อมูลแบบเต็ม นอกจากนี้คุณกำลังพิมพ์ข้อมูลในตัวอย่าง (ซึ่งเป็นสำเนาเต็มรูปแบบอื่น) ในขณะที่ไม่ได้อยู่ในคนอื่น
David Arenburg

คุณถูกต้องคงที่
GoGonzo

ฟังก์ชั่นบางตัวส่งคืนออบเจ็กต์ที่แตกต่างกัน - เวกเตอร์บางตัวและดาต้าเฟรมบางตัว - ดังนั้นมันจึงยังไม่ได้มาตรฐานที่ค่อนข้างยุติธรรม บางคนก็ให้ผลลัพธ์ที่ต่างกัน ลองr = runner_streak(x); d = dt_streak(dt) ; all.equal(r, d$sum)ดู ตรวจสอบเพียงไม่กี่ bbut tv_streakให้เหมือนdt_streak; count_and_sumให้เหมือนกันrunner_streakซึ่งแตกต่างจากสองคนก่อนหน้านี้
user2957945

3

ใน R คุณสามารถทำได้เช่นกัน:

# DATA
set.seed(100)
x <- round(rnorm(20, sd = 0.02), 3)

library(data.table)
dt <- data.table(x = x)

# Create Positive or Negative variable
dt$x_logical <- ifelse(dt$x > 0, "P", "N")

# Create a reference data.frame/table to keep continuous counts
seq_dt <- data.frame(val = rle(x = dt$x_logical)$lengths)
seq_dt$id <- 1:nrow(seq_dt)

# Map id in the main data.table and get cumulative sum
dt$id <- rep(seq_dt$id, seq_dt$val)
dt[, csum := cumsum(x), by = "id"]


        x x_logical id   csum
 1: -0.010         N  1 -0.010
 2:  0.003         P  2  0.003
 3: -0.002         N  3 -0.002
 4:  0.018         P  4  0.018
 5:  0.002         P  4  0.020
 6:  0.006         P  4  0.026
 7: -0.012         N  5 -0.012
 8:  0.014         P  6  0.014
 9: -0.017         N  7 -0.017
10: -0.007         N  7 -0.024
11:  0.002         P  8  0.002
12:  0.002         P  8  0.004
13: -0.004         N  9 -0.004
14:  0.015         P 10  0.015
15:  0.002         P 10  0.017
16: -0.001         N 11 -0.001
17: -0.008         N 11 -0.009
18:  0.010         P 12  0.010
19: -0.018         N 13 -0.018
20:  0.046         P 14  0.046

3

โยนคำตอบ [r] ของฉันไว้ในหมวกปรับให้เหมาะกับความเร็วและทำงานร่วมกับความยาว x ใด ๆ (ซึ่งแตกต่างจากผู้ถามที่รหัสยากสำหรับความยาว 20):

### data 
set.seed(100)
x <- round(rnorm(20, sd = 0.02), 3)

### solution
summation <- c(x[1])
enn <- 1
n_of_seq <- c(enn)
for(i in 2:length(x)){
  first <- x[i]
  second <- summation[i - 1]

  if(sign(first) == sign(second)){
    summation <- c(summation, first + second)
    enn <- enn + 1
  }else{
    summation <- c(summation, first)
    enn <- 1

  }
  n_of_seq <- c(n_of_seq, enn)
  }

และเพื่อเปรียบเทียบเวลาทำงานบนคอมพิวเตอร์ทำงานปัจจุบันของฉัน (ช้ามาก) นี่คือผลลัพธ์ของ microbenchmark ของฉันโดยใช้โซลูชัน R ทั้งหมดในชุดข้อความนี้ แปลกใจที่โซลูชันที่ทำให้การคัดลอกและการแปลงส่วนใหญ่มีแนวโน้มที่จะช้าลง

Unit: microseconds
         expr      min       lq       mean    median       uq      max neval
     my_way()   13.301   19.200   23.38352   21.4010   23.401  20604.0 1e+05
 author_way()   19.702   31.701   40.12371   36.0015   40.502  24393.9 1e+05
      ronak()  856.401 1113.601 1305.36419 1236.8010 1377.501 453191.4 1e+05
      ameer()  388.501  452.002  553.08263  491.3000  548.701 456156.6 1e+05
     andrew() 2007.801 2336.801 2748.57713 2518.1510 2760.302 463175.8 1e+05
      gonzo()   21.901   35.502   48.84946   43.9010   51.001  29519.5 1e+05

-------------- แก้ไข -------------- มันชี้ให้เห็นโดย @nicola ว่าโซลูชันของฉันไม่ใช่วิธีที่เร็วที่สุดสำหรับความยาวของ x ซึ่งยาวกว่า ควรจะค่อนข้างชัดเจนเนื่องจากฉันทำสำเนาเวกเตอร์อย่างต่อเนื่องโดยใช้การเรียกเช่น x <- c (x, y) ฉันสร้างโซลูชันที่เร็วที่สุดสำหรับ lengths = 20 และ microbenchmarked ต่ำที่สุดเท่าที่จะทำได้

เพื่อทำการเปรียบเทียบที่เป็นธรรมฉันได้แก้ไขทุกเวอร์ชันเพื่อสร้างรหัสดั้งเดิมในแบบที่ฉันเชื่อว่าจะเร็วที่สุด แต่ฉันยินดีรับข้อเสนอแนะ นี่คือรหัสเปรียบเทียบและผลลัพธ์สำหรับระบบที่ช้ามากของฉัน ฉันยินดีรับข้อเสนอแนะใด ๆ

# originally benchmarked a few different lengths
for(pie in c(100000)){


my_way<- function(){
  set.seed(100)
  x <- round(rnorm(pie, sd = 0.02), 3)
summation <- c(x[1])
enn <- 1
n_of_seq <- c(enn)
for(i in 2:length(x)){
  first <- x[i]
  second <- summation[i - 1]

  if(sign(first) == sign(second)){
    summation <- c(summation, first + second)
    enn <- enn + 1
  }else{
    summation <- c(summation, first)
    enn <- 1

  }
  n_of_seq <- c(n_of_seq, enn)
  }

# print(summation)
}




author_way <- function(){
  set.seed(100)
  x <- round(rnorm(pie, sd = 0.02), 3)

  sign_indicator <- ifelse(x > 0, 1,-1)
  sky <- length(x)
  number_of_sequence <- rep(NA, sky)
  n <- 1
  for (i in 2:sky) {
    if (sign_indicator[i] == sign_indicator[i - 1]) {
      n <- n + 1
    } else{
      n <- 1
    }
    number_of_sequence[i] <- n

  }
  number_of_sequence[1] <- 1

  #############################

  summation <- rep(NA, sky)

  for (i in 1:sky) {
    summation[i] <- sum(x[i:(i + 1 - number_of_sequence[i])])
  }
}


# other ppls solutions:




ronak <- function(){
df <- data.table('x' = round(rnorm(pie, sd = 0.02), 3))
df[, c("n_of_sequence", "sum") := list(seq_len(.N), cumsum(x)),rleid(sign(x))]
}



ameer <- function(){
  set.seed(100)
  x <- round(rnorm(pie, sd = 0.02), 3)
  run_lengths <- rle(sign(x))$lengths
  n_of_sequence <- run_lengths %>% map(seq) %>% unlist
  start <- cumsum(c(1,run_lengths))
  start <- start[-length(start)] # start points of each series 
  map2(start,run_lengths,~cumsum(x[.x:(.x+.y-1)])) %>% unlist()

}


count_and_sum <- function(x){
  set.seed(100)
  x <- round(rnorm(pie, sd = 0.02), 3)
  runs   <- rle((x > 0) * 1)$lengths
  groups <- split(x, rep(1:length(runs), runs))
  output <- function(group) data.frame(x = group, n = seq_along(group), sum = cumsum(group))
  result <- as.data.frame(do.call(rbind, lapply(groups, output)))
  `rownames<-`(result, 1:nrow(result))
}



andrew <- function(){
  set.seed(100)
  df <- tibble(x = round(rnorm(pie, sd = 0.02), 3)) %>% 
    mutate(seqno = cumsum(c(1, diff(sign(x)) != 0))) %>% #identify sequence ids
    group_by(seqno) %>%                                  #group by sequences
    mutate(n_of_sequence = row_number(),                 #count row numbers for each group
           sum = cumsum(x)) %>%                          #cumulative sum for each group
    ungroup() %>% 
    select(-seqno) 
}

gonzo <- function(){
  set.seed(100)
  x <- round(rnorm(pie, sd = 0.02), 3)
  n_of_sequence <- runner::streak_run(x > 0)
  sum <- runner::sum_run(x, k = n_of_sequence)
}



mi1 <- microbenchmark(my_way(), author_way(), ronak(), ameer(), andrew(), gonzo(), times = 10)
print(mi1)

}

เมื่อผลลัพธ์เหล่านี้แสดงสำหรับความยาวอื่นนอกเหนือจากที่ฉันปรับให้เหมาะสมรุ่นของฉันจะช้า ยิ่ง x ยาวเท่าไหร่ก็ยิ่งช้าขึ้นเรื่อย ๆ จนทุกอย่างที่สูงกว่า 1,000 ช้าลงเวอร์ชันที่ฉันชอบคือ Ronak ซึ่งเร็วที่สุดเป็นอันดับสองในระบบของฉัน GoGonzo เร็วที่สุดในเครื่องของฉันโดยความยาวที่ยาวกว่านี้

Unit: milliseconds
         expr        min         lq        mean      median         uq        max neval
     my_way() 21276.9027 21428.2694 21604.30191 21581.97970 21806.9543 21896.7105    10
 author_way()    82.2465    83.0873    89.42343    84.78315    85.3638   115.4550    10
      ronak()    68.3922    69.3067    70.41924    69.84625    71.3509    74.7070    10
      ameer()   481.4566   509.7552   521.19034   514.77000   530.1121   579.4707    10
     andrew()   200.9654   202.1898   210.84914   206.20465   211.2006   233.7618    10
      gonzo()    27.3317    28.2550    28.66679    28.50535    28.9104    29.9549    10

อีกทั้งคำตอบอื่น ๆ ใช้ได้กับทุกความยาวและมาตรฐานของคุณจะต้องมีปัญหาบางอย่าง สำหรับdata.tableวิธีการแก้ปัญหาของ @ Ronak คุณมีลำดับความสำคัญช้ากว่าความยาวประมาณ 100000
ล่า

ขอบคุณ @nicola ฉันแค่บอกว่าวิธีแก้ปัญหาของผู้ถามทำงานได้เพียง 20 ข้อเท่านั้นไม่ใช่วิธีแก้ปัญหาอื่นที่ไม่ได้ทำ ฉันยังปรับความเร็วสำหรับความยาวของ 20 รายการเพื่อให้การเรียกร้องของฉันจบเร็วที่สุดที่นั่น สำหรับสิ่งที่คุ้มค่าฉันก็ชอบ Ronaks มากที่สุด แต่ผู้เขียนขอวิธีที่แตกต่างกันในการแก้ปัญหาอย่างชัดเจน ของ Ronak นั้นเร็วขึ้นสำหรับความยาว 1,000 เช่นกัน
Adverse_Event

และเพื่อขยายบน microbenchmark ฉันคำนวณเกณฑ์มาตรฐานของฉันเพื่อให้ทุกโซลูชันสร้าง (x) ในรูปแบบที่ใช้ดังนั้นผู้ที่สร้าง tibbles จะสร้าง x ในการเรียก tibble เช่นเดียวกันกับ data.table เป็นต้นฉัน recoder โซลูชันดั้งเดิมของผู้ถามเพื่อทำงานได้ทุกความยาว (เพียงแค่บันทึกความยาวของ x ในตัวแปรและแทนที่ 20 ด้วยแล้วฉันก็วิ่งไปตามความยาว 100.000 สำหรับการทำซ้ำ 10 ครั้งหมายเหตุคอมพิวเตอร์ของฉัน suuuuper ช้ามันทำงานบนโปรเซสเซอร์ระหว่างเจนเนอเรชั่น 5 กับ ddr3 ที่ 1600 mHz. ฉันจะแก้ไขโพสต์ของฉันด้วยผลลัพธ์เหล่านั้น
Adverse_Event

2

ใน Python นอกเหนือจากการกำหนดคลาสเพื่อเก็บตัวแปรหน่วยความจำคุณสามารถใช้การปิดเพื่อให้ได้ผลลัพธ์ที่เหมือนกัน

def run():
    count = 0
    last_sign = 0

    def sign(i):
        return 1 if i > 0 else -1

    def f(i):
        nonlocal count
        nonlocal last_sign
        if sign(i) == last_sign:
            count = count+1
        else:
            last_sign = sign(i)
            count = 1
        return count

    return f

f = run()
y = [f(i) for i in x]

โปรดทราบว่านี่ใช้งานได้กับ Python 3 เท่านั้น (ใน Python 2 ฉันคิดว่าคุณไม่สามารถแก้ไขตัวแปรการปิดเช่นนี้) สิ่งที่คล้ายกันสำหรับการรวมเช่นกัน


2

ฉันคิดว่าการวนซ้ำจะง่ายต่อการอ่าน แต่เพื่อความสนุกนี่เป็นวิธีแก้ปัญหาใน Python โดยใช้การเรียกซ้ำ:

x = [-0.01, 0.003, -0.002, 0.018, 0.002, 0.006, -0.012, 0.014, -0.017, -0.007, 0.002, 0.002, -0.004, 0.015, 0.002,
     -0.001, -0.008, 0.01, -0.018, 0.046]


def sign(number):
    return 1 if number > 0 else -1


def sum_previous(pos, result=None):
    if not result:
        result = x[pos]
    else:
        result += x[pos]
    if pos == 0 or sign(x[pos]) != sign(x[pos-1]):
        return result
    else:
        return sum_previous(pos-1, result)


results = [sum_previous(i) for i in range(len(x))]
print(results)

2

นี่เป็นอีกวิธีพื้นฐาน R:

data.frame(x,
           n = sequence(rle(sign(x))$lengths),
           sum = Reduce(function(x, y) if (sign(x) == sign(y)) x + y else y, x, accumulate = TRUE))

        x n    sum
1  -0.010 1 -0.010
2   0.003 1  0.003
3  -0.002 1 -0.002
4   0.018 1  0.018
5   0.002 2  0.020
6   0.006 3  0.026
7  -0.012 1 -0.012
8   0.014 1  0.014
9  -0.017 1 -0.017
10 -0.007 2 -0.024
11  0.002 1  0.002
12  0.002 2  0.004
13 -0.004 1 -0.004
14  0.015 1  0.015
15  0.002 2  0.017
16 -0.001 1 -0.001
17 -0.008 2 -0.009
18  0.010 1  0.010
19 -0.018 1 -0.018
20  0.046 1  0.046

เพื่อ nitpick Reduceซ่อนวงดังนั้นนี่ไม่ใช่วิธีแก้ปัญหาแบบวนรอบ
ล่า

2

คำตอบของงูหลามง่ายละเว้นกรณี 0:

x = [-0.01, 0.003, -0.002, 0.018, 
     0.002, 0.006, -0.012, 0.014, 
     -0.017, -0.007, 0.002, 0.002, 
     -0.004, 0.015, 0.002, -0.001, 
     -0.008, 0.01, -0.018, 0.046]

count = 0
sign_positive = x[0] > 0
sign_count = []
for n in x:
    # the idea is to keep track of the sign and increment the 
    # count if it agrees with the current number we are looking at
    if (n > 0 and sign_positive) or (n < 0 and not sign_positive):
        count = count + 1
    # if it does not, the count goes back to 1
    else:
        count = 1
    # Whether we increased the count or not, we update whether the
    # sign was positive or negative
    sign_positive = n > 0
    sign_count.append(count)

# This is just to reproduce the output 
# (although I find the last repetition of the number unnecessary)    
results = list(zip(x, sign_count))
for i, result in enumerate(results):
    print(f"{i: >2d} {result[0]: .3f} {result[1]: >2d} {result[0]: .3f}")

 0 -0.010  1 -0.010
 1  0.003  1  0.003
 2 -0.002  1 -0.002
 3  0.018  1  0.018
 4  0.002  2  0.002
 5  0.006  3  0.006
 6 -0.012  1 -0.012
 7  0.014  1  0.014
 8 -0.017  1 -0.017
 9 -0.007  2 -0.007
10  0.002  1  0.002
11  0.002  2  0.002
12 -0.004  1 -0.004
13  0.015  1  0.015
14  0.002  2  0.002
15 -0.001  1 -0.001
16 -0.008  2 -0.008
17  0.010  1  0.010
18 -0.018  1 -0.018
19  0.046  1  0.046

วิธีแก้ปัญหาที่ซับซ้อนยิ่งขึ้นอีกเล็กน้อยดูแลกรณี 0:

# To test the 0 case I am changing two numbers to 0
x = [-0.01, 0.003, -0.002, 0.018, 
     0.002, 0.006, -0.012, 0.014, 
    -0.017, -0.007, 0, 0, 
    -0.004, 0.015, 0.002, -0.001, 
    -0.008, 0.01, -0.018, 0.046]

# The rest is similar
count = 0
# This time we are using a nested ternary assignment 
# to account for the case of 0
# This would be more readable as a function, 
# but what it does is simple
# It returns None if n is 0, 
# True if it is larger than 0 
# and False if it less than 0
sign_positive = None if n == 0 else False if n < 0 else True
sign_count = []
for n in x:
    # We add the case of 0 by adding a third condition where
    # sign_positive was None (meaning the previous
    # number was 0) and the current number is 0.
    if (n > 0 and sign_positive) or \
       (n < 0 and not sign_positive) or \
       (n == 0 and sign_positive == None):
        count = count + 1
    else:
        count = 1
    sign_positive = None if n == 0 else False if n < 0 else True
    sign_count.append(count)
results = list(zip(x, sign_count))
for i, result in enumerate(results):
    print(f"{i: >2d} {result[0]: .3f} {result[1]: >2d} {result[0]: .3f}")

 0 -0.010  1 -0.010
 1  0.003  1  0.003
 2 -0.002  1 -0.002
 3  0.018  1  0.018
 4  0.002  2  0.002
 5  0.006  3  0.006
 6 -0.012  1 -0.012
 7  0.014  1  0.014
 8 -0.017  1 -0.017
 9 -0.007  2 -0.007
10  0.000  1  0.000
11  0.000  2  0.000
12 -0.004  3 -0.004
13  0.015  1  0.015
14  0.002  2  0.002
15 -0.001  1 -0.001
16 -0.008  2 -0.008
17  0.010  1  0.010
18 -0.018  1 -0.018
19  0.046  1  0.046
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.