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 图片验证码代码分享
Jul 04 Python
对于Python的Django框架使用的一些实用建议
Apr 03 Python
Python实现ssh批量登录并执行命令
Oct 25 Python
老生常谈python的私有公有属性(必看篇)
Jun 09 Python
Python下实现的RSA加密/解密及签名/验证功能示例
Jul 17 Python
Python实现基于多线程、多用户的FTP服务器与客户端功能完整实例
Aug 18 Python
对pandas中iloc,loc取数据差别及按条件取值的方法详解
Nov 06 Python
解决项目pycharm能运行,在终端却无法运行的问题
Jan 19 Python
详解用python生成随机数的几种方法
Aug 04 Python
解决django 向mysql中写入中文字符出错的问题
May 18 Python
Python中的全局变量如何理解
Jun 04 Python
python 实现数据库中数据添加、查询与更新的示例代码
Dec 07 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
PHP字符串处理的10个简单方法
2010/06/30 PHP
php is_writable判断文件是否可写实例代码
2016/10/13 PHP
PHP基于ORM方式操作MySQL数据库实例
2017/06/21 PHP
深入理解PHP中mt_rand()随机数的安全
2017/10/12 PHP
JavaScript延迟加载
2021/03/09 Javascript
js function定义函数使用心得
2010/04/15 Javascript
基于jquery库的tab新形式使用
2012/11/16 Javascript
javascript中[]和{}对象使用介绍
2013/03/20 Javascript
深入理解JavaScript系列(41):设计模式之模板方法详解
2015/03/04 Javascript
Javascript数据结构与算法之列表详解
2015/03/12 Javascript
js和jquery分别验证单选框、复选框、下拉框
2015/12/17 Javascript
JavaScript function函数种类详解
2016/02/22 Javascript
js实现浏览器倒计时跳转页面效果
2016/08/12 Javascript
谈谈JavaScript的New关键字
2016/08/26 Javascript
javascript构造函数以及原型对象的理解
2017/01/13 Javascript
JavaScript实现图片切换效果
2017/08/12 Javascript
JS处理数据四舍五入(tofixed与round的区别详解)
2017/10/26 Javascript
JavaScript实现异步图像上传功能
2018/07/12 Javascript
BootStrap模态框闪退问题实例代码详解
2018/12/10 Javascript
微信小程序之滑动页面隐藏和显示组件功能的实现代码
2020/06/19 Javascript
JavaScript中常用的3种弹出提示框(alert、confirm、prompt)
2020/11/10 Javascript
Python 的 with 语句详解
2014/06/13 Python
Python的装饰器使用详解
2017/06/26 Python
Python多叉树的构造及取出节点数据(treelib)的方法
2019/08/09 Python
python错误调试及单元文档测试过程解析
2019/12/19 Python
详解css position 5种不同的值的用法
2019/07/30 HTML / CSS
市场部专员岗位职责
2013/11/30 职场文书
珍珠奶茶店创业计划书
2014/01/11 职场文书
20年同学聚会感言
2014/02/03 职场文书
个人借款担保书
2014/04/02 职场文书
党性心得体会
2014/09/03 职场文书
2014年志愿者工作总结
2014/11/20 职场文书
飞越疯人院观后感
2015/06/09 职场文书
运动会3000米加油稿
2015/07/21 职场文书
Java基础之详解HashSet的使用方法
2021/06/30 Java/Android
vue postcss-px2rem 自适应布局
2022/05/15 Vue.js