1

私は最近 Python を使い始めました (R も調べ始めました)。R で興味深い例 (参照用に以下にコピー) に出くわしました。等。)。

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 に答える