矩阵的花式创建与运算-1

一.矩阵的创建

1.直接用分号隔开的字符串

mat=np.mat("1 2 3;4 5 6;7 8 9")
print(mat,type(mat))------------------------><class 'numpy.matrixlib.defmatrix.matrix'>

print(mat)---------------------------------->[[1 2 3]
                                              [4 5 6]
                                              [7 8 9]]

2.使用numpy数组创建矩阵

arr=np.arange(1,10).reshape(3,3)
print(arr)---------------------------------->[[1 2 3]
                                              [4 5 6]
                                              [7 8 9]]
print(type(arr))----------------------------><class 'numpy.ndarray'>                                              
mat2=np.mat(arr)
print(mat2)--------------------------------->[[1 2 3]
                                              [4 5 6]
                                              [7 8 9]]
print(type(mat2))------------------------><class 'numpy.matrixlib.defmatrix.matrix'>                                              

3.从已有的矩阵中通过bmat函数创建矩阵

A=np.eye(2)
B=A*2
mat3=np.bmat("A B;B A")
print(A)--------------------------------->[[1. 0.]
                                           [0. 1.]]

print(B)--------------------------------->[[2. 0.]
                                           [0. 2.]]  

print(mat3)------------------------------>[[1. 0. 2. 0.]
                                           [0. 1. 0. 2.]
                                           [2. 0. 1. 0.]
                                           [0. 2. 0. 1.]]

二.矩阵的创建

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
1.创建一个服从正态分布的随机矩阵

normal=np.random.normal(size=100)
print(normal)------------------------------->
[ 0.03369914  0.30235076  1.19459525  1.04386579  0.6785404  -0.74902908
 -1.42411325 -0.17561079  0.5662878   0.71190807]

2.产生二行三列均匀分布随机数组

D=np.mat(np.random.rand(2,3))
print(D)------------------------------------>[[0.95951752 0.54479398 0.05657922]
                                              [0.38570468 0.33659269 0.45107454]]

3.创建一个服从beta分布的随机样本

beta=np.random.beta(a=.5,b=.5,size=500)
plt.hist(beta)#画图
plt.show()#展示画的图

这里写图片描述
4.产生4x4随机正态分布样本

normal2=np.random.normal(size=(4,4))
print(normal2.shape)------------------------->(4, 4)
print("normal-4x4",normal2)------------------>normal-4x4 
[[ 1.1201248   0.76547566 -0.61607581  0.3227142 ]
 [-0.70998697 -0.90585552 -1.24078868 -1.13888357]
 [ 0.21957749 -0.7173875   0.63399343 -0.94131284]
 [ 1.21531217 -0.27100659 -1.39307201 -0.81648764]]

plt.hist(normal2)
plt.show()

这里写图片描述
5.产生在某个范围内的随机整数数组\矩阵

numbers=np.random.randint(1,50,[5,5])#1-50内随机数字的五行五列数组
print(numbers)------------------------------->[[34 29 16 47 39]
                                               [26  8 35 22  4]
                                               [19 28 38 14 12]
                                               [ 3 47 26 33 35]
                                               [49 30 18 13  9]]

6.产生0—1直接的随机浮点数

floatNum=np.mat(np.random.random(10))
print(floatNum)------------------------------>
[[0.02678265 0.28299231 0.62277557 0.41339499 0.77849441 0.76525023
  0.69208797 0.12734735 0.49415779 0.12956403]]

7.在某个范围之内的所有数中随机抽取一个

num=np.random.choice(4,5)     #   5以内随机取4个数
num1=np.random.choice(4,(2,3))#   4以内随机数,组成二行三列数组
print(num)---------------------------------->[0 3 2 3 3]

print(num1)--------------------------------->[[2 0 1]
                                              [2 2 2]]

三.矩阵的算术运算

一元:
这里写图片描述

二元:
这里写图片描述
1.矩阵的加法
np.add(mat1,mat2)

mat1=np.mat(np.array([2,6,5]))
mat2=np.mat(np.array([2,6,5]))
print(mat1)--------------------------------->[[2 6 5]]
print(mat2)--------------------------------->[[2 6 5]]

addresult=np.add(mat1,mat2)
print(addresult)---------------------------->[[ 4 12 10]]
print(type(mat2))---------------------------><class 'numpy.matrixlib.defmatrix.matrix'>

2.数组的乘法
np.multiply(mat1,mat2)

mulresult=np.multiply(mat1,mat2)
print(mulresult)---------------------------->[[ 4 36 25]]

矩阵的乘法(第一个矩阵行数m*p和第二个矩阵列数n*m一定要一样,结果是p*n)
mat3=np.mat(np.arange(6).reshape(2,3))------>[[0 1 2]
                                              [3 4 5]]

mat4=np.mat(np.arange(6).reshape(3,2))------>[[0 1]
                                              [2 3]
                                              [4 5]]

