Saturday, 15 August 2015

r - Python data missing at random in two different arrays. Looking to combine these to make a single array -



r - Python data missing at random in two different arrays. Looking to combine these to make a single array -

i've started using python (and have started looking @ r).i came across interesting illustration (copied below reference) in r wanted seek , see if implement in python (without using rpy or pandas etc.).

r code-example

# goal: # stock traded on 2 exchanges. # cost info missing @ random on both exchanges owing non-trading. # want create single cost time-series utilising info # both exchanges. i.e., missing info exchange 1 # replaced info exchange 2 (if observed). # let's create illustration info problem. e1 <- runif(15) # prices on exchange 1 e2 <- e1 + 0.05*rnorm(15) # prices on exchange 2. cbind(e1, e2) # blow away 5 points each @ random. e1[sample(1:15, 5)] <- na e2[sample(1:15, 5)] <- na cbind(e1, e2) # how reconstruct time-series tries utilise both? combined <- e1 # utilize more liquid exchange here. missing <- is.na(combined) combined[missing] <- e2[missing] # if it's missing, don't care. cbind(e1, e2, combined)

i have tried

import numpy np e1=np.random.random((15,)).reshape(-1,1) e2=e1+0.05*np.random.standard_normal(15).reshape(-1,1) np.concatenate((e1,e2),axis=1) # cbind equivalent on 2 vectors

i have not managed next section i.e.

# blow away 5 points each @ random

i did seek python's np.random.random_sample command not work @ all. much appreciate assistance please , lastly section i.e. reconstructing timeseries tries utilise both info arrays described above.

you can utilize "random" package

import numpy np import random e1=np.random.random((15,)).reshape(-1,1) e2=e1+0.05*np.random.standard_normal(15).reshape(-1,1) e1[random.sample(range(e1.shape[0]), 5),:] = np.nan e2[random.sample(range(e2.shape[0]), 5),:] = np.nan np.concatenate((e1,e2),axis=1)

python r random numpy nan

No comments:

Post a Comment