In [1]:
import numpy as np
In [2]:
a = np.array([1, 2, 3])
print(a)
print(a.dtype)
In [3]:
a_float = a.astype(np.float32)
print(a_float)
print(a_float.dtype)
In [4]:
print(a)
print(a.dtype)
In [5]:
a_float = a.astype(float)
print(a_float)
print(a_float.dtype)
In [6]:
a_str = a.astype('str')
print(a_str)
print(a_str.dtype)
In [7]:
a_int = a.astype('int32')
print(a_int)
print(a_int.dtype)
In [8]:
a = np.arange(50).reshape((5, 10)) / 10 - 2
print(a)
print(a.dtype)
In [9]:
a_int = a.astype('int64')
print(a_int)
print(a_int.dtype)
In [10]:
print(np.round(a).astype(int))
In [11]:
my_round_int = lambda x: np.round((x * 2 + 1) // 2)
In [12]:
print(my_round_int(a).astype(int))
In [13]:
def my_round(x, digit=0):
p = 10 ** digit
s = np.copysign(1, x)
return (s * x * p * 2 + 1) // 2 / p * s
In [14]:
print(my_round(a).astype(int))