-
Notifications
You must be signed in to change notification settings - Fork 0
/
7-Concatenation.py
56 lines (46 loc) · 1.73 KB
/
7-Concatenation.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import numpy as np
# # Array Concatenation
# a = np.array([[1, 2, 3, 4],[9, 10, 11, 12]])
# b = np.array([[4, 5, 6, 7]])
# print(a) # => [1 2 3 4]
# print(b) # => [5 6 7 8]
# # by default np.concatenate has axis = 0
# print(np.concatenate((a, b))) # added b as new row
# # [[ 1 2 3 4]
# # [ 9 10 11 12]
# # [ 4 5 7 8]]
# print(np.concatenate((a, b), axis=0)) # same to line 11
# # Lets try with axis = None
# # axis=None will transform all the rows into a 1-d Array
# print(np.concatenate((a, b), axis=None))
# # [ 1 2 3 4 9 10 11 12 4 5 6 7]
# # axis = 0 means add as row
# # axis = 1 means add as column
# # with axis = 1
# # returns error because we cannot add row of size 4 as column of size 2 in a
# print(np.concatenate((a, b), axis=1))
# # using transpose of b also return error because column of elements 4 cannot be added to column of size
# print(np.concatenate((a, b.T), axis=1))
# # now try with matrix of having only column with size 2
# c = np.array([[45],[50]])
# print(np.concatenate((a, c), axis=1))
# # output will be
# # [[ 1 2 3 4 45]
# # [ 9 10 11 12 50]]
# # Transpose means turns rows into columns and columns into rows
# # now try with matrix of having only column with size 2
# c = np.array([[45, 50]])
# # print(np.concatenate((a, c), axis=1)) # this will give error because of row into column
# print(np.concatenate((a, c.T), axis=1))
# # output will be same as above after transposing
# # [[ 1 2 3 4 45]
# # [ 9 10 11 12 50]]
# # Another way using hstack and vstack
# a = np.array([1, 2, 3, 4])
# b = np.array([5, 6, 7, 8])
# # hstack appends array horizontally
# print(np.hstack((a, b))) # => [1 2 3 4 5 6 7 8]
# # vstack appends array vertically
# print(np.vstack((a, b)))
# # [[1 2 3 4]
# # [5 6 7 8]]