import numpy as np
np.array([1,2,3])
array([1, 2, 3])
np.array([[1,2,3],[4,5,6]])
array([[1, 2, 3],
[4, 5, 6]])
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
arr
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
把它给看成一个矩阵,或者看成一个ndarray数组的话,我们去获取他的形状.
arr = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
arr.shape
(4, 3)
arr.shape[0]
print(arr.shape[0])
20
arr.shape[1]
3
切割矩阵
arr = np.array([1,2,3])
arr
arr[:]
array([1, 2, 3])
arr = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
arr
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
arr[:,:]
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
arr[1:2,:]
array([[4, 5, 6]])
arr[1:2,1:2]
array([[5]])
arr[1:2,1:10000]
array([[5, 6]])
arr[1:2,[1,2]]
array([[5, 6]])
arr[1:2,(1,2)]
array([[5, 6]])
矩阵元素的替换
arr = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
arr
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
l = [4,5,6]
l[1] = 0
l
[4, 0, 6]
# arr[1:2,:] = 0
# arr
arr1 = arr.copy()
arr1[1:2,:] = 0
arr1
array([[ 1, 2, 3],
[ 0, 0, 0],
[ 7, 8, 9],
[10, 11, 12]])
arr
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
arr[(1,),(1,)] = 0
arr
array([[ 1, 2, 3],
[ 4, 0, 6],
[ 7, 8, 9],
[10, 11, 12]])
矩阵的合并
l1 = [1,2,3]
l2 = [4,5,6]
# l1.extend(l2)
# l1
l1+l2
[1, 2, 3, 4, 5, 6]
arr1 = np.array([[1, 2], [3, 4], [5, 6]])
arr1
array([[1, 2],
[3, 4],
[5, 6]])
arr2 = np.array([[7, 8,8], [9, 10,9], [11, 12,10]])
arr2
array([[ 7, 8, 8],
[ 9, 10, 9],
[11, 12, 10]])
np.hstack((arr1,arr2)) # h=horizontal水平的
array([[ 1, 2, 7, 8, 8],
[ 3, 4, 9, 10, 9],
[ 5, 6, 11, 12, 10]])
np.hstack([arr1,arr2])
array([[ 1, 2, 7, 8, 8],
[ 3, 4, 9, 10, 9],
[ 5, 6, 11, 12, 10]])
np.vstack((arr1,arr2)) # v=vertical垂直的
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-53-4122b6300983> in <module>
----> 1 np.vstack((arr1,arr2)) # v=vertical垂直的
d:\python36\lib\site-packages\numpy\core\shape_base.py in vstack(tup)
281 """
282 _warn_for_nonsequence(tup)
--> 283 return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
284
285
ValueError: all the input array dimensions except for the concatenation axis must match exactly
arr1 = np.array([[1, 2,3], [3, 4,4], [5, 6,4]])
arr1
array([[1, 2, 3],
[3, 4, 4],
[5, 6, 4]])
arr2 = np.array([[7, 8,8], [9, 10,9], [11, 12,10]])
arr2
array([[ 7, 8, 8],
[ 9, 10, 9],
[11, 12, 10]])
np.vstack((arr1,arr2)) # v=vertical垂直的
array([[ 1, 2, 3],
[ 3, 4, 4],
[ 5, 6, 4],
[ 7, 8, 8],
[ 9, 10, 9],
[11, 12, 10]])
通过函数创建矩阵
range(10)
range(0, 10)
list(range(5,10,2))
[5, 7, 9]
np.arange(10,20,2)
array([10, 12, 14, 16, 18])
# 取头也取尾
arr = np.linspace(1,10,20)
arr
array([ 1. , 1.47368421, 1.94736842, 2.42105263, 2.89473684,
3.36842105, 3.84210526, 4.31578947, 4.78947368, 5.26315789,
5.73684211, 6.21052632, 6.68421053, 7.15789474, 7.63157895,
8.10526316, 8.57894737, 9.05263158, 9.52631579, 10. ])
len(arr)
20
zeros/ones/empty
np.zeros((3,2)) # zeros零
array([[0., 0.],
[0., 0.],
[0., 0.]])
np.ones((3,2)) # ones一
array([[1., 1.],
[1., 1.],
[1., 1.]])
np.empty((3,3)) # 随机元素的矩阵
array([[0.00000000e+000, 0.00000000e+000, 0.00000000e+000],
[0.00000000e+000, 0.00000000e+000, 7.37145944e-321],
[8.70018274e-313, 2.22507386e-306, 3.91786943e-317]])
np.eye(4) # I=1
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
np.eye(7)
array([[1., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0., 0., 1.]])
矩阵的运算
列表无法进行+-*/运算,但是矩阵是可以的.
l1 =[1,3,4]
l1*2
[1, 3, 4, 1, 3, 4]
l1+l1
[1, 3, 4, 1, 3, 4]
arr2 = np.array([[7, 8,8], [9, 10,9], [11, 12,10]])
arr2
array([[ 7, 8, 8],
[ 9, 10, 9],
[11, 12, 10]])
arr2*2
array([[14, 16, 16],
[18, 20, 18],
[22, 24, 20]])
arr2/2
array([[3.5, 4. , 4. ],
[4.5, 5. , 4.5],
[5.5, 6. , 5. ]])
arr2%2
array([[1, 0, 0],
[1, 0, 1],
[1, 0, 0]], dtype=int32)
np.sin(arr2)
array([[ 0.6569866 , 0.98935825, 0.98935825],
[ 0.41211849, -0.54402111, 0.41211849],
[-0.99999021, -0.53657292, -0.54402111]])
矩阵函数 | 详解 |
---|---|
np.sin(arr) | 对矩阵arr中每个元素取正弦, |
np.cos(arr) | 对矩阵arr中每个元素取余弦, |
np.tan(arr) | 对矩阵arr中每个元素取正切, |
np.arcsin(arr) | 对矩阵arr中每个元素取反正弦, |
np.arccos(arr) | 对矩阵arr中每个元素取反余弦, |
np.arctan(arr) | 对矩阵arr中每个元素取反正切, |
np.exp(arr) | 对矩阵arr中每个元素取指数函数, |
np.sqrt(arr) | 对矩阵arr中每个元素开根号 |
np.sqrt(arr2)
array([[2.64575131, 2.82842712, 2.82842712],
[3. , 3.16227766, 3. ],
[3.31662479, 3.46410162, 3.16227766]])
矩阵的点乘
arr1 = np.array([[1, 2,3], [3, 4,4], [5, 6,4]])
arr1
array([[1, 2, 3],
[3, 4, 4],
[5, 6, 4]])
arr2 = np.array([[1, 2,3], [3, 4,4], [5, 6,4]])
arr2
array([[1, 2, 3],
[3, 4, 4],
[5, 6, 4]])
arr1.dot(arr2)
array([[22, 28, 23],
[35, 46, 41],
[43, 58, 55]])
矩阵的转置
arr2 = np.array([[1, 2,3]])
arr2
array([[1, 2, 3]])
arr2.T
array([[1],
[2],
[3]])
矩阵的逆
arr2 = np.array([[1, 2,3],[4,5,6],[7,8,9]])
arr2
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
np.linalg.inv(arr2)
---------------------------------------------------------------------------
LinAlgError Traceback (most recent call last)
<ipython-input-104-12b0a2fff5c3> in <module>
----> 1 np.linalg.inv(arr2)
d:\python36\lib\site-packages\numpy\linalg\linalg.py in inv(a)
549 signature = 'D->D' if isComplexType(t) else 'd->d'
550 extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
--> 551 ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
552 return wrap(ainv.astype(result_t, copy=False))
553
d:\python36\lib\site-packages\numpy\linalg\linalg.py in _raise_linalgerror_singular(err, flag)
95
96 def _raise_linalgerror_singular(err, flag):
---> 97 raise LinAlgError("Singular matrix")
98
99 def _raise_linalgerror_nonposdef(err, flag):
LinAlgError: Singular matrix
其他的用法
arr2 = np.array([[1, 2,3],[4,5,6],[7,8,9]])
arr2
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
arr2.min()
1
arr2.max()
9
arr2.mean()
5.0
arr2.var()
6.666666666666667
函数名称 | 函数功能 | 参数说明 |
---|---|---|
rand() | 产生均匀分布的随机数 | 为第n维数据的维度 |
randn() | 产生标准正态分布随机数 | 为第n维数据的维度 |
randint(low[, high, size, dtype]) | 产生随机整数 | low:最小值;high:最大值;size:数据个数 |
random_sample([size]) | 在$[0,1)$内产生随机数 | size为随机数的shape,可以为元祖或者列表 |
choice(a[, size]) | 从arr中随机选择指定数据 | arr为1维数组;size为数据形状 |
np.random.randint(1,10,(3,3))
array([[8, 5, 6],
[7, 5, 3],
[6, 4, 6]])
np.random.randn(3,2)
array([[ 1.49020068e+00, -5.66224782e-01],
[-1.26022246e+00, 1.41537705e+00],
[-1.99081209e-03, 2.05245204e+00]])