Numpy array dimension conversion - python

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.

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)

Is there a way to properly format a large numpy array

I have a large 200x100 2d tuple (tuple of tuples) of floats that I'm trying to convert into a numpy array.
I do the conventional code:
arr = np.array(2d_tuple_of_floats)
But when I do that instead of converting into a 2d array, it converts into a 1d array of tuples (arr.shape = (200,)). So then I specify:
arr = np.array(2d_tuple_of_floats, ndmin=2)
And instead of changing to shape (200,100) it changes to the shape (1,200), implying that I simply have an array of 1 tuple of 200 tuples.
So then I try
arr = np.array(2d_tuple_of_floats, dtype=np.float, ndmin=2)
And it still remains as shape (1,200); I also tried making it as a matrix and converting the matrix to an array, but that did not work either.
How can I get it to the proper shape of (200,100)?
Edit:
For reference the tuple is formatted as:
((0,1/6.0,...),(0.0,1/9.0,...),...,(100/3.0+101/3.0,...))

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

How to iterate over the third array dimension returning a the 2d array

i have got a 3d numpy array, what is the best way to iterate over the third dimension in a for loop returning the 2d array of the current interation?
One way would be just by iterating with range from 0 to the length of the array:
# arr = 3d numpy array
for i in range(len(arr)):
print(arr[i])
just loop with its third dim:
import numpy as np
a = np.arange(24).reshape((2,3,4))
for i in range(a.shape[2]): # index 2 is for 3rd dimension
print(a[:, :, i])
# or
print(a[..., i])
then you got it.
but using loop with numpy array is costly, you should get used to broadcasting, vectorization, indexing...

extracting ith and ith+1 from random 2D numpy array

I have a numpy array consisting of
[1,3,8,6,0,2,4,5,9,7]
This array is a random array consisting of 10 numbers 0-9.
I also have a 2D numpy array, a 10X10 2D numpy array with numerical values.
I would like to use my 1D numpy array (above) to access specific instances in my 2D numpy array, by looping through the 1D array
Loop 1: takes in 1 and 3, and finds the value at [1:3] in my 2D numpy array.
Loop 2: takes in 3 and 8, and finds the value at [3:8] in my 2D numpy array.
.
Loop 10: takes in 7 and 1, and finds the value at [7:1] in my 2D numpy array.
I would like to add up these values in my 2D numpy array.
so far I have :
array=[1,3,8,6,0,2,4,5,9,7]
values =0
for i in range (0, len(array)): #this is 10
a=array2[i,array[i]+1] #array2 is the 2D numpy array with the values
values=values+a
This works to some degree but how to I get it to access the last element to the first? i.e. find [7,1]
You can use simple slicing to make this work.
arr = np.random.randint(0, 10, (10,10))
pos = np.array([1,3,8,6,0,2,4,5,9,7])
pos = np.append(pos, pos[0])
rows = pos[0:-1]
cols = pos[1:]
result = sum(arr[rows, cols])
You can do the slicing twice to make it work.
values = 0
for i in range(len(array)):
a = Matrix[array[i],array[i+1]]
values += a
Also, the array you put has 11 elements which means the 10-th loop will not be what you intended.
I'm not sure I fully understood what you were trying to achieve but...
What about something like this?
a = np.array([1,3,8,6,0,9,2,4,5,9,7])
b = np.array(range(100)).reshape(10,10)
for i in range (len(a)):
print (a[i%len(a)],a[(i+1)%len(a)])
print (b[a[i%len(a)],a[(i+1)%len(a)]])
I removed 10 from the a array to avoid an index out of range error.
I also took the value [x,y] (and not the range [x:y] from the 2D array.

Categories

Resources