Created
August 11, 2020 02:01
-
-
Save KenoLeon/c1017b6611c33d06ec52699f87c7a33f to your computer and use it in GitHub Desktop.
Type conversion in Numpy
This file contains hidden or 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 | |
| linear_spaced_array = np.linspace(0, 60, 5) | |
| print(linear_spaced_array) | |
| # >>> [ 0. 15. 30. 45. 60.] | |
| # What Type ? | |
| print(linear_spaced_array.dtype) | |
| # >>> float64 | |
| # Convert Float to Int : | |
| int_linear_spaced_array = linear_spaced_array.astype(int) | |
| print(int_linear_spaced_array) | |
| # >>> [ 0 15 30 45 60] | |
| print(int_linear_spaced_array.dtype) | |
| # >>> int32 | |
| # You can also define the type upon array creation: | |
| intLinSpace_array = np.linspace(0, 60, 5, dtype=np.uint8) | |
| print(intLinSpace_array) | |
| # >>> [ 0 15 30 45 60] | |
| print(intLinSpace_array.dtype) | |
| # >>> uint8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment