Error: 3D Matlab array to 0 dimensional np array - python

I'm having an issue transforming 3 dimensional matlab array into a 3 dimensional np array in python. When I read it in, an error message shows me it is a 0 dimensional np array.
This is the code I am using:
import scipy.io
import numpy as np
mat = scipy.io.loadmat('2021.01.25.FC.mat')
matrix = np.array(mat)
However, when I index the array like this:
x=matrix[2,2,2]
I receive this error:
IndexError: too many indices for array: array is 0-dimensional, but 3 were indexed
Does any one know the reason why this array is being read in as a 0 dimensional array in numpy or how to correct this?
Thanks!

I think that it is due to 'mat' being a dictionary as the scipy.io.loadmat documentation suggests.
'mat' is likely a dictionary that stores all the variables present in your '2021.01.25.FC.mat' file. If the matrix you are interested in is named "MyMatrix" in your matlab file then a quick fix could be:
import scipy.io
import numpy as np
mat = scipy.io.loadmat('2021.01.25.FC.mat')['MyMatrix']
matrix = np.array(mat)

Related

petsc4py converting rectangular numpy matrix to petsc matrix

The code below works only if input is a square numpy matrix (eg np.eye(3)) but not if input is a rectangular matrix.
import numpy as np
from petsc4py import PETSc
input = np.array([[0,1,0,0],[1,0,0,0],[0,0,0,1]])
#input = np.eye(3)
indices = np.nonzero(input)
A = PETSc.Mat().create()
A.setSizes(input.shape)
A.setType("aij")
A.setUp()
# First arg is list of row indices, second list of column indices
A.setValues(list(indices[0]),list(indices[1]), input)
A.assemble()
If I run the above I get the error message:
ValueError: incompatible array sizes: ni=3, nj=3, nv=12
Do PETSc matrices have to be square matrices or can I modify the code above to make this work ?
I tried transposing input.shape, but that did not help.

Numpy array dimension conversion

I have a 2 dimension array which sub-array has different size, it is expected to operate as 2 dimension array but turns out 1, is there anything wrong?
import numpy as np
sample_list = [['Section 1','Section 2','Section 3'],['Section 4','Section 5'],['Section 6']]
nd_array = np.array(sample_list, dtype=object)
print(nd_array.ndim)
the output is 1
however, when it change to
import numpy as np
sample_list = [['Section 1','Section 2','Section 3'],['Section 4','Section 5','Section 6'],['Section 7','Section 7','Section 7']]
nd_array = np.array(sample_list, dtype=object)
print(nd_array.ndim)
the output is as expected is 2.
There's nothing wrong, except that your first array is not a 2-dimensional array. It's a one-dimensional array with 3 entries, each of which happens to be a different-sized list.
Numpy 2D arrays are always square. You'll have to pad the lists in your first example if you want to make it a 2D array.

reshape nested numpy array with shape similar to another array

I have two numpy array sample and r.
sample is nested array and r is flat array (1-D).
I want to reshape numpy array r similar to the shape of sample array.
import numpy as np
sample = np.array([[[[1,0,0,1],[0,0.8,0.7,1]],[[2,2,0,1],[0,0.8,0.7,1]]],[[[1,0,0],[0,0.8,0.7]],[[1,1,0],[0,0.25,0.45]]],[[[0,1],[0,4]]]])
r = np.array([2,0,0,2,0,0.81,0.71,11,2,2,0,1,0,0.8,0.7,1,1,0,0,0,0.8,0.7,1,1,0,0,0.25,0.45,0,10,0,40])
desired array:
r_reshaped = np.array([[[[2,0,0,2],[0,0.81,0.71,11]],[[2,2,0,1],[0,0.8,0.7,1]]],[[[1,0,0],[0,0.8,0.7]],[[1,1,0],[0,0.25,0.45]]],[[[0,10],[0,40]]]])

Simple reshape of numpy array: error: 'total size of new array must be unchanged'

Here is a very straightforward version of the problem i am having reshaping a 40*1 array into a 20*2 array. What is going wrong here?
import numpy as np
x=np.linspace(1,20,40)
#confirm length is 40
print(np.shape(x))
#reshape to 2*20
print(np.reshape(x,2,20))
#returns error: 'total size of new array must be unchanged'
You do not use the function as you should use it.
Simply use this:
np.reshape(x,(2,20))
Documentation here
Full code:
import numpy as np
x=np.linspace(1,20,40)
#confirm length is 40
print(np.shape(x))
print(np.reshape(x,(2,20)))

Convert two numpy array to dataframe

I want to convert two numpy array to one DataFrame containing two columns.
The first numpy array 'images' is of shape 102, 1024.
The second numpy array 'label' is of shape (1020, )
My core code is:
images=np.array(images)
label=np.array(label)
l=np.array([images,label])
dataset=pd.DataFrame(l)
But it turns out to be an error saying that:
ValueError: could not broadcast input array from shape (1020,1024) into shape (1020)
What should I do to convert these two numpy array into two columns in one dataframe?
You can't stack them easily, especially if you want them as different columns, because you can't insert a 2D array in one column of a DataFrame, so you need to convert it to something else, for example a list.
So something like this would work:
import pandas as pd
import numpy as np
images = np.array(images)
label = np.array(label)
dataset = pd.DataFrame({'label': label, 'images': list(images)}, columns=['label', 'images'])
This will create a DataFrame with 1020 rows and 2 columns, where each item in the second column contains 1D arrays of length 1024.
Coming from engineering, I like the visual side of creating matrices.
matrix_aux = np.vstack([label,images])
matrix = np.transpose(matrix_aux)
df_lab_img = pd.DataFrame(matrix)
Takes a little bit more of code but leaves you with the Numpy array too.
You can also use hstack
import pandas as pd
import numpy as np
dataset = pd.DataFrame(np.hstack((images, label.reshape(-1, 1))))

Categories

Resources