当我尝试重塑时,它给了我一个错误,说"TypeError: list indices must be integers or slices, not tuple"



实际上,我正在尝试将 Matlab 代码转换为 python,当我尝试重塑时,它给我抛出了一个 TypeError 说"TypeError:列表索引必须是整数或切片,而不是元组"。

马特实验室

[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);

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)

错误

回溯(最近一次调用(: 文件"ppp.py",第 26 行,在 j1 = np.reshape(n1[1: ,1], no_bin_ele[1]*7+4, 360*1( 类型错误:列表索引必须是整数或切片,而不是元组

检查此 MATLAB 行:

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

在八度中,我验证

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]

转换m5tolist()无济于事。 对于列表*表示复制,+表示联接。 对于数组,这些是乘法和加法。 完全不同。

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 个字节的数字,我们使用其中的第一个来选择一片m6进行重塑。

如果我们有此文件格式的文本描述,则进行此翻译可能会更容易。 在没有样本或描述的情况下推断 matlab 行为可能是有问题的。

请尝试将第二个和第三个参数括在括号中,将其压缩为 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

相关内容

最新更新