Matlab cell array to python list conversion - python

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

Related

numpy.absolute() whit array type input data

I'm trying to convert an array of complex numbers to its corresponding numpy.absolute() value.
The task seems easy but I'm getting an error while storing the result into an output array.
The source array is ok, and looks like:
print(Impedance)
Out: [(0.0007800000603310764+0.0014299999456852674j), (0.0008200000156648457+0.001509999972768128j)]
When I print:
print(np.absolute(Impedance))
Out: [1.62889531e-03 1.71828401e-03]
So far so good, but I note that in the second output there are no commas in the array representation.... I've read through different forum questions that this can be related to the way numpy represents arrays..... I'm a Python 'roockie' so I just assume that this is the way it is ....
The issue comes when I try to store the output into an Array:
Imped_AngD = float(np.angle(Impedance,deg=True)) # angle in degrees
Then I get the following error:
TypeError: only size-1 arrays can be converted to Python scalars
any clues¿?¿?
Thanks in advance
Eneko

Python IndexError for slicing the array

I have an array that I would like to take a portion of each element of the array.
id = [000222000,000333000,000444000]
id2 = [222,333,444]
In order to be able to get id2 array I am using a for loop as follows:
id2 = [i[3:5] for i in id]
However, I get an IndexError: invalid index to a scalar variable. I don't understand why am I getting this error and how to overcome it? Also is it a same principle if I have array of strings instead of numbers?
The problem is that you can't slice numbers in python. If you want to slice the elements, you could cast everything to a string, slice them, then cast everything back to an integer.
You are trying to slice integers, which is not possible as of right now in python 3.x
P.S
-It is not a good practice to name your list id as it is a used function in python.
-Try changing everything to Strings if you want to slice.. + change it from [3:5] to [3:6] as it does not include the last index, so you are only selecting two digits and not 3 as I suppose you want..
- It is not possible to have leading zeros in decimal base in python.

matlab string array to python numpy

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.

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

How to read NetCDF variable float data into a Numpy array with the same precision and scale as the original NetCDF float values?

I have a NetCDF file which contains a variable with float values with precision/scale == 7/2, i.e. there are possible values from -99999.99 to 99999.99.
When I take a slice of the values from the NetCDF variable and look at them in in my debugger I see that the values I now have in my array have more precision/scale than what I see in the original NetCDF. For example when I look at the values in the ToosUI/ncdump viewer they display as '-99999.99' or '12.45' but when I look at the values in the slice array they look like '-99999.9921875' (a greater scale length). So if I'm using '-99999.99' as the expected value to indicate a missing data point then I won't get a match with what gets pulled into the slice array since those values have a greater scale length and the additional digits in the scale are not just zeros for padding.
For example I see this if I do a ncdump on a point within the NetCDF dataset:
Variable: precipitation(0:0:1, 40:40:1, 150:150:1)
float precipitation(time=1348, lat=180, lon=360);
:units = "mm/month";
:long_name = "precipitation totals";
data:
{
{
{-99999.99}
}
}
However if I get a slice of the data from the variable like so:
value = precipitationVariable[0:1:1, 40:41:1, 150:151:1]
then I see it like this in my debugger (Eclipse/PyDev):
value == ndarray: [[[-99999.9921875]]]
So it seems as if the NetCDF dataset values that I read into a Numpy array are not being read with the same precision/scale of the original values in the NetCDF file. Or perhaps the values within the NetCDF are actually the same as what I'm seeing when I read them, but what's shown to me via ncdump is being truncated due to some format settings in the ncdump program itself.
Can anyone advise as to what's happening here? Thanks in advance for your help.
BTW I'm developing this code using Python 2.7.3 on a Windows XP machine and using the Python module for the NetCDF4 API provided here: https://code.google.com/p/netcdf4-python/
There is no simple way of doing what you want because numpy stores the values as single precision, so they will always have the trailing numbers after 0.99.
However, netCDF already provides a mechanism for missing data (see the best practices guide). How was the netCDF file written in the first place? The missing_value is a special variable attribute that should be used to indicate those values that are missing. In the C and Fortran interfaces, when the file is created all variable values are set to be missing. If you wrote a variable all in one go, you can then set the missing_value attribute to an array of indices where the values are missing. See more about the fill values in the C and Fortran interfaces. This is the recommended approach. The python netCDF4 module plays well with these missing values, and such arrays are read as masked arrays in numpy.
If you must work with the file you currently have, then I'd suggest creating a mask to cover values around your missing value:
import numpy as np
value = precipitationVariable[:]
mask = (value < -99999.98) & (value > -100000.00)
value = np.ma.MaskedArray(value, mask=mask)

Categories

Resources