欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python中的Numpy矩阵操作

程序员文章站 2023-10-27 18:32:58
numpy 通过观察python的自有数据类型,我们可以发现python原生并不提供多维数组的操作,那么为了处理矩阵,就需要使用第三方提供的相关的包。 nump...

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。