When i try to reshape, it gives me an error saying "TypeError: list indices must be integers or slices, not tuple"

When i try to reshape, it gives me an error saying "TypeError: list indices must be integers or slices, not tuple"

实际上,我正在尝试将 Matlab 代码转换为 python,当我尝试重塑时,它抛出一个 TypeError 说 "TypeError: list indices must be integers or slices, not tuple"。

Matlab

[file,path] = uigetfile('*.dwr');
fid = fopen(strcat(path,'/',file));
m5 = (fread(fid, '*uint8'));
m5=double(m5);
fclose(fid);
m6=m5(12514:end);
no_bin_ele=m5(12039:2:12218)+256*m5(12040:2:12218);
s1=size(m6);
s2=((no_bin_ele(1)*7+4)*360)*1;
n1=m6(1:s2);
j1=reshape(n1(1:end,1),no_bin_ele(1)*7+4,360*1);

Python

import numpy as np
with open('aa.dwr', 'rb') as fp:
m5 = np.fromstring(fp.read(), dtype='uint8')
m5 = m5.astype(float)
m5 = m5.tolist()
m6 = m5[12514:]
no_bin_ele = m5[12039:12218:2]+256*m5[12040:12218:2]
s1 = len(m6)
s2=((no_bin_ele[1]*7+4)*360)*1
s2 =int(s2)
n1=m6[1:s2]
j1 = np.reshape(n1[1: ,1], no_bin_ele[1]*7+4, 360*1)

错误

Traceback (most recent call last): File "ppp.py", line 26, in j1 = np.reshape(n1[1: ,1], no_bin_ele[1]*7+4, 360*1) TypeError: list indices must be integers or slices, not tuple

请尝试将第二个和第三个参数括在括号中,将其压缩为 1:

j1 = np.reshape(n1[1: ,1], (no_bin_ele[1]*7+4, 360*1))

如下所示:https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html 希望对你有帮助。

Python 等效代码。我无权访问 'aa.dwr',因此请检查来自 matlab 的 j1 是否等于来自以下代码的 j1。

import numpy as np
with open('aa.dwr', 'rb') as fp:
    m5 = np.fromstring(fp.read(), dtype='uint8')
    m5 = m5.astype(float)
    m5 = m5.tolist()
    m6 = m5[12513:]
    no_bin_ele = m5[12038:12219:2]+256*m5[12039:12219:2]
    s1 = len(m6)
    s2=((no_bin_ele[0]*7+4)*360)*1
    s2 =int(s2)
    n1=m6[:s2+1]
    j1 = np.reshape(n1[0: ,0], (no_bin_ele[0]*7+4, 360*1))

这重现了您的错误消息:

In [432]: [1,2,3][1:,1]                                                         
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-432-1613afcfe2a2> in <module>
----> 1 [1,2,3][1:,1]

TypeError: list indices must be integers or slices, not tuple

这意味着在

j1 = np.reshape(n1[1: ,1], no_bin_ele[1]*7+4, 360*1)

n1 是一个列表,您使用的是 numpy 数组样式索引。

尝试 n1.shape 时出现同样的问题 - 列表没有 shape

n1 派生自 m6,后者来自 m5,后者来自 tolist() 方法!

在 MATLAB 中,一切都是矩阵(单元格和结构除外)。在 Python 列表中是最接近的,但是加上 numpy,你得到的数组更像 MATLAB - 除了它们的维数可以是 0,1,2 等。调试时要注意到变量的 type,如果是数组,shapedtype.

检查此 MATLAB 行:

no_bin_ele=m5(12039:2:12218)+256*m5(12040:2:12218);

在 Octave 中我验证

12039:2:12218

产生 90 个值,从 12039 到 12217。

12040:2:12218 

还产生90、12040到12218

所以这条线正在对连续的对求和,m5[i]+256*m5[i+1] 因为它们被加载为 uint8,所以我认为这是一个 uint16 值。

但是在numpy:

In [467]: np.arange(12039,12218,2).shape                                        
Out[467]: (90,)
In [468]: np.arange(12040,12218,2).shape                                        
Out[468]: (89,)

端点处理不同。第二个切片结束点应该是 12219.

m5 是一个数组(它应该是)时,这解释了广播错误:

no_bin_ele = m5[12039:12218:2]+256*m5[12040:12218:2]

转换 m5 tolist() 没有帮助。对于列表,* 表示复制,+ 表示加入。对于数组,它们是乘法和加法。完全不同。

In [475]: alist = list(range(0,10))                                             
In [476]: alist                                                                 
Out[476]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [477]: alist[1:6:2] + 4*alist[2:6:2]                                         
Out[477]: [1, 3, 5, 2, 4, 2, 4, 2, 4, 2, 4]

其余代码与列表一起运行,因为索引和切片是相同的 - 直到 n1[1: ,1] 表达式。这仅对 numpy 数组有效。

实际上,还有其他索引问题。 Python 索引从 0 开始。

no_bin_ele(1)     # 1st element of the matlab matrix
no_bin_ele[0]     # 1st element of the array

n1(1:end,1)       # matlab matrix is 2d
n1[1: ,1]         # n1 isn't 2d
n1                # should just be

其实我觉得最后几行应该是

s2=int(no_bin_ele[0]*7+4)*360)
n1=m6[:s2]
j1 = np.reshape(n1, (-1, 360))    # -1 stands in for no_bin_ele[0]*7+4

尽管此重塑可能存在 order 问题。 MATLAB 是列优先的,如 order='F',尾随维度最外层。

我真的很想看一些示例数据来验证这些步骤。仅通过阅读代码我可以推断出的东西是有限的。但我对处理 12218+ 字节长的数据不感兴趣。

同样,八度样本:

>> n1 = 1:10;
>> reshape(n1, 5,2)
ans =

    1    6
    2    7
    3    8
    4    9
    5   10

和 numpy:

In [481]: n1 = np.arange(1,11)                                                  
In [482]: np.reshape(n1, (5,2))                                                 
Out[482]: 
array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10]])
In [483]: np.reshape(n1, (5,2),order='F')                                       
Out[483]: 
array([[ 1,  6],
       [ 2,  7],
       [ 3,  8],
       [ 4,  9],
       [ 5, 10]])
In [484]: np.reshape(n1, (2,5))                                                 
Out[484]: 
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10]])

===

m5 是文件,读取为 uint8,无符号​​字节。

m6 是一个很大的尾随部分,我们要将其重塑为 (n,360) 矩阵(或其转置)。

no_bin_ele 是较早的部分,显然是 2 个字节的数字,我们使用其中的第一个 select 一片 m6 进行整形。

如果我们有此文件格式的文本描述,进行此翻译可能会更容易。在没有示例或描述的情况下推导 matlab 行为可能会出现错误。