I am very new to Python. I need to have a 3 dimensional matrix, in order to save 8 by 8 matrix in some length. Let’s call 530. The problem is that I used np.array since matrix cannot have more than 2 dimensions as numpy argues.
R = zeros([8,8,530],float)
I calculated my 8 by 8 matrix as a np.matrix
R[:,:,ii] = smallR
And, then I try to save it in mat file as scipy claims to do so.
sio.savemat('R.mat',R)
However, error says ‘numpy.ndarray’ object has no attribute ‘items’
/usr/local/lib/python2.7/dist-packages/scipy/io/matlab/mio.py:266: FutureWarning: Using oned_as default value ('column') This will change to 'row' in future versions
oned_as=oned_as)
Traceback (most recent call last):
File "ClassName.py", line 83, in <module>
print (buildR()[1])
File "ClassName.py", line 81, in buildR
sio.savemat('R.mat',R)
File "/usr/local/lib/python2.7/dist-packages/scipy/io/matlab/mio.py", line 269, in savemat
MW.put_variables(mdict)
File "/usr/local/lib/python2.7/dist-packages/scipy/io/matlab/mio5.py", line 827, in put_variables
for name, var in mdict.items():
AttributeError: 'numpy.ndarray' object has no attribute 'items'
If you type
help(sio.savemat), you see:and so even if you don’t recognize
.items()as a dictionary method, it’s clear we’re going to need to use a dictionary (a set of key, value pairs; google “python dictionary tutorial” if necessary).In this case:
Basically, a dictionary is used so that the arrays can be named, as you can store multiple objects in one .mat file.