第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > python dtype(0)_Python numpy 创建数组 数据类型 dtype属性

python dtype(0)_Python numpy 创建数组 数据类型 dtype属性

时间:2018-08-06 07:32:08

相关推荐

python dtype(0)_Python numpy 创建数组 数据类型 dtype属性

demo.py(numpy,创建数组):

# coding=utf-8

import numpy as np

# 使用numpy生成数组,得到ndarray的类型

t1 = np.array([1,2,3])

print(t1) # [1 2 3]

print(type(t1)) #

t2 = np.array(range(10))

print(t2) # [0 1 2 3 4 5 6 7 8 9]

print(type(t2)) #

t3 = np.arange(4,10,2) # 2表示步长,可以省略

print(t3) # [4 6 8]

print(type(t3)) #

demo.py(dtype,numpy中的数据类型):

# coding=utf-8

import numpy as np

import random

# numpy中的数据类型

t1 = np.arange(10)

# dtype表示存放的数据类型

print(t1.dtype) # int64 (64位电脑默认是int64)

t2 = np.array(range(1,4),dtype="float32") # dtype参数指定numpy中的数据类型

# t2 = np.array(range(1,4),dtype="i1")

print(t2) # [1. 2. 3.]

print(t2.dtype) # float32

# numpy中的bool类型

t3 = np.array([1,1,0,1,0,0],dtype=bool)

print(t3) # [True True False True False False]

print(t3.dtype) # bool

# 修改数据类型

t4 = t3.astype("int8") # bool类型转成int8类型

print(t4) # [1 1 0 1 0 0]

print(t4.dtype) # int8

# numpy中的小数 float64

t5 = np.array([random.random() for i in range(5)])

print(t5) # [0.55897787 0.6086214 0.25367407 0.80690028 0.72111836]

print(t5.dtype) # float64 (64位电脑默认是float64)

t6 = np.round(t5,2) # 保留2位小数

print(t6) # [0.56 0.61 0.25 0.81 0.72]

print(t6.dtype) # float64

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。