demo.py(numpy,创建数组) :
#编码=utf-8
import numpy as np
使用numpy生成数组,得到ndarray的类型
t1=NP.array ([ 1,2,3 ] ) ) )。
打印(t1 ) #[1)2)3]
打印(类型) T1 ) #
T2=NP.array (范围(10 ) )
print(T2 ) # [0 1 2 3 4 5 6 7 8 9]
打印(类型) T2 ) #
T3=NP.arange (4,10,2 ) # 2表示步长,并且可以省略
打印(T3 ) # [4]6)8]
打印(类型) T3 ) #
demo.py(dtype、numpy的数据类型) :
#编码=utf-8
import numpy as np
导入随机
# numpy的数据类型
T1=NP.arange(10 ) )。
# dtype指示要存储的数据类型
print(t1.dtype ) int64 ) 64位电脑默认为int64 )
T2=NP.array (范围(1,4 )、# dtype='float32 ' ) dtype参数指定numpy的数据类型
# T2=NP.array (范围(1,4 ),类型=' i1 ' ) ) ) ) ) ) )。
print(T2 ) # [1. 2. 3.]
print(T2.dtype ) # float32
# numpy bool类型
T3=NP.array ([ 1,1,0,1,0,0 ],dtype=bool ) ) ) ) ) ) ) ) ) ) )。
打印(T3 ) # [True True False True False False]
打印(T3.dtype ) # bool
#更改数据类型
T4=T3.astype(int8) ) # bool类型到int8类型的转换
print(T4 ) # [1 1 0 1 0 0]
print(T4.dtype ) # int8
# numpy的小数浮点64
T5=NP.array([random.random ) for i in range(5) )5]
print(T5 ) # [ 0.55897787.6086214.25367407.806900280.72111836 ]
print(t5.dtype ) float64 ) ) 64位电脑的默认值为float64 ) ) ) ) ) ) ) )。
T6=NP.round (t5,2 ) #保留小数点后两位
打印(T6 ) # [0.56 0.61 0.25 0.81 0.72]
print(T6.dtype ) # float64