为什么我运行相同的代码但名称不同时会得到不同的输出?

时间:2017-09-05 13:02:05

标签: python

当我使用print(xdata[0:1, 0:1, :])运行我的代码时,我得到了第一行数组但是当我使用print(state)运行时,我得到了一行包含在xdata中的数组,但我不知道为什么我知道了。

def load_data(test=False):
    #prices = pd.read_pickle('data/OILWTI_1day.pkl')
    #prices = pd.read_pickle('data/EURUSD_1day.pkl')
    #prices.rename(columns={'Value': 'close'}, inplace=True)
    prices = pd.read_pickle('D:\data/XBTEUR_1day.pkl')
    prices.rename(columns={'Open': 'open', 'High': 'high', 'Low': 'low', 'Close': 'close', 'Volume (BTC)': 'volume'}, inplace=True)

    x_train = prices.iloc[-2000:-300,]
    x_test= prices.iloc[-2000:,]
    if test:
        return x_test
    else:
        return x_train

def init_state(indata, test=False):
    close = indata['close'].values
    diff = np.diff(close)
    diff = np.insert(diff, 0, 0)
    sma15 = SMA(indata, timeperiod=15)
    sma60 = SMA(indata, timeperiod=60)
    rsi = RSI(indata, timeperiod=14)
    atr = ATR(indata, timeperiod=14)

    #--- Preprocess data
    xdata = np.column_stack((close, diff, sma15, close-sma15, sma15-sma60, rsi, atr))

    xdata = np.nan_to_num(xdata)
    if test == False:
        scaler = preprocessing.StandardScaler()
        xdata = np.expand_dims(scaler.fit_transform(xdata), axis=1)
        joblib.dump(scaler, 'D:\data/scaler.pkl')
    elif test == True:
        scaler = joblib.load('D:\data/scaler.pkl')
        xdata = np.expand_dims(scaler.fit_transform(xdata), axis=1)
    state = xdata[0:1, 0:1, :]

    return state, xdata, close

indata = load_data(test=True)
A = init_state(indata, test=False)
print(xdata[0:1, 0:1, :])

1 个答案:

答案 0 :(得分:1)

您没有以合理的方式存储您的返回值。您将所有三个值存储到A,然后访问xdata。目前还不清楚为什么后者甚至在全球范围内定义,如果是,它只是其他地方的遗留问题。使用state, xdata, close = init_state(...)代替A = ...,一切都应该像预期的那样工作。