【问题标题】:Python data missing at random in two different arrays. Looking to combine these to make a single arrayPython 数据在两个不同的数组中随机丢失。希望将这些组合成一个数组
【发布时间】:2013-01-29 09:36:28
【问题描述】:

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

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 命令,但根本无法让它工作。
非常感谢您在本节和最后一节的帮助,即重建试图利用上述两个数据数组的时间序列。

【问题讨论】:

    标签: python r random numpy nan


    【解决方案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)
    

    【讨论】:

    • 感谢 (Kith) 的回复。我能够完成从 R 到 Python 代码的其余翻译。使用 np.isnan(e1) 函数很容易使用。为 Python 竖起大拇指!
    • 没问题。如果答案有帮助,为什么不点击接受按钮?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多