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

yipeiwu_com6年前Python基础

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))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python requests使用socks5的例子

网络爬虫由于一个ip频繁访问同一网站,容易返回456或者被长时间封禁。 特别的本机有socks5客户端的设置如下,前提是已经安装了socks5的客户端软件,并且启动起来在固定端口为本机提...

python计算auc指标实例

1、安装scikit-learn 1.1Scikit-learn 依赖 Python (>= 2.6 or >= 3.3), NumPy (>= 1.6.1), Sci...

Python使用django框架实现多人在线匿名聊天的小程序

Python使用django框架实现多人在线匿名聊天的小程序

最近看到好多设计类网站,都提供了多人在线匿名聊天的小功能,感觉很有意思,于是基于python的django框架自己写了一个,支持手动实时更名,最下方提供了完整的源码. 在线聊天地址(无...

Dlib+OpenCV深度学习人脸识别的方法示例

Dlib+OpenCV深度学习人脸识别的方法示例

前言 人脸识别在LWF(Labeled Faces in the Wild)数据集上人脸识别率现在已经99.7%以上,这个识别率确实非常高了,但是真实的环境中的准确率有多少呢?我没有这方...

Python version 2.7 required, which was not found in the registry

Python version 2.7 required, which was not found in the registry

安装PIL库的时候,直接提示:Python version 2.7 required, which was not found in the registry。 如图: 大意是说找不到...