numpy常用代码总结

1、矩阵特征值和特征向量

下面这段代码展示了用numpy求矩阵特征值和特征向量的方法:

import numpy as np
from numpy import linalg as LA

a = np.array([[-2, -36, 0], [-36, -23, 0], [0, 0, 3]])
w, v = LA.eig(a)

输出结果:

w
array([ 25., -50.,   3.])

v
array([[ 0.8,  0.6,  0. ],
       [-0.6,  0.8,  0. ],
       [ 0. ,  0. ,  1. ]])

 

2、点乘法和矩阵乘法

import numpy as np

'''
a = 1  2
    3  4
    5  6
    
b = 1
    2
'''
a = np.array([[1, 2], [3, 4], [5, 6]])
b = np.array([1, 2])

c = np.dot(a, b)
print('dot product of a and b is ', c)
d = np.matmul(a, b)
print('matrix multiplicity is ', d)


x1 = np.random.randint(1, 10, size=(1, 2))
'''
change the statement like this:
x1 = np.random.randint(1, 10, size=(1, 2))
you will get the same result.
'''

x2 = np.random.randint(1, 10, size=(2))
x3 = np.dot(x1, x2)
print("x1 is ", x1, "its shape is ", x1.shape)
print("x2 is ", x2, "its shape is ", x2.shape)
print("x1 * x2 = ", x3)

 3、洗牌

import numpy as np

a = np.random.randint(1, 10, 10)
print(a)
np.random.shuffle(a)
print(a)

输出:

[6 4 6 8 4 4 5 6 8 2]
[6 6 5 6 8 2 4 8 4 4]

 

转载于:https://www.cnblogs.com/licb/p/9661255.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值