-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination.py
42 lines (28 loc) · 1.16 KB
/
combination.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import numpy as np
# implement your function to combine two numpy arrays
def combination(arrayOne, arrayTwo, axis=0):
'''Write a function combination that takes in two numpy arrays and an optional parameter axis which should be 0 by default.
Remove unnecessary dimensions of the input arrays,
check whether they can be combined along the given axis and return the combined array.
If the combination is not possible, raise a meaningful error message.'''
# Remove unnecessary dimensions of the input arrays,
arrayOne = arrayOne.squeeze()
arrayTwo = arrayTwo.squeeze()
print(arrayOne.shape)
print(arrayTwo.shape)
print(arrayOne)
print(arrayTwo)
try:
combination = np.concatenate((arrayOne, arrayTwo) , axis)
return combination
except:
print('The two arrays can not be combined.')
if __name__ == "__main__":
# use this for your own testing!
arrayONE = np.array([4,5,6,7,8,9,0])
arrayTWO = np.array([4,5,6,7,8,9,0])
print(combination(arrayONE, arrayTWO, 0))
a = np.array([[[[1, 2], [3, 4], [5, 6]]]])
b = np.ones((2,2))
print(combination(a, b))
pass