test
2018-11-18 本文已影响0人
心梦帆影
[TOC]
创建矩阵(采用ndarray对象)
# 创建一维的narray对象
a = np.array([1,2,3])
print(a) # [1 2 3]
# 创建二维的narray对象
a = np.array([
[1,2,3],
[4,5,6]
])
print(a) # [[1 2 3] [4 5 6]]
# 创建多维对象以其类推
a = np.array([1,2,3,4,5,6])
print(a) # [1 2 3 4 5 6]
b = a.reshape((2,3))
print(b)
'''
[[1 2 3]
[4 5 6]]
'''
获取矩阵行数列数(二维情况)
a = np.array([
[1,2,3],
[4,5,6]
])
print(a.shape) # (2, 3)
print(a.shape[0]) # 2
print(a.shape[1]) # 3
矩阵的截取
按行列截取
a = np.array([
[1,2,3,4,5],
[6,7,8,9,10]
])
print(a[0:1]) # [[1 2 3 4 5]]
print(a[1,2:4]) # [8 9]
print(a[1,:]) # [ 6 7 8 9 10]
print(a[1:,2:]) # [[ 8 9 10]]
按条件截取
a = np.array([
[1,2,3,4,5],
[6,7,8,9,10]
])
b = a[a%2==0]
print(b) # [ 2 4 6 8 10]
a[a>6] = 0
print(a) # [[1 2 3 4 5] [6 0 0 0 0]]
矩阵的合并
a1 = np.array([
[1,2],
[3,4]
])
a2 = np.array([
[5,6],
[7,8]
])
a = np.hstack([a1, a2])
print(a)
'''
[[1 2 5 6]
[3 4 7 8]]
'''
a = np.vstack([a1, a2])
print(a)
'''
[[1 2]
[3 4]
[5 6]
[7 8]]
'''
通过函数创建矩阵
arange
a = np.arange(5)
print(a) # [0 1 2 3 4]
a = np.arange(0, 5)
print(a) # [0 1 2 3 4]
a = np.arange(0, 10, 2)
print(a) # [0 2 4 6 8]
linspace等差数列
a = np.linspace(0, 1, 7)
print(a) # [0. 0.16666667 0.33333333 0.5 0.66666667 0.83333333 1. ]
logspace等比数列
a = np.logspace(0, 4, 5)
print(a) # [1.e+00 1.e+01 1.e+02 1.e+03 1.e+04]
ones、zeros、eye、empty
a_ones = np.ones((3,4))
print(a_ones)
'''
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
'''
a_zeros = np.zeros((3,4))
print(a_zeros)
'''
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
'''
a_eye = np.eye(3)
print(a_eye)
'''
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
'''
a_empty = np.empty((3,4))
print(a_empty)
矩阵的运算
矩阵的点乘
a1 = np.array([
[1,2,3,4,5],
[6,7,8,9,10],
])
a2 = np.array([
[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12],
[13,14,15]
])
a = a1.dot(a2)
print(a)
'''
[[135 150 165]
[310 350 390]]
'''
矩阵的转置
a = np.array([
[1,2,3],
[4,5,6]
])
b = a.transpose()
print(b)
'''
[[1 4]
[2 5]
[3 6]]
'''
矩阵的逆
import numpy.linalg as lg
a = np.array([
[1,2,3],
[4,5,6],
[7,8,9]
])
b = lg.inv(a)
print(b)
'''
[[-4.50359963e+15 9.00719925e+15 -4.50359963e+15]
[ 9.00719925e+15 -1.80143985e+16 9.00719925e+15]
[-4.50359963e+15 9.00719925e+15 -4.50359963e+15]]
'''
矩阵信息获取
最大最小值
a = np.array([
[1,2,3],
[4,5,6]
])
print(a.max()) # 6
print(a.max(axis=0)) # [4 5 6]
print(a.max(axis=1)) # [3 6]
print(a.min()) # 1
print(a.min(axis=0)) # [1 2 3]
print(a.min(axis=1)) # [1 4]
平均值mean()
a = np.array([
[1,2,3],
[4,5,6]
])
print(a.mean()) # 3.5
print(a.mean(axis=0)) # [2.5 3.5 4.5]
print(a.mean(axis=1)) # [2. 5.]
方差var()
a = np.array([
[1,2,3],
[4,5,6]
])
print(a.var()) # 2.9166666666666665
print(a.var(axis=0)) # [2.25 2.25 2.25]
print(a.var(axis=1)) # [0.66666667 0.66666667]
标准差std()
a = np.array([
[1,2,3],
[4,5,6]
])
print(a.std()) # 1.707825127659933
print(a.std(axis=0)) # [1.5 1.5 1.5]
print(a.std(axis=1)) # [0.81649658 0.81649658]
中值median()
a = np.array([
[1,2,3],
[4,5,6]
])
print(np.median(a)) # 3.5
print(np.median(a, axis=0)) # [2.5 3.5 4.5]
print(np.median(a, axis=1)) # [2. 5.]
求和sum()
a = np.array([
[1,2,3],
[4,5,6]
])
print(a.sum()) # 21
print(a.sum(axis=0)) # [5 7 9]
print(a.sum(axis=1)) # [ 6 15]
累积和cumsum()
a = np.array([
[1,2,3],
[4,5,6]
])
print(a.cumsum()) # [ 1 3 6 10 15 21]
print(a.cumsum(axis=0)) # [[1 2 3] [5 7 9]]
print(a.cumsum(axis=1)) # [[ 1 3 6] [ 4 9 15]]