a13a22
a13a22

Reputation: 133

How do I convert a Matlab matrix to a python array

I have a 100x200 input, and a 1x100 target matrix that I am using to run a gridsearch and create a classifier in python. However, I get errors that my training set of target data is not an array. I've tried:

target=np.asarray(matTarget)

Where the matTarget is just my target imported from Matlab using scipy.io.loadmat.

My exact error is

len() of unsized object

When I try target.size I get a blank size as well.

If I do not do the array conversion, then I get

Expected array-like (array or non string sequence) got {'_header_': b'Matlab matfile ... Array([[1],[1]...)}

I still have the original matrix in Matlab and have also tried using np.array instead of asarray.

If I do print(matTarget.keys()) then I get ('header`,'version','globals','y_train'])

y_train is the name of the mat file itself

Upvotes: 0

Views: 792

Answers (1)

a_guest
a_guest

Reputation: 36329

According to the documentation of scipy.io.loadmat it returns a dictionary where the values are the contained matrices.

Returns: mat_dict : dict

dictionary with variable names as keys, and loaded matrices as values.

So you need to select your matrix by its name before using it with numpy:

matrix = matTarget['name of matrix']

Upvotes: 2

Related Questions