怎么把normalized_beat_2d[i] = np.reshape(normalized_beat[i], (-1, 1))改成转换为2列的二维数组且第二列数据是归一化到-1,1元素序数
时间: 2023-12-10 08:41:42 浏览: 37
可以将代码改为如下形式:
```
normalized_beat_2d = np.zeros((len(normalized_beat), 2))
for i in range(len(normalized_beat)):
normalized_beat_2d[i, 0] = normalized_beat[i]
normalized_beat_2d[i, 1] = (i / (len(normalized_beat) - 1)) * 2 - 1
```
这里我们先创建一个形状为 (len(normalized_beat), 2) 的全零数组 `normalized_beat_2d`,然后使用循环遍历 `normalized_beat` 中的每个元素。对于每个元素,我们把它放到 `normalized_beat_2d` 的第一列,然后计算它在序列中的位置,将它归一化到 -1 到 1 的范围内并放到 `normalized_beat_2d` 的第二列。最终得到的 `normalized_beat_2d` 就是所需的形式。
相关问题
怎么把normalized_beat_2d[i] = np.reshape(normalized_beat[i], (-1, 1))改成转换为2列的二维数组且第二列数据归一化到-1,1
可以使用以下代码将 `normalized_beat[i]` 转换为两列的二维数组,并将第二列数据归一化到 -1 和 1 之间:
```
normalized_beat_2d = np.zeros((normalized_beat[i].shape[0], 2))
normalized_beat_2d[:, 0] = normalized_beat[i]
normalized_beat_2d[:, 1] = 2 * (normalized_beat_2d[:, 0] - np.min(normalized_beat_2d[:, 0])) / (np.max(normalized_beat_2d[:, 0]) - np.min(normalized_beat_2d[:, 0])) - 1
```
首先,我们创建一个大小为 `(normalized_beat[i].shape[0], 2)` 的零矩阵 `normalized_beat_2d`,其中第一列将包含原始归一化数据,第二列将包含归一化到 -1 和 1 之间的数据。然后,我们将原始归一化数据复制到 `normalized_beat_2d` 的第一列中。接下来,我们使用最大值和最小值的归一化公式将第一列数据归一化到 -1 和 1 之间,并将结果存储在第二列中。
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怎么改
这个错误是因为 `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]` 分别取出第一列和第二列的值。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)