重塑多个数组后的累积问题
accumulation problem after reshaping multiple arrays
我有x_train,这是一个属于数据波形的数组,维度为(475,1501),我希望最终输出(seg2)为(1425,500)。我尝试了以下代码:
count=0
sega=[]
seg2=[]
for i in range (0,len(x_train)):
sega = x_train[i][:(x_train[i].size // 500) * 500].reshape(-1, 500)
seg2[count:(count+1),500] = sega
count = count + i
但它报错如下:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-137-72281c805a83> in <module>
10 sega = x_train[i][:(x_train[i].size // 500) * 500].reshape(-1, 500)
11 print(sega.shape)
---> 12 seg2[count:(count+1),500] = sega
13 count = count + i
14
TypeError: list indices must be integers or slices, not tuple
我该如何解决这个错误?
seg2
是一个 list
。看起来您需要将其声明为 np.array
。像这样:
seg2 = np.zeros((total_count, 500))
其中 total_count=1425
.
你也可以这样使用np.concatenate
:
seg2 = np.concatenate([
x_train[i][:(x_train[i].size // 500) * 500].reshape(-1, 500)
for i in range(0,x_train.shape[0])
], axis=0)
我有x_train,这是一个属于数据波形的数组,维度为(475,1501),我希望最终输出(seg2)为(1425,500)。我尝试了以下代码:
count=0
sega=[]
seg2=[]
for i in range (0,len(x_train)):
sega = x_train[i][:(x_train[i].size // 500) * 500].reshape(-1, 500)
seg2[count:(count+1),500] = sega
count = count + i
但它报错如下:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-137-72281c805a83> in <module>
10 sega = x_train[i][:(x_train[i].size // 500) * 500].reshape(-1, 500)
11 print(sega.shape)
---> 12 seg2[count:(count+1),500] = sega
13 count = count + i
14
TypeError: list indices must be integers or slices, not tuple
我该如何解决这个错误?
seg2
是一个 list
。看起来您需要将其声明为 np.array
。像这样:
seg2 = np.zeros((total_count, 500))
其中 total_count=1425
.
你也可以这样使用np.concatenate
:
seg2 = np.concatenate([
x_train[i][:(x_train[i].size // 500) * 500].reshape(-1, 500)
for i in range(0,x_train.shape[0])
], axis=0)