normalized_beat_2d[i] = np.zeros((len(normalized_beat[i]), 2)) normalized_beat_2d[i, 0] = normalized_beat[i] normalized_beat_2d[i, 1] = (i / (len(normalized_beat[i]) - 1)) * 2 - 1 mtf = MarkovTransitionField() image[i] = mtf.transform(normalized_beat_2d[i]) print('normalized_beat_2d[{}] ='.format(i), normalized_beat_2d[i]) print('len(normalized_beat_2d[{}]) ='.format(i), len(normalized_beat_2d[i]))报错Traceback (most recent call last): File "D:\pythonProject_ecg_analyzation\main.py", line 22, in <module> GetBeats.getbeats(r_peaks, ecg_voltage, user_number) File "D:\pythonProject_ecg_analyzation\GetBeats.py", line 53, in getbeats normalized_beat_2d[i, 0] = normalized_beat[i] TypeError: list indices must be integers or slices, not tuple怎么改
时间: 2023-12-10 11:41:49 浏览: 146
这个错误是因为 `normalized_beat_2d` 是一个列表,而在赋值时使用了类似于数组的方式进行索引。需要将 `normalized_beat_2d` 改为一个 NumPy 数组,代码如下:
```
normalized_beat_2d = np.zeros((len(normalized_beat),), dtype=object)
for i in range(len(normalized_beat)):
normalized_beat_2d[i] = np.zeros((len(normalized_beat[i]), 2))
normalized_beat_2d[i][:, 0] = normalized_beat[i]
normalized_beat_2d[i][:, 1] = (i / (len(normalized_beat[i]) - 1)) * 2 - 1
mtf = MarkovTransitionField()
image[i] = mtf.transform(normalized_beat_2d[i])
print('normalized_beat_2d[{}] ='.format(i), normalized_beat_2d[i])
print('len(normalized_beat_2d[{}]) ='.format(i), len(normalized_beat_2d[i]))
```
这里使用了一个大小为 `len(normalized_beat)` 的对象数组 `normalized_beat_2d`,然后在遍历 `normalized_beat` 的过程中,为每个元素赋值一个大小为 `(len(normalized_beat[i]), 2)` 的 NumPy 数组。对这个 NumPy 数组进行赋值时,可以使用 `[:, 0]` 和 `[:, 1]` 分别取出第一列和第二列的值。
阅读全文