
Today, I will show you how to use the NumPy array with the following example.
create one-dimension NumPy array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
arr = np.array([1, 2, 3]) | |
print(arr) | |
# Output | |
# [1 2 3] |
create two-dimension NumPy array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
arr = np.array([[1, 1, 1], [2, 2, 2]]) | |
print(arr) | |
# Output | |
# [[1 1 1] | |
# [2 2 2]] |
Set minimum dimension to NumPy array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
arr = np.array([1, 1, 1], ndmin=3) | |
print(arr) | |
# Ouput | |
# [[[1 1 1]]] |
Numpy can automatically upcasting type like this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
arr = np.array([1, 1, 1.0]) | |
print(arr) | |
# Output | |
# [1. 1. 1.] |
also can change the data type in an array like this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
arr = np.array([1, 1, 1], dtype=float) | |
print(arr) | |
# Output | |
# [1. 1. 1.] |
Thank you for reading & enjoy your coding 🙂