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

Python 为什么当我运行相同的代码但不同的名称时会得到不同的输出?,python,Python,当我使用print(扩展数据[0:1,0:1,:])运行代码时,我得到了数组的第一行,但当我使用print(state)运行代码时,我得到了一行数组,其中包含扩展数据,但我不知道为什么会得到它 def load_data(test=False): #prices = pd.read_pickle('data/OILWTI_1day.pkl') #prices = pd.read_pickle('data/EURUSD_1day.pkl') #prices.rename(c

当我使用
print(扩展数据[0:1,0:1,:])
运行代码时,我得到了数组的第一行,但当我使用
print(state)
运行代码时,我得到了一行数组,其中包含扩展数据,但我不知道为什么会得到它

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=…
,一切都应该正常工作。

非常感谢您节省了我的时间。