import numpy as np
a = np.array([1,2,3],dtype=np.int32)#创建array时可以定义元素类型
print(a.dtype)
out:int32
b = np.array([1,2,3],dtype=np.float)
print(b.dtype)
out:float64
zero = np.zeros((2,3))#生成2行3列全为0的数组(不是矩阵)
print(zero)
out:
[[0. 0. 0.]
[0. 0. 0.]]
one = np.ones((2,3))#生成2行3列全为1的数组(不是矩阵)
print(one)
out:
[[1. 1. 1.]
[1. 1. 1.]]
empty = np.empty((3,2))#empty是接近于0但是不等于0
print(empty)
out:
[[0. 0.]
[0. 0.]
[0. 0.]]
a = np.arange(10)
print(a)
out:[0 1 2 3 4 5 6 7 8 9]
b = np.arange(4,12)#左闭右开
print(b)
out:[ 4 5 6 7 8 9 10 11]
c = np.arange(1,20,3)
print(c)
out:[ 1 4 7 10 13 16 19]
#reshape重新定义数组的形状
d = np.arange(8).reshape(2,4)
print(d)
out:
[[0 1 2 3]
[4 5 6 7]]
print(d.reshape(4,2))
out:
[[0 1]
[2 3]
[4 5]
[6 7]]
numpy基础--2.创建array
最新推荐文章于 2022-07-22 20:56:37 发布