AttributeError: 'numpy.ndarray' object has no attribute 'append' - Something escapes my logic
AttributeError: 'numpy.ndarray' object has no attribute 'append' - Something escapes my logic
我正在尝试 运行 基于所用方法的一组数据的决策树算法 here。
我已经清理了数据,根据 medium 中的示例,一切正常。
我正在尝试定义函数:
def datasets (df, x_len = 12, y_len = 1, y_test_len = 12):
D = df.values
periods = D.shape[1]
#Training Dataset Creation
loops = periods + 1 - x_len - y_len - y_test_len
train = []
for col in range(loops):
train.append(D[:,col:col+x_len+y_len])
train = np.vstack(train)
X_train, Y_train = np.split(train, [x_len], axis = 1)
#Test set creation
max_col_test = periods - x_len - y_len + 1
test = []
for col in range(loops, max_col_test):
test.append(D[:,col:col+x_len+ y_len])
test = np.vstack(test)
X_test, Y_test = np.split(test, [x_len], axis = 1)
训练数据集创建工作正常,但在测试集创建代码段中出现此错误:
AttributeError: 'numpy.ndarray' object has no attribute 'append'
我不明白它发生了什么,因为在第一个循环中它工作得很好,所以显然有些东西逃避了我的逻辑。
for col in range(loops):
train.append(D[:,col:col+x_len+y_len])
train = np.vstack(train)
X_train, Y_train = np.split(train, [x_len], axis = 1)
错误在上面的代码中。
在 train = np.vstack(train)
之后,train
变成了一个 numpy 数组,而不是一个列表,所以在下一次迭代中,你将在一个没有 [= 的 numpy 数组上附加一个值13=] 方法,因此出现错误。
我正在尝试 运行 基于所用方法的一组数据的决策树算法 here。
我已经清理了数据,根据 medium 中的示例,一切正常。
我正在尝试定义函数:
def datasets (df, x_len = 12, y_len = 1, y_test_len = 12):
D = df.values
periods = D.shape[1]
#Training Dataset Creation
loops = periods + 1 - x_len - y_len - y_test_len
train = []
for col in range(loops):
train.append(D[:,col:col+x_len+y_len])
train = np.vstack(train)
X_train, Y_train = np.split(train, [x_len], axis = 1)
#Test set creation
max_col_test = periods - x_len - y_len + 1
test = []
for col in range(loops, max_col_test):
test.append(D[:,col:col+x_len+ y_len])
test = np.vstack(test)
X_test, Y_test = np.split(test, [x_len], axis = 1)
训练数据集创建工作正常,但在测试集创建代码段中出现此错误:
AttributeError: 'numpy.ndarray' object has no attribute 'append'
我不明白它发生了什么,因为在第一个循环中它工作得很好,所以显然有些东西逃避了我的逻辑。
for col in range(loops):
train.append(D[:,col:col+x_len+y_len])
train = np.vstack(train)
X_train, Y_train = np.split(train, [x_len], axis = 1)
错误在上面的代码中。
在 train = np.vstack(train)
之后,train
变成了一个 numpy 数组,而不是一个列表,所以在下一次迭代中,你将在一个没有 [= 的 numpy 数组上附加一个值13=] 方法,因此出现错误。