Skip to content

Commit a904e73

Browse files
authored
Update concatenation-of-arrays.md
1 parent 8199256 commit a904e73

File tree

1 file changed

+55
-27
lines changed

1 file changed

+55
-27
lines changed

contrib/numpy/concatenation-of-arrays.md

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,39 @@ Args:
2020

2121
### Example
2222

23+
#### Concatenate along axis 0
24+
2325
```python
2426
import numpy as np
2527
#creating 2 arrays
2628
arr1 = np.array([1 2 3],[7 8 9])
2729
arr2 = np.array([4 5 6],[10 11 12])
2830

29-
#concatenate along axis 0
3031
result_1 = np.concatenate((arr1, arr2), axis=0)
31-
print("Concatenation along axis 0:")
3232
print(result_1)
33-
""" Output- Concatenation along axis 0:
33+
```
34+
35+
#### Output
36+
```
3437
[[ 1 2 3]
3538
[ 7 8 9]
3639
[ 4 5 6]
37-
[10 11 12]] """
40+
[10 11 12]]
41+
```
3842

39-
#concatenate along axis 1
43+
#### Concatenate along axis 1
44+
45+
```python
4046
result_2 = np.concatenate((arr1, arr2), axis=1)
41-
print("Concatenation along axis 1:")
4247
print(result_2)
43-
""" Output- Concatenation along axis 1:
44-
[[ 1 2 3 4 5 6 ]
45-
[ 7 8 9 10 11 12]] """
48+
```
4649

50+
#### Output
4751
```
52+
[[ 1 2 3 4 5 6 ]
53+
[ 7 8 9 10 11 12]]
54+
```
55+
4856
## np.vstack
4957

5058
Vertical stacking of arrays (row-wise).
@@ -59,6 +67,7 @@ Args:
5967
- arrays: Sequence of arrays to stack.
6068

6169
### Example
70+
6271
```python
6372
import numpy as np
6473
#create arrays
@@ -67,12 +76,16 @@ arr2 = np.array([4 5 6],[10 11 12])
6776

6877
result = np.vstack((arr1, arr2))
6978
print(result)
70-
"""output-
79+
```
80+
81+
#### Output
82+
```
7183
[[ 1 2 3]
7284
[ 7 8 9]
7385
[ 4 5 6]
74-
[10 11 12]] """
86+
[10 11 12]]
7587
```
88+
7689
## 3. np.hstack
7790

7891
Stacks arrays horizontally (column-wise).
@@ -96,9 +109,12 @@ arr2 = np.array([4 5 6],[10 11 12])
96109

97110
result = np.hstack((arr1, arr2))
98111
print(result)
99-
"""output-
112+
```
113+
114+
#### Output
115+
```
100116
[[ 1 2 3] [ 4 5 6]
101-
[ 7 8 9] [10 11 12]] """
117+
[ 7 8 9] [10 11 12]]
102118
```
103119

104120
## np.dstack
@@ -123,14 +139,17 @@ arr2 = np.array([4 5 6],[10 11 12])
123139

124140
result = np.dstack((arr1, arr2))
125141
print(result)
126-
""" output-
142+
```
143+
144+
#### Output
145+
```
127146
[[[ 1 4]
128147
[ 2 5]
129148
[ 3 6]]
130149
131150
[[ 7 10]
132151
[ 8 11]
133-
[ 9 12]]] """
152+
[ 9 12]]]
134153
```
135154

136155
## np.stack
@@ -154,12 +173,15 @@ arr2 = np.array([4 5 6],[10 11 12])
154173

155174
result = np.stack((arr1, arr2), axis=0)
156175
print(result)
157-
""" output-
176+
```
177+
178+
#### Output
179+
```
158180
[[[ 1 2 3]
159181
[ 7 8 9]]
160182
161183
[[ 4 5 6]
162-
[10 11 12]]] """
184+
[10 11 12]]]
163185
```
164186

165187
# Concatenation with Mixed Dimensions
@@ -168,28 +190,34 @@ When concatenating arrays with different shapes, it's often necessary to reshape
168190

169191
## Example
170192

193+
#### Concatenate along axis 0
194+
171195
```python
172196
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
173197
arr2 = np.array([7, 8, 9])
174198

175-
# Concatenate along axis 0
176199
result_0= np.concatenate((arr1, arr2[np.newaxis, :]), axis=0)
177-
print("Concatenation along axis 0:")
178200
print(result_0)
179-
""" output-
180-
Concatenation along axis 0:
201+
```
202+
203+
#### Output
204+
```
181205
[[1 2 3]
182206
[4 5 6]
183-
[7 8 9]] """
207+
[7 8 9]]
208+
```
209+
210+
#### Concatenate along axis 1
184211

185-
# Concatenate along axis 1
212+
```python
186213
result_1 = np.concatenate((arr1, arr2[:, np.newaxis]), axis=1)
187-
print("\nConcatenation along axis 1:")
188214
print(result_1)
189-
""" output-
190-
Concatenation along axis 1:
215+
```
216+
217+
#### Output
218+
```
191219
[[1 2 3 7]
192-
[4 5 6 8]] """
220+
[4 5 6 8]]
193221
```
194222

195223

0 commit comments

Comments
 (0)