Skip to content

Latest commit

 

History

History
65 lines (47 loc) · 1.17 KB

File metadata and controls

65 lines (47 loc) · 1.17 KB

ndarray的数据类型

arr1 = np.array([1,2,3],dtype=np.float64)
arr2 = np.array([1,2,3],dtype=np.int32)
arr1.dtype
>>>dtype('float64')
arr2.dtype
>>>dtype('int32')

astype

arr = np.array([1,2,3,4,5])
arr.dtype
>>>dtype('int64')

float_arr = arr.astype(np.float64)
float_arr.dtype
>>>dtype('float64')

arr = np.array([3.7,-1.2,3.2])
arr
>>>array([ 3.7, -1.2,  3.2])

arr.astype(np.int32)
>>>array([ 3, -1,  3], dtype=int32)

numeric_strings = np.array(['1.2','34.32','1.34'],dtype=np.string_)
numeric_strings.astype(float)
>>>array([ 1.2 , 34.32,  1.34])

int_array = np.arange(10)
calibers = np.array([.22,.240,.234,.33,.23],dtype=np.float64)
int_array.astype(calibers.dtype)
>>>array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])

empty_unit32 = np.empty(8,dtype='u4')
empty_unit32
>>>array([         0, 1075314688,          0, 1075707904,          0,
       1075838976,          0, 1072693248], dtype=uint32)

数组和标量的运算

arr = np.array([[1.,2.,3.],[4.,5.,6.]])
arr
>>>array([[1., 2., 3.],
       [4., 5., 6.]])

arr*arr
>>>array([[ 1.,  4.,  9.],
       [16., 25., 36.]])

arr-arr
array([[0., 0., 0.],
       [0., 0., 0.]])