Frage

Ich bin sehr neu in Python.Ich benötige eine dreidimensionale Matrix, um eine 8 x 8-Matrix in einer gewissen Länge zu speichern.Rufen wir 530 an.Das Problem ist, dass ich np.array verwendet habe, da die Matrix nicht mehr als zwei Dimensionen haben kann, wie Numpy argumentiert.
R = zeros([8,8,530],float)
Ich habe meine 8 x 8-Matrix als np.matrix berechnet
R[:,:,ii] = smallR
Und dann versuche ich, es in einer Mat-Datei zu speichern, wie Scipy behauptet, dies zu tun.
sio.savemat('R.mat',R)
Der Fehler besagt jedoch, dass das Objekt „numpy.ndarray“ kein Attribut „items“ hat.

/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'

War es hilfreich?

Lösung

Wenn Sie tippen help(sio.savemat), Sie sehen:

savemat(file_name, mdict, appendmat=True, format='5', long_field_names=False, do_compression=False, oned_as=None)
    Save a dictionary of names and arrays into a MATLAB-style .mat file.
[...]
    mdict : dict
        Dictionary from which to save matfile variables.

und das auch, wenn du es nicht erkennst .items() Es ist klar, dass wir als Wörterbuchmethode ein Wörterbuch (eine Reihe von Schlüssel-Wert-Paaren) verwenden müssen.Google „Python-Wörterbuch-Tutorial“, falls erforderlich).

In diesem Fall:

>>> from numpy import zeros
>>> from scipy import io as sio
>>> 
>>> R = zeros([8,8,530],float)
>>> R += 12.3
>>> 
>>> sio.savemat('R.mat', {'R': R})
>>> 
>>> S = sio.loadmat('R.mat')
>>> S
{'R': array([[[ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
        [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
        [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
        ..., 

        ..., 
        [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
        [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
        [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3]]]), '__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file Platform: posix, Created on: Sat Feb 25 18:16:02 2012', '__globals__': []}
>>> S['R']
array([[[ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
        [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
        [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
        ..., 

        ..., 
        [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
        [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3],
        [ 12.3,  12.3,  12.3, ...,  12.3,  12.3,  12.3]]])

Grundsätzlich wird ein Wörterbuch verwendet, damit die Arrays benannt werden können, da Sie mehrere Objekte in einer .mat-Datei speichern können.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top