侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python中的Numpy矩阵操作

Python  /  管理员 发布于 7年前   190

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,emptyz = 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, arangea = 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 += 3a# Out[17]:# array([[ 3, 4, 5],#    [ 6, 7, 8],#    [ 9, 10, 11]]) 
######################################## 矩阵内置操作#######################################from numpy import array as matrix, arangea = arange(9).reshape(3,3)a# Out[10]:# array([[0, 1, 2],#    [3, 4, 5],#    [6, 7, 8]])a.max()# Out[23]: 8a.min()# Out[24]: 0a.sum()# Out[25]: 36 
######################################## 矩阵索引、拆分、遍历#######################################from numpy import array as matrix, arangea = 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]: 13a[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, arangeb = 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,newaxisimport numpy as npd1 = 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之新式类
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客