Python中的Numpy矩阵操作


Posted in Python onAugust 12, 2018

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 相关文章推荐
记录Django开发心得
Jul 16 Python
Python中还原JavaScript的escape函数编码后字符串的方法
Aug 22 Python
Python获取Linux系统下的本机IP地址代码分享
Nov 07 Python
python实现多线程的方式及多条命令并发执行
Jun 07 Python
Python探索之爬取电商售卖信息代码示例
Oct 27 Python
Python利用公共键如何对字典列表进行排序详解
May 19 Python
Python使用wget实现下载网络文件功能示例
May 31 Python
python sklearn库实现简单逻辑回归的实例代码
Jul 01 Python
python机器学习包mlxtend的安装和配置详解
Aug 21 Python
在notepad++中实现直接运行python代码
Dec 18 Python
Python 发送邮件方法总结
Aug 10 Python
python中字符串String及其常见操作指南(方法、函数)
Apr 06 Python
浅谈python之新式类
Aug 12 #Python
详解Django中类视图使用装饰器的方式
Aug 12 #Python
python中pip的安装与使用教程
Aug 10 #Python
python3判断url链接是否为404的方法
Aug 10 #Python
Python实现数据可视化看如何监控你的爬虫状态【推荐】
Aug 10 #Python
Selenium元素的常用操作方法分析
Aug 10 #Python
Selenium定位元素操作示例
Aug 10 #Python
You might like
关于mysql 字段的那个点为是定界符
2007/01/15 PHP
PHP数组实例详解
2016/06/26 PHP
php版微信返回用户text输入的方法
2016/11/14 PHP
详细解读php的命名空间(一)
2018/02/21 PHP
php curl获取到json对象并转成数组array的方法
2018/05/31 PHP
node.js中的fs.closeSync方法使用说明
2014/12/17 Javascript
javascript事件模型实例分析
2015/01/30 Javascript
jQuery使用CSS()方法给指定元素同时设置多个样式
2015/03/26 Javascript
在Javascript操作JSON对象,增加 删除 修改的简单实现
2016/06/02 Javascript
jQuery 生成svg矢量二维码
2016/08/09 Javascript
js style.display=block显示布局错乱问题的解决方法
2016/09/21 Javascript
Angularjs自定义指令实现三级联动 选择地理位置
2017/02/13 Javascript
jQuery实现QQ空间汉字转拼音功能示例
2017/07/10 jQuery
Vue0.1的过滤代码如何添加到Vue2.0直接使用
2017/08/23 Javascript
vue-router实现tab标签页(单页面)详解
2017/10/17 Javascript
vue axios封装及API统一管理的方法
2019/04/18 Javascript
mock.js模拟前后台交互
2019/07/25 Javascript
layui的面包屑或者表单不显示的解决方法
2019/09/05 Javascript
jquery弹窗时禁止body滚动条滚动的例子
2019/09/21 jQuery
vue实现标签云效果的示例
2020/11/09 Javascript
Python 的 Socket 编程
2015/03/24 Python
Python爬虫使用脚本登录Github并查看信息
2018/07/16 Python
pygame游戏之旅 创建游戏窗口界面
2018/11/20 Python
django删除表重建的实现方法
2019/08/28 Python
在flask中使用python-dotenv+flask-cli自定义命令(推荐)
2020/01/05 Python
基于Python数据结构之递归与回溯搜索
2020/02/26 Python
什么是python的必选参数
2020/06/21 Python
Pycharm无法打开双击没反应的问题及解决方案
2020/08/17 Python
HTML5之web workers_动力节点Java学院整理
2017/07/17 HTML / CSS
介绍一下UNIX启动过程
2013/11/14 面试题
九年级科学教学反思
2014/01/29 职场文书
平安建设工作方案
2014/06/02 职场文书
2014年度个人工作总结
2014/11/07 职场文书
运动会三级跳加油稿
2015/07/21 职场文书
应收账款管理制度
2015/08/06 职场文书
利用Python多线程实现图片下载器
2022/03/25 Python