2019-07-18
阅读量:
833
指定ndarray数组元素的类型
# -*- coding:utf-8 -*-
# author:
import numpy
x = numpy.array([1,2.6,3],dtype = numpy.int64)#生成指定元素类型的数组:设置dtype属性
x = numpy.array([1,2,3],dtype = numpy.float64)
print(x )# 元素类型为float64
print(x.dtype)
x = numpy.array([1,2.6,3],dtype = numpy.float64)#使用astype复制数组,并转换类型
y = x.astype(numpy.int32)
z = y.astype(numpy.float64)
x = numpy.array(['1','2','3'],dtype = numpy.string_)#将字符串元素转换为数值元素
y = x.astype(numpy.int32)
x = numpy.array([ 1., 2.6,3. ],dtype = numpy.float32)#使用其他数组的数据类型作为参数
y = numpy.arange(3,dtype=numpy.int32)
print(y)
print(y.astype(x.dtype))






评论(0)


暂无数据
推荐帖子
2条评论
6条评论
7条评论