numpy改变数组类型的方法

33 篇文章 7 订阅
3 篇文章 0 订阅

使用astype函数转换dtype

import numpy as np
a = np.zeros([2,2],dtype='float32')
print("a type is {}".format(a.dtype))

b = a.astype(int)
print("b is type {}".format(b.dtype))

b = a.astype(np.float32)
print("b is type {}".format(b.dtype))

numpy.ndarray.astype输入的类型有两种字符str或者dtype

numpy.ndarray.astype
ndarray.astype(**dtype**, order='K', casting='unsafe', subok=True, copy=True)
Copy of the array, cast to a specified type.

Parameters:	
dtype : **str or dtype**

Typecode or data-type to which the array is cast.

numpy中np.array()与np.asarray的区别以及.tolist

array和asarray都可以将结构数据转化为ndarray,但是主要区别就是当数据源是ndarray时,array仍然会copy出一个副本,占用新的内存,但asarray不会。

  • list列表转化成ndarray的数组
import numpy as np
a = [[1,2],[4,6]]
b = np.array(a,dtype=np.float32)
print(b.dtype)
  • .tolist将array类型转为list类型
import numpy as np
a = [[1,2],[4,6]]
b = np.array(a,dtype=np.float32)
print(b.dtype)

b.tolist()
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值