1

我最近开始使用 Python(并且也开始研究 R)。我在 R 中遇到了一个有趣的示例(复制如下以供参考),我想尝试看看我是否可以在 Python 中实现(不使用 rpy 或 Pandas ETC。)。

R 代码示例

# Goal:
#       A stock is traded on 2 exchanges.
#       Price data is missing at random on both exchanges owing to non-trading.
#       We want to make a single price time-series utilising information
#       from both exchanges. I.e., missing data for exchange 1 will
#       be replaced by information for exchange 2 (if observed).
# Let's create some example data for the 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 from each at random.
e1[sample(1:15, 5)] <- NA
e2[sample(1:15, 5)] <- NA
cbind(e1, e2)
# Now how do we reconstruct a time-series that tries to utilise both?
combined <- e1                          # Do use the more liquid exchange here.
missing <- is.na(combined)
combined[missing] <- e2[missing]        # if it's also missing, I don't care.
cbind(e1, e2, combined)

我试过了

import numpy as 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 two vectors

我还没有设法做下一部分,即

# Blow away 5 points from each at random

我确实尝试了 python 的np.random.random_sample命令,但根本无法让它工作。
我非常感谢您在本节和最后一节的帮助,即重建试图利用上述两个数据数组的时间序列。

4

1 回答 1

1

您可以使用“随机”包

import numpy as 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)
于 2013-02-13T18:48:12.567 回答