matlab string array to python numpy - python

I have an array of strings in Matlab and I want to use it with python. I tried to do the same process as for numeric arrays i.e.
np_array = scipy.io.loadmat(mat_array)
which works with numeric data.
But when I try to use a string array like
mat_str_array = [“This”, “is”, “a”, “string”]
I get an error/warning stating
cannot be transformed using the same method
Any suggestions?
Thanks in advance.

Related

Python String representation of numpy array

Iam trying to get a FULL string representation of an 2D float32 512x512 array. I can either use numpys string2array(array) or repr(array). But the problem is that I always get a shortened output like that:
'...[2.0886018e-04 1.7029114e-04 2.8904244e-05 ... 4.1985390e-06
1.4982919e-06 4.7537060e-06]]'
Is there a possibility of full representation?
The answer is that numpy array2string has a threshold option which can be set to np.inf. Then a full representation is possible! :)

Matlab cell array to python list conversion

I have Matlab cell array of size (1,260), but I am unable to convert it into a Python list. My code is as follow:
i=sio.loadmat('bispec.mat')
k=i['a']
After executing the code, I get these entities in the variable explorer.
I am trying to convert the Matlab cell array named k to the Python list by the following code.
val = np.asarray(k, dtype='float').tolist()
But I get the following error.
As shown in one of the images, you have tried:
val = np.asarray(k, dtype='double').tolist().
There should not be inverted commas around double. You should be actually using:
val = np.asarray(k, dtype=np.longdouble).tolist().
Adding further, one more reason for this error to occur is trying to use a string as an element in an array type of double. If you really want to have a NumPy array containing both strings and doubles, you could use the dtype object, which enables the array to hold arbitrary Python objects, as shown.
val = np.asarray(k, dtype=object).tolist().

Arrays of single-element arrays in Python: why?

I came across an oddity when loading a .mat-file created in Matlab into Python with scipy.io.loadmat. I found similar 'array structures' being alluded to in other posts, but none explaining them. Also, I found ways to work around this oddity, but I would like to understand why Python (or scipy.io.loadmat) handles files this way.
Let's say I create a cell in Matlab and save it:
my_data = cell(dim1, dim2);
% Fill my_data with strings and floats...
save('my_data.mat','my_data')
Now I load it into Python:
import scipy.io as sio
data = sio.loadmat('my_data.mat')['my_data']
Now data has type numpy.ndarray and dtype object. When I look at a slice, it might look something like this:
data[0]
>>> array([array(['Some descriptive string'], dtype='<U13'),
array([[3.141592]]), array([[2.71828]]), array([[4.66920]]), etc.
], dtype=object).
Why is this happening? Why does Python/sio.loadmat create an array of single-element arrays, rather than an array of floats (assuming I remove the first column, which contain strings)?
I'm sorry if my question is basic, but I'd really like to understand what seems like an unnecessary complication.
As said in the comments:
This behaviour comes because you are saving a cell, an "array" that can contain anything inside. You fill this with matrices of size 1x1 (floats).
That is what python is giving you. an nparray of dtype=object that has inside 1x1 arrays.
Python is doing exactly what MATLAB was doing. For this example, you should just avoid using cells in MATLAB.

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

concatenating arrays in python like matlab without knowing the size of the output array

I am trying to concatenate arrays in python similar to matlab
array1= zeros(3,500);
array2=ones(3,700);
array=[array1, array2];
I did the following in python:
array1=np.zeros((3,500))
array2=np.ones((3,700))
array=numpy.concatenate((array1, array2), axis=2)
however this gives me different results when i access try to "array[0,:]"
is there a way in python to put arrays in one array similar to matlab.
Thank you
concatenate((a,b),1) or
hstack((a,b)) or
column_stack((a,b)) or
c_[a,b]
From here: Link

Categories

Resources