numpy中ndim,shape,dtype,astype的用法

1.ndim:返回的是数组的维度,返回的只有一个数,该数即表示数组的维度。

l = np.array([[1,2]])
print (l.ndim)

Out:2

2.shape:表示各位维度大小的元组。返回的是一个元组。

l = np.array([[1.0,2.0]])
print (l.shape)

Out:(1,2)

3.dtype:一个用于说明数组数据类型的对象。返回的是该数组的数据类型。由于图中的数据都为整形,所以返回的都是int32。如果数组中有数据带有小数点,那么就会返回float64。

l = np.array([[1.0,2.0]])
print (l.dtype)

Out:float64

4.astype:转换数据类型,必须有接受对象,不能在原位置改动

l = np.array([[1.1,2.]])
l2 = l.astype(np.int32)
print (l2)
print (l2.dtype)

Out:[[1 2]]
Out:int32

int32

arr1 = np.array(['1.22','0'])
arr2 = arr1.astype(np.float64)
print (arr2)

Out:[1.22 0. ]

参考链接:https://blog.csdn.net/Da_wan/article/details/80518725

发布了59 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Galen_xia/article/details/103480239
今日推荐