0

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

1 Answer 1

2

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']
Sign up to request clarification or add additional context in comments.

9 Comments

So in your line there, is matrix the same as 'name of matrix'? So the matrix that I've imported from Matlab used the scipy.io.loadmat is called matTarget. The matrix that I'm trying to create is just target. So if I write the code according to your line there I get 'target=matTarget['target']' but how does that work if target is being defined as something of itself?
@aksharagarwal No, 'name of matrix' is a string literal which you should replace with the actual name of the matrix in your Matlab file. matrix is a variable which stores a reference to the matrix returned by matTarget[...]. matTarget is a dictionary which contains the matrices' names as keys and the matrices themselves as values. Can you post the output of print(matTarget.keys()) (maybe as an extension to your question)?
I added that output to the question. So should I run 'target2=matTarget('y_train') and then define target as np.asarray(target2)? Doing that gets me the error that dict is not callable
@aksharagarwal Yes but you need to replace the parentheses ( ) with square brackets [ ] because you want to __getitem__ rather than __call__ the dictionary.
Sorry, I just fixed that and ran it with the brackets. Also, I just wanted to thank you for helping me out thus far. I took another look at the documentation for the import, and there is a 'struct_as_record' option so that would load the Matlab struct as a numpy record array. Would that work also?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.