numpy教程:矩阵matrix及其运算
http://blog.csdn.net/pipisorry/article/details/48791403
numpy矩阵简介
NumPy函数库中存在两种不同的数据类型(矩阵matrix和数组array),都可以用于处理行列表示的数字元素。虽然它们看起来很相似,但是在这两个数据类型上执行相同的数学运算可能得到不同的结果,其中NumPy函数库中的matrix与MATLAB中matrices等价。
numpy模块中的矩阵对象为numpy.matrix,包括矩阵数据的处理,矩阵的计算,以及基本的统计功能,转置,可逆性等等,包括对复数的处理,均在matrix对象中。
关于numpy中矩阵和二维数组的取舍
matrix是array的分支,matrix和array在很多时候都是通用的,但官方建议如果两个可以通用,那就选择array,因为array更灵活,速度更快,很多人把二维的array也翻译成矩阵。
matrix的优势就是相对简单的运算符号,如矩阵相乘用符号*,但是array相乘得用方法.dot()。
Note: array * mat也是矩阵相乘,而不是点乘。
array的优势就是不仅仅表示二维,还能表示3、4、5...维,而且在大部分Python程序里,array也是更常用的。
Note:
1. numpy中二维数组不支持求逆运算(给gui),但可以使用scripy中的linalg.inv()函数求逆。
2. lz建议使用二维ndarray代替matrix,结合使用scripy.linalg库可以实现全部矩阵运算。[Scipy教程 - 线性代数库linalg]
Matrix objects矩阵对象
创建示例
np.matrix
>>> a = np.matrix(’1 2; 3 4’)
>>> print a
[[1 2]
[3 4]]
>>> np.matrix([[1, 2], [3, 4]])
matrix([[1, 2],
[3, 4]])
Note:
1. class numpy.matrix(data,dtype,copy):返回一个矩阵,其中data为ndarray对象或者字符形式;dtype:为data的type;copy:为bool类型。
2. 矩阵的换行必须是用分号(;)隔开,内部数据必须为字符串形式(‘ ’),矩阵的元素之间必须以空格隔开。
3. 矩阵中的data可以为数组对象。
np.asmatrix
>>> x = np.array([[1, 2], [3, 4]])
>>> m = np.asmatrix(x)
>>> x[0,0] = 5
>>> m
matrix([[5, 2],
[3, 4]])
矩阵对象属性Attribute
矩阵对象方法Methods
[numpy-ref-1.8.1 - 1.6.2 Matrix objects p120]
Matrix矩阵对象方法使用示例
>>> a = np.asmatrix('0 2 7; 3 4 8; 5 0 9')
>>> a.all()
False
>>> a.all(axis=0)
matrix([[False, False, True]], dtype=bool)
>>> a.all(axis=1)
matrix([[False],
[ True],
[False]], dtype=bool)
ü Astype方法
>>> a.astype(float)
matrix([[ 12., 3., 5.],
[ 32., 23., 9.],
[ 10., -14., 78.]])
ü Argsort方法
>>> a=np.matrix('12 3 5; 32 23 9; 10 -14 78')
>>> a.argsort()
matrix([[1, 2, 0],
[2, 1, 0],
[1, 0, 2]])
ü Clip方法
>>> a
matrix([[ 12, 3, 5],
[ 32, 23, 9],
[ 10, -14, 78]])
>>> a.clip(12,32)
matrix([[12, 12, 12],
[32, 23, 12],
[12, 12, 32]])
ü Cumprod方法
>>> a.cumprod(axis=1)
matrix([[ 12, 36, 180],
[ 32, 736, 6624],
[ 10, -140, -10920]])
ü Cumsum方法
>>> a.cumsum(axis=1)
matrix([[12, 15, 20],
[32, 55, 64],
[10, -4, 74]])
ü Tolist方法
>>> b.tolist()
[[12, 3, 5], [32, 23, 9], [10, -14, 78]]
ü Tofile方法
>>> b.tofile('d:\\b.txt')
ü compress()方法
>>> from numpy import *
>>> a = array([10, 20, 30, 40])
>>> condition = (a > 15) & (a < 35)
>>> condition
array([False, True, True, False], dtype=bool)
>>> a.compress(condition)
array([20, 30])
>>> a[condition] # same effect
array([20, 30])
>>> compress(a >= 30, a) # this form a
so exists
array([30, 40])
>>> b = array([[10,20,30],[40,50,60]])
>>> b.compress(b.ravel() >= 22)
array([30, 40, 50, 60])
>>> x = array([3,1,2])
>>> y = array([50, 101])
>>> b.compress(x >= 2, axis=1) # illustrates
the use of the axis keyword
array([[10, 30],
[40, 60]])
>>> b.compress(y >= 100, axis=0)
array([[40, 50, 60]])
The Matrix class numpy矩阵类
建立矩阵
Note: numpy.mat(data, dtype=None) Interpret the input as a matrix.
Unlike matrix, asmatrix does not make a copy if the input is already a matrix or an ndarray. Equivalent to matrix(data, copy=False).
[numpy-ref-1.8.1 - 3.1.7 The Matrix class p484]
Matrix library矩阵库(numpy.matlib)
This module contains all functions in the numpy namespace, with the following replacement functions that return matrices instead of ndarrays.
Functions that are also in the numpy namespace and return matrices
Replacement functions in matlib
[numpy-ref-1.8.1 - 3.21 Matrix library p940]
from:http://blog.csdn.net/pipisorry/article/details/48791403
numpy教程:矩阵matrix及其运算的更多相关文章
- [转]Numpy中矩阵对象(matrix)
numpy模块中的矩阵对象为numpy.matrix,包括矩阵数据的处理,矩阵的计算,以及基本的统计功能,转置,可逆性等等,包括对复数的处理,均在matrix对象中. class numpy.matr ...
- [转]numpy中的matrix矩阵处理
今天看文档发现numpy并不推荐使用matrix类型.主要是因为array才是numpy的标准类型,并且基本上各种函数都有队array类型的处理,而matrix只是一部分支持而已. 这个转载还是先放着 ...
- numpy中的matrix矩阵处理
numpy模块中的矩阵对象为numpy.matrix,包括矩阵数据的处理,矩阵的计算,以及基本的统计功能,转置,可逆性等等,包括对复数的处理,均在matrix对象中. class numpy.matr ...
- numpy教程
[转]CS231n课程笔记翻译:Python Numpy教程 原文链接:https://zhuanlan.zhihu.com/p/20878530 译者注:本文智能单元首发,翻译自斯坦福CS231n课 ...
- 转:Numpy教程
因为用到theano写函数的时候饱受数据结构困扰 于是上网找了一篇numpy教程(theano的数据类型是基于numpy的) 原文排版更好,阅读体验更佳: http://phddreamer.blog ...
- Python numpy中矩阵的用法总结
关于Python Numpy库基础知识请参考博文:https://www.cnblogs.com/wj-1314/p/9722794.html Python矩阵的基本用法 mat()函数将目标数据的类 ...
- numpy中的matrix与array的区别
Numpy matrices必须是2维的,但是 numpy arrays (ndarrays) 可以是多维的(1D,2D,3D····ND). Matrix是Array的一个小的分支,包含于Array ...
- Python 机器学习库 NumPy 教程
0 Numpy简单介绍 Numpy是Python的一个科学计算的库,提供了矩阵运算的功能,其一般与Scipy.matplotlib一起使用.其实,list已经提供了类似于矩阵的表示形式,不过numpy ...
- Numpy入门(二):Numpy数组索引切片和运算
在Numpy中建立了数组或者矩阵后,需要访问数组里的成员,改变元素,并对数组进行切分和计算. 索引和切片 Numpy数组的访问模式和python中的list相似,在多维的数组中使用, 进行区分: 在p ...
随机推荐
- JS中的DOM— —节点以及操作
DOM操作在JS中可以说是非常常见了吧,很多网页的小功能的实现,比如一些元素的增删操作等都可以用JS来实现.那么在DOM中我们需要知道些什么才能完成一些功能的实现呢?今天这篇文章就先简单的带大家入一下 ...
- Docker命令查询
基本语法 docker [OPTIONS] COMMAND [arg...] 一般来说,Docker 命令可以用来管理 daemon,或者通过 CLI 命令管理镜像和容器.可以通过 man docke ...
- Python教学相关资料
Python教学调查链接 一.专题 1.绘图 如何开始使用Python来画图 Python画图总结 2.科学计算与数据分析 3.可视化 4.网络爬虫 5. 做笔记 Python-Jupyter Not ...
- Lua热更新时正确设置文件名
Lua热更新时正确设置文件名(金庆的专栏 2016.12)Lua热更新模块见:https://github.com/jinq0123/hotfix其中使用 load(chunk) 来加载更新后的内容, ...
- 在Spring Boot中使用数据缓存
春节就要到了,在回家之前要赶快把今年欠下的技术债还清.so,今天继续.Spring Boot前面已经预热了n篇博客了,今天我们来继续看如何在Spring Boot中解决数据缓存问题.本篇博客是以初识在 ...
- Java Web前端到后台常用框架介绍
一.SpringMVC http://blog.csdn.net/evankaka/article/details/45501811 Spring Web MVC是一种基于Java的实现了Web MV ...
- 1小时学会JQuery
---------------------------------------------------------------------------------------------------- ...
- 将树形递归转换为loop
class Stack(object): def __init__(self,**kwargs): self.__dict__.update(kwargs) def __str__(self): re ...
- iOS下JS与OC互相调用(七)--Cordova 基础
Cordova 简介 在介绍Cordova之前,必须先提一下PhoneGap.PhoneGap 是Nitobi软件公司2008年推出的一个框架,旨在弥补web 和iOS 之间的不足,使得web 和 i ...
- ASCII 大文字生成器
display text in large ASCII art fonts 显示大ASCII艺术字体 这种东西在源码声明或者软件初始化控制台打印时候很有用. 例如打开: http://www.oran ...