Skip to content

Instantly share code, notes, and snippets.

@KenoLeon
Created August 11, 2020 02:01
Show Gist options
  • Select an option

  • Save KenoLeon/c1017b6611c33d06ec52699f87c7a33f to your computer and use it in GitHub Desktop.

Select an option

Save KenoLeon/c1017b6611c33d06ec52699f87c7a33f to your computer and use it in GitHub Desktop.
Type conversion in Numpy
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