ndarray一个强大的N维数组对象Array
•ndarray的建立(元素默认浮点数)
import numpy as np
list =[0,1,2,3]
从列表类型建立
x = np.array(list)
print(x)
#[0 1 2 3]
import numpy as np
从元组类型建立
x = np.array((4,5,6,7))
print(x)
#[4 5 6 7]
- 可以从列表和元组混合类型创建(所包含数据个数相同就可混合使用,一般不建议)
x = np.array([list,(4,5,6,7)],dtype=np.float32)
print(x)
#[0,1,2,3,4,5,6,7]
x = np.arange(10)
print(x)
#[0 1 2 3 4 5 6 7 8 9]
x = np.ones((3,6))
print(x)
#[[1. 1. 1. 1. 1. 1.]
# [1. 1. 1. 1. 1. 1.]
#[1. 1. 1. 1. 1. 1.]]
x = np.ones((3,4,5))
print(x)
#[[[1. 1. 1. 1.]
# [1. 1. 1. 1.]
# [1. 1. 1. 1.]]
# [[1. 1. 1. 1.]
# [1. 1. 1. 1.]
# [1. 1. 1. 1.]]]
x = np.zeros((3,6),dtype = np.int32)
print(x)
#[[0 0 0 0 0 0]
# [0 0 0 0 0 0]
# [0 0 0 0 0 0]]
x = np.eye(5)
print(x)
#[[1. 0. 0. 0. 0.]
# [0. 1. 0. 0. 0.]
# [0. 0. 1. 0. 0.]
# [0. 0. 0. 1. 0.]
# [0. 0. 0. 0. 1.]]
- .full(shape,vale)生成一个shape的矩阵,每个元素都是val
x = np.full((3,4),5)
print(x)
#[[5 5 5 5]
# [5 5 5 5]
# [5 5 5 5]]
- .ones_like(x)根据数组x的shape形成一个全为1的数组
x = full((3,4),5)
a = np.ones_like(x)
print(a)
#[[1 1 1 1]
# [1 1 1 1]
# [1 1 1 1]]
- .zeros_likes(x)根据数组x的shape形成一个全为0的数组
x = full((3,4),5)
a = np.zeros_like(x)
print(a)
#[[0 0 0 0]
# [0 0 0 0]
# [0 0 0 0]]
- .full_likes(x)根据数组x的形状生成一个数组,值为val
x = full((3,4),5)
a = np.full_like(x,0)
print(a)
#[[0 0 0 0]
# [0 0 0 0]
# [0 0 0 0]]
- .linespace(begin,end,val,endpoint)根据起止数据等距的填充数据,形成数组
#endpoint默认为True,表示end是其中的元素
x = np.linespace(1,10,4)
print(x)
#[ 1. 4. 7. 10.]
#endpoint为False,表示end不是其中的元素
x = np.linespace(1,10,4,endpoint=True)
print(x)
#[1. 3.25 5.5 7.75]
- .concatenate()将两个或多个数组合并成一个新的数组,axis默认为0
b = np.full((2,1,3),5)
a = np.full((2,1,3),1)
print(b)
print(a)
#b
#[[[5 5 5]]
#
# [[5 5 5]]]
#a
#[[[1 1 1]]
#
# [[1 1 1]]]
x = np.concatenate((a,b))
print(x)
#[[[1 1 1]]
#
# [[1 1 1]]
#
# [[5 5 5]]
#
# [[5 5 5]]]
x = np.concatenate((a,b),axis=1)
print(x)
#[[[1 1 1]
# [5 5 5]]
#
# [[1 1 1]
# [5 5 5]]]
x = np.concatenate((a,b),axis=2)
print(x)
#[[[1 1 1 5 5 5]]
#
# [[1 1 1 5 5 5]]]