Numpy

通过观察Python的自有数据类型,我们可以发现Python原生并不提供多维数组的操作,那么为了处理矩阵,就需要使用第三方提供的相关的包。

NumPy 是一个非常优秀的提供矩阵操作的包。NumPy的主要目标,就是提供多维数组,从而实现矩阵操作。

NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In NumPy dimensions are called axes.

基本操作

#######################################
# 创建矩阵
#######################################
from numpy import array as matrix, arange # 创建矩阵
a = arange(15).reshape(3,5)
a # Out[10]:
# array([[0., 0., 0., 0., 0.],
# [0., 0., 0., 0., 0.],
# [0., 0., 0., 0., 0.]]) b = matrix([2,2])
b # Out[33]: array([2, 2]) c = matrix([[1,2,3,4,5,6],[7,8,9,10,11,12]], dtype=int)
c # Out[40]:
# array([[ 1, 2, 3, 4, 5, 6],
# [ 7, 8, 9, 10, 11, 12]])
#######################################
# 创建特殊矩阵
#######################################
from numpy import zeros, ones,empty z = zeros((3,4))
z # Out[43]:
# array([[0., 0., 0., 0.],
# [0., 0., 0., 0.],
# [0., 0., 0., 0.]]) o = ones((3,4))
o # Out[46]:
# array([[1., 1., 1., 1.],
# [1., 1., 1., 1.],
# [1., 1., 1., 1.]]) e = empty((3,4))
e # Out[47]:
# array([[0., 0., 0., 0.],
# [0., 0., 0., 0.],
# [0., 0., 0., 0.]])
#######################################
# 矩阵数学运算
#######################################
from numpy import array as matrix, arange a = arange(9).reshape(3,3)
a # Out[10]:
# array([[0, 1, 2],
# [3, 4, 5],
# [6, 7, 8]]) b = arange(3)
b # Out[14]: array([0, 1, 2]) a + b # Out[12]:
# array([[ 0, 2, 4],
# [ 3, 5, 7],
# [ 6, 8, 10]]) a - b # array([[0, 0, 0],
# [3, 3, 3],
# [6, 6, 6]]) a * b # Out[11]:
# array([[ 0, 1, 4],
# [ 0, 4, 10],
# [ 0, 7, 16]]) a < 5 # Out[12]:
# array([[ True, True, True],
# [ True, True, False],
# [False, False, False]]) a ** 2 # Out[13]:
# array([[ 0, 1, 4],
# [ 9, 16, 25],
# [36, 49, 64]], dtype=int32) a += 3
a # Out[17]:
# array([[ 3, 4, 5],
# [ 6, 7, 8],
# [ 9, 10, 11]])
#######################################
# 矩阵内置操作
#######################################
from numpy import array as matrix, arange a = arange(9).reshape(3,3)
a # Out[10]:
# array([[0, 1, 2],
# [3, 4, 5],
# [6, 7, 8]]) a.max() # Out[23]: 8 a.min() # Out[24]: 0 a.sum() # Out[25]: 36
#######################################
# 矩阵索引、拆分、遍历
#######################################
from numpy import array as matrix, arange a = arange(25).reshape(5,5)
a # Out[9]:
# array([[ 0, 1, 2, 3, 4],
# [ 5, 6, 7, 8, 9],
# [10, 11, 12, 13, 14],
# [15, 16, 17, 18, 19],
# [20, 21, 22, 23, 24]]) a[2,3] # 取第3行第4列的元素 # Out[3]: 13 a[0:3,3] # 取第1到3行第4列的元素 # Out[4]: array([ 3, 8, 13]) a[:,2] # 取所有第二列元素 # Out[7]: array([ 2, 7, 12, 17, 22]) a[0:3,:] # 取第1到3行的所有列 # Out[8]:
# array([[ 0, 1, 2, 3, 4],
# [ 5, 6, 7, 8, 9],
# [10, 11, 12, 13, 14]]) a[-1] # 取最后一行 # Out[10]: array([20, 21, 22, 23, 24]) for row in a: # 逐行迭代
print(row) # [0 1 2 3 4]
# [5 6 7 8 9]
# [10 11 12 13 14]
# [15 16 17 18 19]
# [20 21 22 23 24] for element in a.flat: # 逐元素迭代,从左到右,从上到下
print(element) # 0
# 1
# 2
# 3
# ...
#######################################
# 改变矩阵
#######################################
from numpy import array as matrix, arange b = arange(20).reshape(5,4) b # Out[18]:
# array([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11],
# [12, 13, 14, 15],
# [16, 17, 18, 19]]) b.ravel() # Out[16]:
# array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
# 17, 18, 19]) b.reshape(4,5) # Out[17]:
# array([[ 0, 1, 2, 3, 4],
# [ 5, 6, 7, 8, 9],
# [10, 11, 12, 13, 14],
# [15, 16, 17, 18, 19]]) b.T # reshape 方法不改变原矩阵的值,所以需要使用 .T 来获取改变后的值 # Out[19]:
# array([[ 0, 4, 8, 12, 16],
# [ 1, 5, 9, 13, 17],
# [ 2, 6, 10, 14, 18],
# [ 3, 7, 11, 15, 19]])
#######################################
# 合并矩阵
#######################################
from numpy import array as matrix,newaxis
import numpy as np d1 = np.floor(10*np.random.random((2,2)))
d2 = np.floor(10*np.random.random((2,2))) d1 # Out[7]:
# array([[1., 0.],
# [9., 7.]]) d2 # Out[9]:
# array([[0., 0.],
# [8., 9.]]) np.vstack((d1,d2)) # 按列合并 # Out[10]:
# array([[1., 0.],
# [9., 7.],
# [0., 0.],
# [8., 9.]]) np.hstack((d1,d2)) # 按行合并 # Out[11]:
# array([[1., 0., 0., 0.],
# [9., 7., 8., 9.]]) np.column_stack((d1,d2)) # 按列合并 # Out[13]:
# array([[1., 0., 0., 0.],
# [9., 7., 8., 9.]]) c1 = np.array([11,12])
c2 = np.array([21,22]) np.column_stack((c1,c2)) # Out[14]:
# array([[11, 21],
# [12, 22]]) c1[:,newaxis] # 添加一个“空”列 # Out[18]:
# array([[11],
# [12]]) np.hstack((c1,c2)) # Out[27]: array([11, 12, 21, 22]) np.hstack((c1[:,newaxis],c2[:,newaxis])) # Out[28]:
# array([[11, 21],
# [12, 22]])

参考

  1. NumPy官方文档

Python中的矩阵操作的更多相关文章

  1. 关于python中的矩阵乘法(array和mat类型)

    关于python中的矩阵乘法,我们一般有两种数据格式可以实现:np.array()类型和np.mat()类型: 对于这两种数据类型均有三种操作方式: (1)乘号 * (2)np.dot() (3)np ...

  2. python中的矩阵、多维数组----numpy

    https://docs.scipy.org/doc/numpy-dev/user/quickstart.html  (numpy官网一些教程) numpy教程:数组创建 python中的矩阵.多维数 ...

  3. python中numpy矩阵运算操作大全(非常全)!

    python中numpy矩阵运算操作大全(非常全) //2019.07.10晚python矩阵运算大全1.矩阵的输出形式:对于任何一个矩阵,python输出的模板是:import numpy as n ...

  4. [转]Python中的矩阵转置

    Python中的矩阵转置 via 需求: 你需要转置一个二维数组,将行列互换. 讨论: 你需要确保该数组的行列数都是相同的.比如: arr = [[1, 2, 3], [4, 5, 6], [7, 8 ...

  5. python中的赋值操作和复制操作

    之前一直写C#,变量之间赋值相当于拷贝,修改拷贝变量不会改变原来的值.但是在python中发现赋值操作本质是和C++中的引用类似,即指向同一块内存空间.下面通过一个例子说明: p=[0,1,2,3,4 ...

  6. python中的赋值操作

    参考:https://www.cnblogs.com/andywenzhi/p/7453374.html?tdsourcetag=s_pcqq_aiomsg(写的蛮好) python中的赋值操作“=” ...

  7. python中的日志操作和发送邮件

    1.python中的日志操作 安装log模块:pip install nnlog 参数:my_log = nnlog.Logger('server_log.log',level='debug',bac ...

  8. python中OS模块操作文件和目录

    在python中执行和操作目录和文件的操作是通过内置的python OS模块封装的函数实现的. 首先导入模块,并查看操作系统的类型: >>> import os os.name # ...

  9. Python中的字符串操作总结(Python3.6.1版本)

    Python中的字符串操作(Python3.6.1版本) (1)切片操作: str1="hello world!" str1[1:3] <=> 'el'(左闭右开:即是 ...

随机推荐

  1. java反射机制执行命令

    public class Encryptor{ public static void main(String[] args) throws IOException, ClassNotFoundExce ...

  2. 五子棋项目总结 JavaScript+jQuery(插件写法)+bootstrap(模态框)

    Html部分(界面): 1.五子棋棋盘由canvas完成: 2.两个按钮,样式由bootstrap完成: 3.菜单按钮对应的模态框,可以选择游戏模式:玩家自由对战,和电脑对战,还可以指定谁先执子和哪个 ...

  3. 移动端h5列表页上拉加载更多

    背景 上星期公司要求做一个回收书籍的h5给安卓用,里面有一个功能是回收记录列表.设计师那边出的稿子是没有要求分页或者是上拉刷新的,但是众所周知,列表页数据很多的情况下,h5加载是很慢的.所以我一开始是 ...

  4. PAT——1052. 卖个萌

    萌萌哒表情符号通常由“手”.“眼”.“口”三个主要部分组成.简单起见,我们假设一个表情符号是按下列格式输出的: [左手]([左眼][口][右眼])[右手] 现给出可选用的符号集合,请你按用户的要求输出 ...

  5. HDU 1102(Constructing Roads)(最小生成树之prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...

  6. iOS:UITableView相关(18-10-20更)

    UITableView用得较多,遇到的情况也较多,单独记录一篇. 一.零散的技巧 二.取cell 三.cell高度 四.导航栏.TableView常见问题相关 五.自定义左滑删除按钮图片 六.仅做了解 ...

  7. Java并发编程(九)线程间协作(下)

    上篇我们讲了使用wait()和notify()使线程间实现合作,这种方式很直接也很灵活,但是使用之前需要获取对象的锁,notify()调用的次数如果小于等待线程的数量就会导致有的线程会一直等待下去.这 ...

  8. VS2015调试,签名时出错: 未在路径 C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\signtool.exe 找到 SignTool.exe

    1.发布项目是出现这个错误网上找了有两种方式, 一种是重新安装VS2015的ClickOnce程序 第二种是修改项目文件的签名 右击项目文件的属性,选择签名,然后把红框内去掉,保存即可.

  9. Spring retry实践

    在开发中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络波动出现超时而采取重试手段......可以看见重试操作是非常常见的一种处理问题, ...

  10. C# 依据鼠标坐标取网页内成员坐标.ie

    C# 根据鼠标坐标取网页内成员坐标.ie 有时候你需要后台获取ie浏览器 鼠标所在位置的元素坐标,然而你使用屏幕坐标是不可行的 所以我们需要把坐标转换成浏览器内坐标 然后再通过elementFromP ...