Domanda

Sono molto nuovo a Python.Ho bisogno di avere una matrice tridimensionale, per risparmiare 8 per 8 matrice in una certa lunghezza.Chiamiamo 530. Il problema è che ho usato NP.Array poiché la matrice non può avere più di 2 dimensioni come argomenti di polpastrello.
. R = zeros([8,8,530],float)
. Ho calcolato la mia 8 per 8 Matrix come NP.Matrix R[:,:,ii] = smallR
. E, quindi cerco di salvarlo nel file mat mentre Scipy afferma di farlo.
sio.savemat('R.mat',R)
. Tuttavia, l'errore dice che l'oggetto 'Numpy.ndarray' non ha attributo 'articoli'

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

È stato utile?

Soluzione

Se digiti help(sio.savemat), vedi:

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.
.

E così anche se non si riconosce .items() come metodo di dizionario, è chiaro che dovremo usare un dizionario (un set di tasti, coppie di valori; tutorial di Google "Python Dictionary" se necessario). .

In questo caso:

>>> 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]]])
.

Fondamentalmente, un dizionario viene utilizzato in modo che gli array possano essere nominati, poiché è possibile memorizzare più oggetti in un file .mat.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top