Reshaping an array into a nested array - python

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

Related

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.

create a random numpy array between -1 and 1 that sum of all element become zero

I would like to create a NumPy array that all arrays are between -1 to 1. I want to sum all array that becomes zero.
Could you please tell me how can I create this NumPy array?
You can accomplish this by creating two arrays with the two values and concatenating them. To finish it all of you shuffle the final concatenated array.
import numpy as np
size = 10
array_size = int(size / 2)
ones = np.ones(array_size)
minus_ones = np.full(array_size, -1)
sum_zero = np.concatenate((ones, minus_ones))
np.random.shuffle(sum_zero)
print(sum_zero)

Numpy array into multidimensional slices at different indices

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

create 2D array using multiple 1D array having different size using python

How can i make a 2d array using four 1d arrays arr1[] arr2[] arr3[] arr4[] of different sized, like arr1 = [1,2,3] arr2 = [4,5] arr3=[1,2,6,7] arr4=[1,3,8,7,5,4,0] arr4=[1,2,4,3,5] to create image and it should looks like:

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