kopia lustrzana https://github.com/animator/learn-python
Update reshape-array.md
rodzic
bf4fa5a067
commit
02c7f2c3a2
|
@ -1,54 +1,57 @@
|
||||||
# Numpy Array Shape and Reshape
|
# Numpy Array Shape and Reshape
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
Code to create a 2D array
|
Let us create a 2D array
|
||||||
|
|
||||||
``` python
|
``` python
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
numbers = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
|
numbers = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
|
||||||
print(numbers)
|
print(numbers)
|
||||||
|
|
||||||
# Output:
|
|
||||||
# array([[1, 2, 3, 4],[5, 6, 7, 8]])
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Changing Array Shape using Reshape()
|
#### Output:
|
||||||
|
|
||||||
|
``` python
|
||||||
|
array([[1, 2, 3, 4],[5, 6, 7, 8]])
|
||||||
|
```
|
||||||
|
|
||||||
|
## Changing Array Shape using `reshape()`
|
||||||
|
|
||||||
The `reshape()` function allows you to rearrange the data within a NumPy array.
|
The `reshape()` function allows you to rearrange the data within a NumPy array.
|
||||||
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.
|
|
||||||
|
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.
|
||||||
|
|
||||||
``` python
|
``` python
|
||||||
arr_1d = np.array([1, 2, 3, 4, 5, 6]) # 1D array
|
arr_1d = np.array([1, 2, 3, 4, 5, 6]) # 1D array
|
||||||
arr_2d = arr_1d.reshape(2, 3) # Reshaping with 2rows and 3cols
|
arr_2d = arr_1d.reshape(2, 3) # Reshaping with 2 rows and 3 cols
|
||||||
|
|
||||||
print(arr_2d)
|
print(arr_2d)
|
||||||
|
|
||||||
# Output:
|
|
||||||
# array([[1, 2, 3],[4, 5, 6]])
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Changing Array Shape using Resize()
|
#### Output:
|
||||||
|
|
||||||
|
``` python
|
||||||
|
array([[1, 2, 3],[4, 5, 6]])
|
||||||
|
```
|
||||||
|
|
||||||
|
## Changing Array Shape using `resize()`
|
||||||
|
|
||||||
The `resize()` function allows you to modify the shape of a NumPy array directly.
|
The `resize()` function allows you to modify the shape of a NumPy array directly.
|
||||||
|
|
||||||
It take 2 arguements, row and columns.
|
It take 2 arguements, row and columns.
|
||||||
|
|
||||||
``` python
|
``` python
|
||||||
import numpy as np
|
import numpy as np
|
||||||
arr_1d = np.array([1, 2, 3, 4, 5, 6])
|
arr_1d = np.array([1, 2, 3, 4, 5, 6])
|
||||||
|
|
||||||
arr_1d.resize((2, 3)) # 2rows and 3cols
|
arr_1d.resize((2, 3)) # 2 rows and 3 cols
|
||||||
print(arr_1d)
|
print(arr_1d)
|
||||||
|
|
||||||
# Output:
|
|
||||||
# array([[1, 2, 3],[4, 5, 6]])
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Reshape() VS Resize()
|
#### Output:
|
||||||
|
|
||||||
| Reshape | Resize |
|
``` python
|
||||||
| ----------- | ----------- |
|
array([[1, 2, 3],[4, 5, 6]])
|
||||||
| Does not modify the original array | Modifies the original array in-place |
|
```
|
||||||
| Creates a new array | Changes the shape of the array |
|
|
||||||
| Returns a reshaped array | Doesn't return anything |
|
|
||||||
| Compatibility between dimensions | Does not compatibility between dimensions |
|
|
||||||
| Syntax: reshape(row,col) | Syntax: resize((row,col)) |
|
|
||||||
|
|
Ładowanie…
Reference in New Issue