栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Numpy数组array和矩阵matrix转换方法

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Numpy数组array和矩阵matrix转换方法

1、ndarray转换成matrix

import numpy as np
from numpy import random,mat

r_arr=random.rand(4,4)
print('r_arr',r_arr)
r_mat=mat(r_arr)
print(r_mat.I)#求逆

运行结果:

r_arr [[ 0.65603592  0.39908438  0.44722351  0.92652759]
 [ 0.32357477  0.45384697  0.31687359  0.73861219]
 [ 0.3534119   0.12693696  0.15701767  0.9163409 ]
 [ 0.36515841  0.87377377  0.44150506  0.4605718 ]]
r_mat [[ 0.65603592  0.39908438  0.44722351  0.92652759]
 [ 0.32357477  0.45384697  0.31687359  0.73861219]
 [ 0.3534119   0.12693696  0.15701767  0.9163409 ]
 [ 0.36515841  0.87377377  0.44150506  0.4605718 ]]
[[  2.20907786 -11.21482242   4.73076477   4.12886716]
 [ -1.79239654  -4.48728612   3.52875987   3.78121743]
 [  2.86148809  15.82430961 -11.71577803  -7.82425058]
 [ -1.09402215   2.23536661   0.78545617  -0.77549894]]

import numpy as np
from numpy import random,mat,eye

r_arr=random.rand(4,4)
print('r_arr',r_arr)
r_mat=mat(r_arr)
print('r_mat',r_mat)
r_invmat=r_mat.I
# print(r_mat.I)
r2_mat=r_mat*r_invmat
print('r2_mat',r2_mat)
print('error',r2_mat-eye(4))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/24769.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号