簡體   English   中英

為什么運行相同的代碼但使用不同的名稱時會得到不同的輸出?

[英]Why I get different output when I run the same code but different name?

當我使用print(xdata[0:1, 0:1, :])運行我的代碼時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, :])

您沒有以合理的方式存儲返回值。 您將所有三個值存儲到A ,然后訪問xdata 目前尚不清楚為什么后者甚至是在全局范圍內定義的,如果是的話,那只是其他地方的一些遺留問題。 使用state, xdata, close = init_state(...)而不是A = ... ,一切都應該像預期的那樣工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM