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 ...
随机推荐
- WMI远程启动软件(某个应用程序)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.M ...
- all unicode
Unicode Chart Range Decimal Name 0x0000-0x007F 0-127 Basic Latin 0x0080-0x00FF 128-255 Latin-1 Suppl ...
- Python3 条件控制
if 语句 Python中if语句的一般形式如下所示: if condition_1: statement_block_1 elif condition_2: statement_block_2 el ...
- Jmeter(十七)_驱动浏览器做GUI测试
jmeter不光可以完成性能测试.接口测试,现在也可以依靠WebDriver来完成GUI的功能自动化测试了,是不是很神奇? 1:下载JMeterPlugins-WebDriver-1.3.1.zip, ...
- JavaScript中的事件模型
JS中的事件 1.鼠标事件 onclick ondbclick onmouseover onmouseout 2.HTML事件 onload onunload onsubmit ...
- Rails里rake db:migrate出现undefined method last_comment问题的解决
这个问题和特定的rake版本有关,因为Rails要使用rake的last_comment方法在较新版本的rake中已被废弃,所以很多人卸载了新版本的rake去安装旧版本的rake. 这样也能解决问题, ...
- Linux 下的一个全新的性能测量和调式诊断工具 Systemtap, 第 3 部分: Systemtap
Systemtap的原理,Systemtap与DTrace比较,以及安装要求和安装步骤本系列文章详细地介绍了一个Linux下的全新的调式.诊断和性能测量工具Systemtap和它所依赖的基础kprob ...
- 20160208.CCPP体系详解(0018天)
程序片段(01):main.c 内容概要:PointWithOutInit #include <stdio.h> #include <stdlib.h> //01.野指针详解: ...
- Web自动化框架LazyUI使用手册(5)--模板工程:LazyUI-template详解
概述: LazyUI-template: 提供Maven管理的,基于Spring+Testng的,包含常用浏览器driver的,方便连接各种数据库的java模板工程,并提供以百度搜索为例的第一个测试用 ...
- UIKit中ImageView动画堆叠显示的微调整
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 网上看到一个PackingList项目(如果需要源代码可以Q我 ...