How can I write a function named split which accepts three parameters a, b, c and then do the following.
- create a n dimensional array 'x' having first a natural numbers (use
np.arange
method). - change the shape of x to (c, b) and assign to new array y.
- split the array y horizontally into two arrays, then assign it to i and j.
- display i and j.
I tried using hsplit
and array_split
methods and then assign it to i and j. But the output is not matching as given below.
import numpy as np
x=np.arange(20)
y = np.array(x)
z= y.reshape(10,2)
#a = np.hsplit(z,2)
(a,b)=np.array_split(z,2,axis=0)
print(a)
print(b)
Actual output:-
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
[[10 11]
[12 13]
[14 15]
[16 17]
[18 19]]
Desired output:-
[[ 0 1 2 3 4]
[10 11 12 13 14]]
[[ 5 6 7 8 9]
[15 16 17 18 19]]
CodePudding user response:
You were right with hsplit, the problem is just the shape is the other way around to get the desired output:
import numpy as np
x=np.arange(20)
y = np.array(x)
z= y.reshape(2,10)
a,b = np.hsplit(z,2)
print(a)
print(b)
output:
[[ 0 1 2 3 4]
[10 11 12 13 14]]
[[ 5 6 7 8 9]
[15 16 17 18 19]]