reshape nested numpy array with shape similar to another array - python
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]]]])
Related
Numpy array reshape customization python
I have an np array n and the shape is (250,250). after that I converted (final array w)it into (250,250,3) because I need to do some operation on it. is it possible to convert the shape of w to (250,250)? thanks in advance. I tried some reshaping operation of Numpy but it does not work! Numpy reshaping array Comparing two NumPy arrays for equality, element-wise Numpy reshaping array
numpy.reshape Gives a new shape to an array without changing its data. so this is not right to convert array with shape of (250,250,3) into array with shape of (250,250) as 1st does have 187500 cells and 2nd does have 62500 cells. You probably should use slicing, consider following example import numpy as np arr = np.array([[[0,1],[2,3]],[[4,5],[6,7]]]) # has shape (2,2,2) arr2 = arr[:,:,0] # get certain cross-section, check what will happend if you use 1 inplace of 0 and 2 inplace of 0 print("arr2") print(arr2) print("arr2.shape",arr2.shape) output arr2 [[0 2] [4 6]] arr2.shape (2, 2)
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.
Reshaping an array into a nested array
Given an array with multiple arrays nested inside of different length, how do I reshape a separate array that has an equal amount of values? For example: import numpy array1 = numpy.array([[0.1,0.2],[0.3,0.4,0.5]],dtype=object) array2 = numpy.array([0.11,0.22,0.33,0.44,0.55]) What I am trying to get is: finalarray = numpy.array([[0.11,0.22,], [0.33,0.44,0.55]])
Error: 3D Matlab array to 0 dimensional np array
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)
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))))