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 相关文章推荐
python字典get()方法用法分析
Apr 17 Python
Python中用PIL库批量给图片加上序号的教程
May 06 Python
Python的randrange()方法使用教程
May 15 Python
Python函数中*args和**kwargs来传递变长参数的用法
Jan 26 Python
Python数据分析之双色球统计两个红和蓝球哪组合比例高的方法
Feb 03 Python
python3+PyQt5重新实现QT事件处理程序
Apr 19 Python
解决pycharm运行出错,代码正确结果不显示的问题
Nov 30 Python
Python实现的旋转数组功能算法示例
Feb 23 Python
Python函数的定义方式与函数参数问题实例分析
Dec 26 Python
pytorch实现onehot编码转为普通label标签
Jan 02 Python
python检查目录文件权限并修改目录文件权限的操作
Mar 11 Python
详解pandas赋值失败问题解决
Nov 29 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
URL Rewrite的设置方法
2007/01/02 PHP
PHP函数spl_autoload_register()用法和__autoload()介绍
2012/02/04 PHP
PHP实现抓取Google IP并自动修改hosts文件
2015/02/12 PHP
Zend Framework实现留言本分页功能(附demo源码下载)
2016/03/22 PHP
全面解析PHP面向对象的三大特征
2017/06/10 PHP
PHP双向链表定义与用法示例
2018/01/31 PHP
win10下 php安装seaslog扩展的详细步骤
2020/12/04 PHP
JavaScript中使用正则匹配多条,且获取每条中的分组数据
2010/11/30 Javascript
使用JSON.parse将json字符串转换成json对象的时候会出错
2014/09/04 Javascript
JavaScript各类型的关系图解
2015/10/16 Javascript
基于jQuery实现动态搜索显示功能
2016/05/05 Javascript
简单解析JavaScript中的__proto__属性
2016/05/10 Javascript
基于Bootstrap仿淘宝分页控件实现代码
2016/11/07 Javascript
vue2.5.2使用http请求获取静态json数据的实例代码
2018/02/27 Javascript
Angular6 用户自定义标签开发的实现方法
2019/01/08 Javascript
vue结合element-ui使用示例
2019/01/24 Javascript
Python对两个有序列表进行合并和排序的例子
2014/06/13 Python
介绍Python的Urllib库的一些高级用法
2015/04/30 Python
python2.7无法使用pip的解决方法(安装easy_install)
2018/04/03 Python
python调用OpenCV实现人脸识别功能
2018/05/25 Python
pybind11在Windows下的使用教程
2019/07/04 Python
python获取点击的坐标画图形的方法
2019/07/09 Python
pyqt5 textEdit、lineEdit操作的示例代码
2020/08/12 Python
如何用H5实现一个触屏版的轮播器的实例
2017/01/09 HTML / CSS
加拿大在线隐形眼镜和眼镜店:VisionPros
2019/10/06 全球购物
NYX Professional Makeup英国官网:美国平价专业彩妆品牌
2019/11/13 全球购物
台湾专柜女包:KINAZ
2019/12/26 全球购物
KEEN美国官网:美国人气户外休闲鞋品牌
2021/03/09 全球购物
大学毕业生管理学求职信
2014/09/01 职场文书
工资收入证明样本(5篇)
2014/09/16 职场文书
工作收入证明模板
2014/10/10 职场文书
2014学生会工作总结报告
2014/12/02 职场文书
小学生成绩单评语
2014/12/31 职场文书
2015年度校学生会工作总结报告
2015/05/23 职场文书
Python Django搭建文件下载服务器的实现
2021/05/10 Python
输入框跟随文字内容适配宽实现示例
2022/08/14 Javascript