array = np.array([[1,2,3],[2,3,4]])
print(array)
print(array.ndim) #维度
print(array.shape) #形状
print(array.size) #元素个数
print(array.dtype) #数据类型
output:
array:
[[1 2 3]
[2 3 4]]
ndim: 2
shape: (2, 3)
size: 6
dtype: int64
a = np.array([2,3,4])
b = np.zeros((3,4)) #生成0矩阵,中间为shape
c = np.empty((3,4)) #生成一堆接近0的矩阵
d = np.ones((3,4)) #生成1矩阵
e = np.arrange(0,12,1)) # 开始 结束 步长
f = np.linspace(1,10,5) #开始 结束 生成个数+1
output:
a:
[2 3 4]
b:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
d:
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
e:
[ 0 1 2 3 4 5 6 7 8 9 10 11]
f:
[ 1. 3.25 5.5 7.75 10. ]
array1 = np.array([[1, 2],
[3, 4]])
array2 = np.array([[5, 6],
[7, 8]])
print(array1.T)
print(array1.reshape((4,1)))
print(np.concatenate((array1, array2), axis=0))
print(np.concatenate((array1, array2), axis=1))
print(np.split(array1, 2, axis=0))
print(np.split(array1, 2, axis=1))
print(np.dot(array1,array2))
output:
array1.T:
[[1 3]
[2 4]]
array1.reshape:
[[1]
[2]
[3]
[4]]
np.concatenate_axis=0:
[[1 2]
[3 4]
[5 6]
[7 8]]
np.concatenate_axis=1:
[[1 2 5 6]
[3 4 7 8]]
np.split_axis=0:
[array([[1, 2]]), array([[3, 4]])]
np.split_axis=1:
[array([[1],
[3]]),
array([[2],
[4]])]
np.dot:
[[19 22]
[43 50]]
均分布:在概率论和统计学中,均匀分布也叫矩形分布,它是对称概率分布,在相同长度间隔的分布概率是等可能的。
>>> np.random.uniform()
0.3999807403689315
>>> np.random.uniform(5, 6, size=(2,3))
array([[5.82416021, 5.68916836, 5.89708586],
[5.63843125, 5.22963754, 5.4319899 ]])
>>> np.random.randint(8)
5
>>> np.random.randint(8, size=(2,2,3))
array([[[4, 7, 0],
[1, 4, 1]],
[[2, 2, 5],
[7, 6, 4]]])
>>> np.random.choice(5)
3
>>> np.random.choice([0.2, 0.4], p=[1, 0])
0.2
>>> np.random.permutation(5)
array([1, 2, 3, 0, 4])
>>> np.random.permutation([[1,2,3],[4,5,6]])
array([[4, 5, 6],
[1, 2, 3]])
链接:https://www.runoob.com/numpy/numpy-matplotlib.html