Given a numpy array and integer x, is there a way to create a multi-dimensional numpy array where each row is a slice of the original array starting from index 0 ... x without hard-coding? For example, if x = 3:
array = np.arange(10)
output = np.array([array, array[1:], array[2:], array[3:]])
Related
I have 3 different 2D numpy arrays, each having a different number of columns:
import numpy as np
arr1 = np.random.randn(10, 5)
arr2 = np.random.randn(10, 6)
arr3 = np.random.randn(10, 4)
Now I want to merge these 3 2D arrays into a single 3D array:
myList = []
myList.append(arr1)
myList.append(arr2)
myList.append(arr3)
3darr = np.dstack(myList)
However, since the arrays have different number of columns, I get an error:
ValueError: all the input array dimensions for the concatenation axis
must match exactly, but along dimension 1, the array at index 0 has
size 5 and the array at index 1 has size 6
How can I create a 3D array by taking the minimum number of columns across the 3 2D arrays? In other words, the minimum number of columns across the 3 2D arrays is equal to 4. Thus, I want to drop the columns after 4th in all 2D arrays that have more than 4 columns.
hope following could help.
import numpy as np
a = []
a.append(np.random.randn(10, 5))
a.append(np.random.randn(10, 6))
a.append(np.random.randn(10, 4))
k = min([i.shape[1] for i in a])
out = np.zeros((a[0].shape[0], k, len(a)))
for ind in range(len(a)):
out[:,:,i] = a[i][:,:k]
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.
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]])
I have a 3D array [256,256,450] that I would like to fetch a 2D array from a cross section along the z axis. The new 2D array should start at z=0 at the top and have the values across some i'th y slice for x =0 to x=255. Then the next row in the new 2D array should be the same for z=1, and so on until z=449. How can this be done?
Use NumPy's NDArray class and slicing syntax.
import numpy as np
my_array = np.zeros([256, 256, 450]) # 256x256x450 array
... # Do whatever you want to do to load data in
x_slice = my_array[0,:,:] # A 256x450 array, the first element in x
y_slice = my_array[:,0,:] # A 256x450 array, the first element in y
y_slice = my_array[:,99,:] # A 256 x 450 array, the 100th element in y
import numpy as np
array_3d = np.ones((256, 256, 450))
y_layer = 24 # arbitrary y layer
array_2d = array_3d[:, y_layer, :]
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.