Estoy tratando de traducir este código R a Python. La parte que no entiendo es lo que hacen los corchetes dobles en EventTime[[i]] = c(timeList,cutoff[i]). EventTime está vacío para empezar también.

n = 50
cutoff = runif(n,min = 2, max = n)
rvStore = rgeom(1000,prob=0.2)+runif(1000) 
rvPosn = 0
EventTime = list()
for(i in 1:n){
        rvPosn = rvPosn + 1
        intArrival = currentTime = rvStore[rvPosn] 
        timeList = c()
        while(currentTime < cutoff[i]){
          timeList = c(timeList,currentTime)
          rvPosn = rvPosn + 1
          intArrival = rvStore[rvPosn]
          currentTime = currentTime + intArrival
        }
        EventTime[[i]] = c(timeList,cutoff[i])
}

En Python esto parece ser:

n = 50
cutoff = np.random.uniform(n, 2, n)
rvStore = np.random.geometric(0.2, size=1000) + np.random.uniform(size=1000)
rvPosn = 0
EventTime = []
for i in range(n):
    rvPosn = rvPosn + 1
    intArrival = currentTime = rvStore[rvPosn]
    timeList = []
    while(currentTime < cutoff[i]):
        timeList.append(currentTime)
        rvPosn = rvPosn + 1
        intArrival = rvStore[rvPosn]
        currentTime = currentTime + intArrival
    EventTime[i] = # What should this be??
2
donald 9 may. 2019 a las 19:28

3 respuestas

La mejor respuesta

Para comenzar, abordemos la terminología R: EventTime es una lista, esto significa que para obtener / modificar una entrada específica se necesita la notación de doble paréntesis, vea

EventTime[1] # returns the sub-list consisting of the first entry
EventTime[[1]] # actually returns the content of the first entry

Acerca de que la lista está vacía, creo que internamente EventTime[[i]] = ... vuelve a declarar la lista. Considere el siguiente ejemplo:

test_list <- list()
test_list[[2]] <- "Hello"
test_list
# [[1]]
# NULL
# 
# [[2]]
# [1] "Hello"

Ahora a Python, aquí está su código ligeramente modificado:

# import numpy as np

# Set seed for replicating purposes
np.random.seed(seed=123)

n = 50
cutoff = np.random.uniform(n, 2, n)
rvStore = np.random.geometric(0.2, size=1000) + np.random.uniform(size=1000)
rvPosn = -1    # Indexing starts at 0 in python, hence set to -1 here.
EventTime = []
for i in range(n):
    rvPosn += 1
    intArrival = currentTime = rvStore[rvPosn]
    timeList = list()
    while currentTime < cutoff[i]:
        timeList.append(currentTime)
        rvPosn += 1
        intArrival = rvStore[rvPosn]
        currentTime += intArrival
    timeList.append(cutoff[i])
    if EventTime:    # If EventTime is not an empty list
        EventTime.extend(timeList)
    else:
        EventTime = timeList

Que regresa

