Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'numpy.ndarray' object has no attribute 'items'

Tags:

python

numpy

For the following code, I am getting the error I have put in title:

import scipy.io as sio
import numpy as np

temp = np.load('temp.npy')
sio.savemat('final.mat',temp)

Although AttributeError is a common error in python, I did not find anything useful for 'items' as mentioned in the title. How can we fix this?

like image 519
A.M. Avatar asked Jun 29 '15 22:06

A.M.


People also ask

How to fix NumPy Ndarray object has no Attribute append?

This error occurs when you attempt to append one or more values to the end of a NumPy array by using the append() function in regular Python. Since NumPy doesn't have an append attribute, an error is thrown. To fix this, you must use np. append() instead.

How do I change Ndarray to list?

To convert a NumPy array (ndarray) to a Python list use ndarray. tolist() function, this doesn't take any parameters and returns a python list for an array. While converting to a list, it converts the items to the nearest compatible built-in Python type.

What is NumPy Ndarray in Python?

The main data structure in NumPy is the ndarray, which is a shorthand name for N-dimensional array. When working with NumPy, data in an ndarray is simply referred to as an array. It is a fixed-sized array in memory that contains data of the same type, such as integers or floating point values.


1 Answers

It takes a dict as the second argument not an array:

From the docs:

mdict : dict

Dictionary from which to save matfile variables.

I am not overly familiar but I imagine you pass the name as the key and the array as the value, something like:

sio.savemat('final.mat',{"foo":temp})
like image 72
Padraic Cunningham Avatar answered Nov 15 '22 01:11

Padraic Cunningham