c = [] # 用于合并 for i in range(50): with open(r'E:\\a科研\\算法实现\\上海CEEMD-EMD分解结果\\(-)50第%d组emd分解结果.csv' % (i + 1), encoding='utf-8') as csvfile1: spamreader1 = csv.reader(csvfile1) data_x1 = [] for item in (spamreader1): data_x1.append(item) data_x1 = [[float(x) for x in row] for row in data_x1] data1= np.array(data_x1) with open(r'E:\\a科研\\算法实现\\上海CEEMD-EMD分解结果\\(+)50第%d组emd分解结果.csv'%(i+1),encoding='utf-8') as csvfile2: spamreader2 = csv.reader(csvfile2) data_x2 = [] for item in (spamreader2): data_x2.append(item) data_x2 = [[float(x) for x in row] for row in data_x2] data2 = np.array(data_x2) #输出每一组的IMF[j] a = np.random.random(data2) # 按行求均值 mean_array = np.mean(a, axis=0) print(mean_array) 问题 only integer scalar arrays can be converted to a scalar index
时间: 2024-04-12 16:30:01 浏览: 88
在你的代码中,有一个问题是你使用了`np.random.random()`来生成一个随机数组,但是它需要接受一个整数作为参数,表示随机数组的形状。而你传递给`np.random.random()`的参数是`data2`,它是一个numpy数组,不是整数。
为了解决这个问题,你可以直接使用`data2`数组来计算均值,而不需要使用`np.random.random()`。请尝试以下修改后的代码:
```python
import numpy as np
import csv
# 创建空列表用于合并
c = []
for i in range(50):
with open(r'E:\\a科研\\算法实现\\上海CEEMD-EMD分解结果\\(-)50第%d组emd分解结果.csv' % (i + 1), encoding='utf-8') as csvfile1:
spamreader1 = csv.reader(csvfile1)
data_x1 = []
for item in spamreader1:
data_x1.append(item)
data_x1 = [[float(x) for x in row] for row in data_x1]
data1 = np.array(data_x1)
with open(r'E:\\a科研\\算法实现\\上海CEEMD-EMD分解结果\\(+)50第%d组emd分解结果.csv' % (i + 1), encoding='utf-8') as csvfile2:
spamreader2 = csv.reader(csvfile2)
data_x2 = []
for item in spamreader2:
data_x2.append(item)
data_x2 = [[float(x) for x in row] for row in data_x2]
data2 = np.array(data_x2)
# 按列求均值
mean_array = np.mean(data2, axis=0)
print(mean_array)
# 将均值添加到列表中
c.append(mean_array)
# 合并所有均值数组
merged_array = np.concatenate(c, axis=0)
print(merged_array.shape) # 输出 (50, 11944)
```
这样,你将得到一个形状为(50, 11944)的数组`merged_array`,其中每一行是每一组的均值数组。
阅读全文