我遇到了问题。 我在python中调用自构建函数。在函数中,我返回一个数组。我最好显示代码,因为它很难解释。 这是代码:
=transpose(unique(query(A2:C,"Select B where A = "&F1&" Order by B Asc")))
所以x数据看起来像这样:
x = []
y = []
for seq, target in sequential_data:
x.append(seq)
y.append(target)
return np.array(x), y
y看起来像这样:
array([[-6.32800918e-01, 2.89825783e+00, -1.73310681e-01,
-4.15661278e-01, -9.33079353e-02, -4.58912725e-01],
[ 1.07589222e+00, -3.94481134e-01, -1.95411356e+00,
3.33890284e-01, 8.47644383e-01, 3.54376319e+00],
[-2.83976404e-02, -4.19161683e-01, 2.38210386e-01,
-3.94514913e-01, -6.06152332e-01, -9.27321083e-01]]), array([[ 0.09504018, -0.12478065, -0.84749427, -0.41267553, 0.03296391,
-0.31431189],
[ 1.48297783, 0.45790544, 1.72273948, 0.71392208, 0.1164553 ,
1.24876302],
[-1.54352725, 0.06401262, -0.44148997, -0.36444495, 0.2416613 ,
-0.6424675 ],
[ 0.62600517, -0.36715788, 0.09876015, -0.30232469, 0.82585385,
-0.96059436]])
此数据是函数返回的内容。所以我想做的就是为x数组分配一个变量名称,为y数组分配一个变量名称。我这样尝试过:
[0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0]
输入具有正确的值,所以那不是问题。
但是当我执行validation_x, validation_y = function(Input)
时,它将返回0。validation_y也是如此。那我在做什么错了?
全功能:
len(validation_x)
全功能呼叫:
def preprocess_df(df):
for i in df.values:
prev_days.append([n for n in i[:-1]])
if len(prev_days) == SEQ_LEN:
sequential_data.append([np.array(prev_days), i[-1]])
random.shuffle(sequential_data)
buys = []
sells = []
for seq, target in sequential_data:
if target == 0:
sells.append([seq, target])
elif target == 1:
buys.append([seq, target])
random.shuffle(buys)
random.shuffle(sells)
lower = min(len(buys), len(sells))
buys = buys[:lower]
sells = sells[:lower]
sequential_data = buys+sells
random.shuffle(sequential_data)
x = []
y = []
for seq, target in sequential_data:
x.append(seq)
y.append(target)
return np.array(x), y
变量:Validation_main_df
times = sorted(main_df.index.values)
last_5pct = times[-int(0.05*len(times))]
validation_main_df = main_df[(main_df.index >= last_5pct)]
main_df = main_df[(main_df.index < last_5pct)]
train_x, train_y = preprocess_df(main_df)
validation_x, validation_y = preprocess_df(validation_main_df)
print("train data: " + str(len(train_x)) + ", validation: " + str(len(validation_x)))
答案 0 :(得分:0)
使用您提供的详细信息,我无法重现此问题:
import numpy as np
def function(sequential_data):
x = []
y = []
for seq, target in sequential_data:
x.append(seq)
y.append(target)
return np.array(x), y
Input = [[1, [1, 2, 3]], [2, [2, 3, 4]]]
validation_x, validation_y = function(Input)
print(len(validation_x))
print(len(validation_y))
输出
2
2
符合预期。