【问题标题】:x_input=numpy.array(), TypeError: 'int' object is not subscriptablex_input=numpy.array(), TypeError: 'int' object is not subscriptable
【发布时间】:2021-03-25 13:14:55
【问题描述】:

我正在运行 python 代码并遇到该错误。我正在尝试解决它,但我不能,因为我不熟悉 python。因此,请指导我并提出适当的解决方案。我想分别从有和没有 GAN 的两个结果中计算差异分数。 这是有问题的代码部分:

raw_seq = class_instances[:,1]
n_steps = len(class_instances1[:,1])
X, y = split_sequence(raw_seq, n_steps)

n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))

model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')

model.fit(X, y, epochs=200, verbose=0)
model.summary()
x_input = class_instances1[:,1]
x_input = numpy.array(n_steps[6:13])
x_input = x_input.reshape((1, n_steps, n_features))
yhat = model.predict(x_input, verbose=0)
print("\n Result:", yhat, "\n")

这是输出:

Mean Feature Representation (MFR) : 74.47619047619048
Difference of each activity with MFR: 
 [42.47619047619048, 40.47619047619048, -120.52380952380952, -39.52380952380952, -185.52380952380952, 51.47619047619048, 24.47619047619048, 50.47619047619048, -69.52380952380952, 54.47619047619048, 9.476190476190482, 64.47619047619048, -25.52380952380952, 54.47619047619048, 53.47619047619048, -71.52380952380952, 33.47619047619048, 23.47619047619048, 57.47619047619048, -2.5238095238095184, -45.52380952380952]
Model: "sequential_10"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_10 (LSTM)               (None, 50)                10400     
_________________________________________________________________
dense_10 (Dense)             (None, 1)                 51        
=================================================================
Total params: 10,451
Trainable params: 10,451
Non-trainable params: 0
_________________________________________________________________
Traceback (most recent call last):

  File "C:\Users\Lenovo\anaconda3\thesis code\TC_code.py", line 102, in <module>
    x_input = numpy.array(n_steps[6:13])

TypeError: 'int' object is not subscriptable

【问题讨论】:

  • n_steps 是一个用n_steps = len(class_instances1[:,1]) 声明的int 对象,因此您不能将其作为n_steps[6:13] 使用索引,因此会出现错误。
  • 你想用那条线做什么?
  • @MadPhysicist 给一个数组进行整形,然后计算差异分数。在此之后应用生成对抗网络,最后应用 LSTM。
  • 这就是您通常在代码中尝试执行的操作。那条特定线路的目的是什么?我看的越多,它就越没有意义。
  • 通常在这样的问题中,很难说出您从某个教程或其他示例中借用了哪些代码,以及您自己编写了哪些代码。尤其是当您声称自己是 Python 新手,但拥有相对高级的代码(使用 Sequential)时。不管你的意图是什么,n_steps[6:13] 表达式是一个基本的 Python 错误。

标签: python arrays numpy integer lstm


【解决方案1】:

在 python 中,您只能在可迭代对象上使用索引(例如,listsnumpy.arrays 等)。 如错误所述,在以下行中:

x_input = numpy.array(n_steps[6:13])

您尝试索引n_steps 变量,这是一个int,来自n_steps = len(class_instances1[:,1]),这是不允许的。

干杯

【讨论】:

  • 那么,我会用什么来代替 n_steps 变量。你有什么建议?
  • 这一行的目的是什么?
  • 给一个数组进行reshape,然后计算Dissimilarity Score。
  • 如果我理解正确,未知数据位于raw_seq,所以我猜你需要将n_steps[6:13]替换为raw_seq[6:13],但同样,这只是我的猜测,因为我真的不知道您的代码的期望结果是什么。
  • 给出一个数组进行整形,然后计算差异分数。在此之后,应用生成对抗网络,最后应用 LSTM。这是期望的目标。
猜你喜欢
  • 2018-02-05
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-13
  • 1970-01-01
相关资源
最近更新 更多