print("mat3*mat4=\n",mat3*mat4)------------->mat3*mat4=[[10 13]
                                                        [28 40]]

3.数组除法(矩阵除法)

a=np.mat(np.array([4,5,8]))----------------->[[4,5,8]]
b=np.mat(np.array([2,3,5]))----------------->[[2,3,5]]

result=np.divide(a,b)
print(result)------------------------------->[[2.         1.66666667 1.6       ]]
数组除法并将结果向下取整  np.floor_divide(a,b)  # 即取商整数部分
result2=np.floor_divide(a,b)
print("相除向下取整:",result2)---------------->相除向下取整: [[2 1 1]]
print("相除向下取整:",a//b)------------------->相除向下取整: [[2 1 1]]

4.矩阵相除返回结果包含小数部分(和divide结果一样)
np.true_divide(a,b)

floatResult=np.true_divide(a,b)
print("返回除法的浮点数结果",floatResult)------->
                                      返回除法的浮点数结果 [[2.         1.66666667 1.6       ]]

5.取模运算
np.remainder() , % , np.mod()

mat1=np.mat(np.array([5,-7,9]))----------->[[5,-7,9]]
mat2=np.mat(np.array([2,-4,3]))----------->[[2,-4,3]]
result1=np.remainder(mat1,mat2)
print(result1)---------------------------->[[ 1 -3  0]]

result2=np.mod(mat1,mat2)
print(result2)---------------------------->[[ 1 -3  0]]

result3=mat1%mat2
print(result3)---------------------------->[[ 1 -3  0]]

6.取绝对值
np.abs(arr1)

mat=np.mat(np.array([-4,2,-6,12,0,9]))---->[-4,2,-6,12,0,9]

print(np.abs(mat))------------------------>[[ 4  2  6 12  0  9]]

print(np.fabs(mat))----------------------->[[ 4.  2.  6. 12.  0.  9.]]
#mat里面值为非复数

7.获取mat矩阵中各元素正负号# 正取1 , 负取-1 , 0取0

sign=np.sign(mat)
print("sign:",sign)---------------------->sign: [[-1  1 -1  1  0  1]]

8.将数组中的小数和整数部分抽取出来

arr=np.array([[1.2,3.14],
              [-2.5,6.8]])
arr1,arr2=np.modf(arr)
print("小数部分:",arr1)------------------->小数部分: [[ 0.2   0.14],
                                                    [-0.5   0.8 ]]

print("整数部分:",arr2)------------------->整数部分: [[ 1.  3.]
                                                    [-2.  6.]]

9.平方及平方根

平方:
np.square( [ [1 2 3] ]------------------->[[1. 4. 9.]]
平方根:
np.sqrt( [ [4 16 25] ] ) ---------------->[[2. 4. 5.]]

10.矩阵的幂运算
np.power(a,b)—–>a**b

mat=np.mat(np.array([1,2,3,4,5]))
I=np.mat(np.array([2,3,2,1,3]))
print("power:",np.power(mat,I))---------->power: [[  1   8   9   4 125]]

11.获取两个数组对应元素的最大值和最小值,返回至一个新的数组中
np.maximum() , np.minimum()

d2=np.array([[22,15],[5.4,6.6]])--------->[[22 , 15 ],
                                           [5.4, 6.6]]
d3=np.array([[15,18],[7.9,4.0]])--------->[[15 , 18 ],
                                           [7.9, 4.0]]
maxiumun=np.maximum(d2,d3)
print("maxiumn:\n",maxiumun)------------->maxiumn:
        #谁大取谁                                 [[22.  18.]
                                                  [7.9  6.6]]
minimum=np.minimum(d2,d3)
print("minimum:\n",minimum)-------------->minimum:
        #谁小取谁                                 [[15.  15.]
                                                  [5.4  4. ]]

12.数组比较函数,对应元素进行比较
np.greater(d2,d3) , d2>d3 就输出True,反之则输出False

result=np.greater(d2,d3)
print(result)---------------------------->[[ True False]
                                           [False  True]]

13.like函数复制数组框架

a=np.arange(4).reshape(2,2)
print(a)--------------------------------->[[0 1]
                                           [2 3]]
like_a=np.zeros_like(a)
print("like_a:",like_a)------------------>like_a: [[0 0]
                                                   [0 0]]

复制值

like_a.flat=42
print("result:",like_a)------------------>result: [[42 42]
                                                   [42 42]]

运用函数
np.frompyfunc(func,nin,nout)

def like(ndarray):
    result=np.zeros_like(ndarray)
    result=42
    return result
#调用numpy创建通用函数的方法
myfunc=np.frompyfunc(like,1,1) #输入1个输出1个
test=myfunc(np.arange(9).reshape(3,3))
print(test)------------------------------->[[42 42 42]
                                            [42 42 42]
                                            [42 42 42]]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值