[1.6980586861472202, 9.698178787636195, 15.519992402662904, 16.56947909130264, 2.744619894754501, 4.788857101608995, 7.998121531007067, 14.068926710602266, 24.248750505706788, 28.63317173798157, 34.12472354898552, 36.265311922381784, 5.627174019986028, 12.333653833967311, 21.415764510639853, 22.702551598773276, 30.69428351172636, 33.25470581337055, 35.042523246771054, 39.07500571277746, 39.11113022891825, 10.109233198101675, 15.12451904244045, 22.046498011097786, 23.300133297270904, 23.53689108402122, 4.738129850542359, 5.988740640658454, 7.98054527634349, 14.849079133869045, 15.46548945029297, 6.1853219112587485, 10.865508052726064, 13.943788298320333, 26.528313160713935, 29.690889914025874, 2.2212976062688705, 2.9233184775384586, 2.4985716373493645, 6.072284606863263, 12.341967884723532, 17.12817254792656, 2.133002406072575, 9.64961871637566, 14.703574509175564, 20.751823877739234, 22.431471617346183, 25.486453227823375, 26.915268728750675, 2.266026494157204, 6.45372043716427, 12.027039327218917, 13.323502624568642, 14.779884999295646, 16.91885883801014, 22.045344526463364, 31.151873461318353, 31.178359126680775, 9.553713718700765, 13.309719210188344, 16.1020860823872, 19.059930890526598, 20.228323250717473, 28.363942204606314, 30.833896983916503, 33.52745522475827, 4.76706927392631, 9.325247382145143, 10.48203064775288, 15.005614045566006, 11.71934577665475, 14.092758461597112, 16.131877811873775, 20.715761772946976, 28.948532255378026, 19.714771491642455, 21.879637330177182, 26.215628919581114, 34.62480121187804, 38.045282624605164, 44.15944111410984, 45.544972644811196, 47.13546096274072, 10.710560672116372, 30.280008360960473, 30.893875744139294, 3.927597362754534, 6.525680929386272, 10.500471813091686, 14.576220524862286, 1.6736261016251144, 4.57293959704982, 17.564179275168147, 21.785056542132292, 25.476113062036095, 33.39489233989522, 40.412292499911686, 41.240396938232, 6.089402949891007, 16.00595724733622, 17.617970620740373, 24.780040059306952, 26.819450226878004, 29.647271710654223, 34.78676365159901, 36.72630502544583, 41.57831570492036, 10.837978483793233, 17.261511027023744, 21.134246374413856, 24.485534055591756, 7.852882282360201, 12.323124374770442, 14.03689269149914, 18.355560284189572, 24.47227581935043, 2.2324001108540488, 14.233761749959303, 19.54875398953659, 4.493828836984034, 9.227273884266097, 1.0077298152264467, 10.694193383658753, 15.226144406689507, 11.073845506226222, 12.569443681531235, 14.287602991112045, 16.26476822974501, 19.560165529909696, 20.67087148747602, 4.068536584039964, 5.276047280627715, 7.3761019376819235, 15.322717636629363, 3.285073692160046, 11.449280575828558, 12.467530477923688, 16.822162461370844, 30.648078248362534, 34.49797213504745, 2.710099736493584, 9.438834684145377, 12.464391147841388, 18.426176250775413, 19.565185643765425, 25.28265063306906, 26.662093768021577, 32.63414453012892, 3.994961028423735, 5.18828399578152, 10.007739613829647, 11.513242561409056, 17.47867362569205, 39.04336491781013, 7.593237901646298, 11.8227613492495, 13.54146141356032, 19.829662700621885, 25.67542131336267, 27.652684972461035, 35.66047795394031, 35.90172577333619, 3.358460293164132, 10.846775972674441, 14.770861486372471, 18.546819175077673, 19.713146054984588, 2.096852688045505, 11.938078915790413, 13.685139048263647, 17.543478471440224, 38.9275197998302, 41.41963351046323, 45.57896288263639, 12.3147216705513, 13.6503940485568, 17.369043307741684, 22.122114434468987, 24.98596842097862, 26.83087045905422, 29.182343711382643, 1.332777817869239, 4.04282415569234, 13.015448647800634, 24.931688562672857, 26.90317628906251, 28.93938380900622, 29.318587360177098, 4.215342900973726, 9.46190263537273, 11.305963543508307, 15.056155223300028, 26.3031153127853, 3.5199153636501332, 4.708245240710887, 7.712072444510997, 23.612030551490193, 26.321672870883, 29.560146065800257, 8.054098862555623, 10.474939039977933, 13.854980941002268, 20.02652793751223, 23.18271621631419, 24.355894013541274, 35.01146129732167, 5.124703951840116, 14.363253208503512, 16.309525389507478, 18.528987173959504, 23.29284377045799, 24.890883780006522, 29.535137265785206, 2.5952858934976852, 5.728905923419344, 7.117320170377532, 1.9721335039087353, 4.399854319017098, 4.680319126213774, 5.927506880622765, 8.538281200589076, 12.266138445034487, 13.809543069603565, 16.820745079630505, 21.576445190048975, 23.555142365970948, 25.911839557551843, 2.808757370538774, 8.660344040099975, 10.6595453026846, 20.050258313978663, 1.4493841923188184, 2.9922102823176746, 10.144665976622084, 11.625641977204268, 17.90262286399708, 34.7395552145328, 44.45031703619381, 6.081684662113023, 21.091770081033683, 24.952313568577566, 34.7702968726246, 2.934088705573286, 15.006634238546495, 16.401392887767344, 29.82958683130629, 30.088341826225673, 8.417160421598439, 1.3022043980030458, 7.0349316671907225, 8.214984157825558, 14.850358994002711, 16.592568228998775, 17.773021694715535, 19.9949664624289, 30.694775710376874, 34.4892800279326, 37.978142460967675, 4.308649897364187, 6.857283481478644, 11.004096237037679, 16.440991396363568, 18.70211208093526, 22.050912261320907, 25.854277860077463, 26.81435531539019, 2.693130290686156, 14.123639308472068, 18.407664387220862, 23.346960209853492, 24.79948636088113, 25.064714275529152, 12.018309055020522, 13.536329103128121, 15.225471973300316, 18.284353756133278, 20.581062763377552]
3
niko 9 may. 2019 a las 17:18

En R, puede llamar a un elemento específico de una lista entre corchetes dobles.

https://www.tutorialspoint.com/r/r_lists.htm

# Create a list containing strings, numbers, vectors and a logical
# values.
list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1)
print(list_data)

Lo que da

[[1]]
[1] "Red"

[[2]]
[1] "Green"

[[3]]
[1] 21 32 11

[[4]]
[1] TRUE

[[5]]
[1] 51.23

[[6]]
[1] 119.1

Entonces, en R, llamar a list_data[[1]] devolvería [1] "Red"

list_data[[3]] (que es este elemento de la lista es un vector, entonces puede ser subconjunto)

list_data[[3]][2] devolvería 32.

Mi Python-exe no es mi mejor lenguaje, pero creo que puedes escribir

EventTime[i] = [timeList, cutoff[i]]
0
James Holland 9 may. 2019 a las 16:41

El paréntesis doble [[]] en r se usa para subconjuntos de una lista. Debido a que el código r establece "EventTime" como una lista con EventTime = list(), necesita los corchetes dobles para indicar que está cambiando los elementos de la lista, y no los elementos dentro de ellos.

EventTime[[1]] es el primer elemento en la lista "EventTime".

0
adam kemberling 9 may. 2019 a las 16:37