Python中的Numpy矩阵操作

作者:峻之岭峰 时间:2021-10-06 07:19:23 

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官方文档

来源:https://www.cnblogs.com/sitemanager/p/9057195.html

标签:Python,Numpy,矩阵
0
投稿

猜你喜欢

  • oracle 多个字符替换实现

    2009-10-23 17:50:00
  • 如何建设一个多语言版的ASP网站?

    2009-11-26 20:36:00
  • Python趣味挑战之turtle库绘画飘落的银杏树

    2023-07-21 21:46:40
  • asp实现*号隐藏IP地址

    2008-08-10 18:51:00
  • 浮动元素对浏览器的支持

    2008-02-02 10:05:00
  • 详解MySQL数据库之更新语句

    2010-08-08 09:15:00
  • 对Python3使运行暂停的方法详解

    2021-02-05 22:55:21
  • PHP引用的调用方法分析

    2023-11-06 15:23:23
  • ASP中的Debug类--VBScript

    2008-10-24 09:38:00
  • 浅谈购物类网站如何保持视觉设计的一致性

    2009-03-30 16:02:00
  • Oracle 8i字符集乱码问题析及其解决办法

    2010-07-26 13:29:00
  • Python cookbook(数据结构与算法)对切片命名清除索引的方法

    2023-10-31 02:27:35
  • 嵌入式Web视频点播系统实现方法

    2007-10-10 21:17:00
  • Go语言框架快速集成限流中间件详解

    2023-08-26 11:44:39
  • Python将字符串常量转化为变量方法总结

    2023-07-14 00:36:58
  • 浅谈SQL Server中统计对于查询的影响分析

    2012-06-06 20:08:23
  • 微信小程序request请求后台接口php的实例详解

    2023-11-11 14:24:04
  • javascript根据像素点取位置示例

    2023-09-03 22:58:54
  • Python中字典(dict)和列表(list)的排序方法实例

    2021-11-30 11:15:18
  • [译]2009年海外Web设计风潮(下)

    2009-01-23 09:34:00
  • asp之家 网络编程 m.aspxhome.com