Skip to content

Commit 02c7f2c

Browse files
authored
Update reshape-array.md
1 parent bf4fa5a commit 02c7f2c

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

contrib/numpy/reshape-array.md

+25-22
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,57 @@
11
# Numpy Array Shape and Reshape
2+
23
In NumPy, the primary data structure is the ndarray (N-dimensional array). An array can have one or more dimensions, and it organizes your data efficiently.
34

4-
Code to create a 2D array
5+
Let us create a 2D array
6+
57
``` python
68
import numpy as np
79

810
numbers = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
911
print(numbers)
12+
```
13+
14+
#### Output:
1015

11-
# Output:
12-
# array([[1, 2, 3, 4],[5, 6, 7, 8]])
16+
``` python
17+
array([[1, 2, 3, 4],[5, 6, 7, 8]])
1318
```
1419

15-
## Changing Array Shape using Reshape()
20+
## Changing Array Shape using `reshape()`
21+
1622
The `reshape()` function allows you to rearrange the data within a NumPy array.
17-
It take 2 arguements, row and columns. The `reshape()` can add or remove the dimensions. For instance, array can convert a 1D array into a 2D array or vice versa.
23+
24+
It take 2 arguments, row and columns. The `reshape()` can add or remove the dimensions. For instance, array can convert a 1D array into a 2D array or vice versa.
1825

1926
``` python
2027
arr_1d = np.array([1, 2, 3, 4, 5, 6]) # 1D array
21-
arr_2d = arr_1d.reshape(2, 3) # Reshaping with 2rows and 3cols
28+
arr_2d = arr_1d.reshape(2, 3) # Reshaping with 2 rows and 3 cols
2229

2330
print(arr_2d)
31+
```
2432

25-
# Output:
26-
# array([[1, 2, 3],[4, 5, 6]])
33+
#### Output:
2734

35+
``` python
36+
array([[1, 2, 3],[4, 5, 6]])
2837
```
2938

30-
## Changing Array Shape using Resize()
39+
## Changing Array Shape using `resize()`
40+
3141
The `resize()` function allows you to modify the shape of a NumPy array directly.
42+
3243
It take 2 arguements, row and columns.
3344

3445
``` python
3546
import numpy as np
3647
arr_1d = np.array([1, 2, 3, 4, 5, 6])
3748

38-
arr_1d.resize((2, 3)) # 2rows and 3cols
49+
arr_1d.resize((2, 3)) # 2 rows and 3 cols
3950
print(arr_1d)
40-
41-
# Output:
42-
# array([[1, 2, 3],[4, 5, 6]])
43-
4451
```
4552

46-
## Reshape() VS Resize()
53+
#### Output:
4754

48-
| Reshape | Resize |
49-
| ----------- | ----------- |
50-
| Does not modify the original array | Modifies the original array in-place |
51-
| Creates a new array | Changes the shape of the array |
52-
| Returns a reshaped array | Doesn't return anything |
53-
| Compatibility between dimensions | Does not compatibility between dimensions |
54-
| Syntax: reshape(row,col) | Syntax: resize((row,col)) |
55+
``` python
56+
array([[1, 2, 3],[4, 5, 6]])
57+
```

0 commit comments

Comments
 (0)