MENU

科学计算库 Numpy

August 4, 2018 • Read: 4339 • python阅读设置

genfromtxt 函数展开目录

  • import numpy
  • tmp = numpy.genfromtxt("1.txt",delimiter=",",dtype=str)
  • print (type(tmp))
  • print (tmp)
  • print (help(numpy.genfromtxt))

genfromtxt 函数里串了三个参数,分别是 要打开的文档名称,分隔符,以什么类型存储

打印结果:
第一行输出的是 "tmp" 这个变量的类型,可以看到是个 ndarray 矩阵类型,然后下面输出的是矩阵的值,最后输出的是 genfromtxt 这个函数的帮助文档

array 函数展开目录

  • import numpy
  • vector = numpy.array([5,10,15,20])
  • matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
  • print (vector)#[ 5 10 15 20]
  • print (matrix)
  • #[[ 5 10 15]
  • # [20 25 30]
  • # [35 40 45]]

这个太简单不说了

shape 变量展开目录

  • import numpy
  • vector1 = numpy.array([[1],[2],[3]])
  • vector2 = numpy.array([1,2,3])
  • vector3 = numpy.array([[5,10,15],[20,25,30]])
  • print (vector1.shape)#(3, 1)
  • print (vector2.shape)#(3,)
  • print (vector3.shape)#(2,3)

shape 变量就是打印矩阵的维度

取矩阵数据的问题展开目录

格式:变量名 = 矩阵名 [x,y],需要注意矩阵下标从 0 开始

格式:变量名 = 矩阵名 [x,y$_1$:$y_2$];取矩阵第 x 行,第 $y_1$ 列到第 $y_2$ 列的数据,不包含 $y_2$,下标同样从 0 开始

格式:变量名 = 矩阵名 [:,y],取第 y 列上的值,同理,[x,:] 表示取第 x 行上的所有值

== 运算展开目录

  • import numpy
  • vector = numpy.array([5,10,15,20])
  • print (vector == 10) #[False True False False]
  • import numpy
  • vector = numpy.array([5,10,15,20])
  • equal_to_ten = (vector == 10)
  • print (vector[equal_to_ten])#[10]

&、| 运算展开目录

不知道为什么 python 里面不是两个 &,而是一个 &,反正只要记住在 python 里面逻辑与和逻辑或都是一个就行了 qwq....

  • import numpy
  • vector = numpy.array([5,10,15,20])
  • equal_to_ten_or_five = (vector == 5) | (vector == 10)
  • print (equal_to_ten_or_five)#[True True False False]

astype 函数展开目录

astype 函数是将矩阵中所有元素的类型变为参数指定的类型

  • import numpy
  • vector = numpy.array([5,10,15,20])
  • print (vector.dtype)#int32
  • vector = vector.astype(str)
  • print (vector.dtype)#<U11
  • print (vector)#['5' '10' '15' '20']

min 函数展开目录

  • import numpy
  • vector = numpy.array([[5,10,15],
  • [20,25,30],
  • [35,40,45]
  • ])
  • print (vector.min(axis = 1))#[5 20 35]
  • print (vector.min(axis = 0))#[5 10 15]
  • #print (help(numpy.array))

axis 表示按行或列进行计算,axis=1 表示按行计算,axis=0 表示按列计算

sum 函数展开目录

  • import numpy
  • vector = numpy.array([[5,10,15],
  • [20,25,30],
  • [35,40,45]
  • ])
  • print (vector.sum())#225
  • print (vector.sum(axis = 1))#[30 75 120]
  • print (vector.sum(axis = 0))#[60 75 90]

arange 和 reshape 函数展开目录

arange (x) 表示构造一个 0 到 x-1 的一维矩阵;reshape (x,y) 表示将矩阵改为 x 行 y 列

  • import numpy as np
  • vector = np.arange(15)
  • print (vector)#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
  • vector = vector.reshape(3,5)
  • print (vector)
  • #[[ 0 1 2 3 4]
  • # [ 5 6 7 8 9]
  • # [10 11 12 13 14]]

arange 还有一种用法 arange (start,end,c),表示的含义是构造一个起始值为 start,小于 end 的一个公差为 c 的等差序列

ndim 和 size 变量展开目录

  • import numpy as np
  • vector = np.arange(9)
  • vector = vector.reshape(3,3)
  • print (vector.ndim)#2
  • print (vector.size)#9

zeros 和 ones 函数展开目录

zeros 和 ones 分别构造全 0 和全 1 矩阵

  • import numpy as np
  • print (np.zeros((2,2),dtype = np.int32))
  • #[[0 0]
  • # [0 0]]
  • print (np.ones((2,3),dtype = np.int32))
  • #[[1 1 1]
  • # [1 1 1]]

random 函数展开目录

  • import numpy as np
  • print (np.random.random((2,3)))#产生小于1的随机数

两个 radom 含义不一样,最左边的 random 类似于 Java 中包,先进入 random 包,然后调用 random 函数

linspace 函数展开目录

  • import numpy as np
  • from numpy import pi
  • print (np.linspace(0,2*pi,100))

linspace (start,end,num),构造起始值为 0,终点值为 end,个数为 num 个的一维矩阵

矩阵运算展开目录

  • import numpy as np
  • A = np.array([[1,1],
  • [0,1]])
  • B = np.array([[2,0],
  • [3,4]])
  • print (A * B)#对应位置的元素相乘
  • print (A.dot(B))#矩阵乘法

ravel()展开目录

将 n 行 m 列的矩阵变成一行 m*n 列矩阵

  • import numpy as np
  • vector = np.array([[1,2,3],
  • [4,5,6],
  • [7,8,9],
  • [10,11,12]])
  • print (vector.ravel())
  • print (vector.T)#转置
  • print (vector.reshape(4,-1))

reshape 只要有一个参数确定,另一个参数就确定了,所以另一个参数如果你懒得算,直接写 - 1,python 会自动计算另一个维度是多少

hstack 和 vstack 函数展开目录

hstack 函数的作用是将两个矩阵横着拼接在一起;vstack 是将两个矩阵竖着拼接在一起

  • import numpy as np
  • A = np.array([[1,2],
  • [3,4]])
  • B = np.array([[5,6],
  • [7,8]])
  • print (np.hstack((A,B)))
  • #[[1 2 5 6]
  • # [3 4 7 8]]
  • print (np.vstack((A,B)))
  • #[[1 2]
  • # [3 4]
  • # [5 6]
  • # [7 8]]

hsplit 和 vsplit 函数展开目录

分割矩阵的函数

  • import numpy as np
  • A = np.array([[1,2,3,4,5,6,7,8,],[9,10,11,12,13,14,15,16]])
  • print (A)
  • print (np.hsplit(A,4))
  • print (np.hsplit(A,(3,4)))
  • print (np.vsplit(A,2))

矩阵复制展开目录

  • import numpy as np
  • A = np.arange(12)
  • B = A
  • B.shape = 3,4
  • print (A.shape)#(3, 4)
  • print (id(A))#1521835664848
  • print (id(B))#1521835664848

从这结果我们可以看出,python 赋值是传引用

  • import numpy as np
  • A = np.arange(12)
  • B = A.view()
  • B.shape = 3,4
  • B[0,0] = 9
  • print (A[0:1])#[9]
  • print (A.shape)#(12,)
  • print (id(A))#1521835665728
  • print (id(B))#1521835665648

view 相当于一个浅复制,虽然改变 B 的维度,A 不变,但是如果改变 A 矩阵中某位置上的值,B 里也会改变,反之也一样

  • import numpy as np
  • A = np.arange(12)
  • B = A.copy()
  • B.shape = 3,4
  • B[0,0] = 9
  • print (A[0:1])#[0]
  • print (A.shape)#(12,)
  • print (id(A))#1521835666208
  • print (id(B))#1521835665488

copy 和上面两种复制不一样,copy 完完全全是将两个矩阵分离开,任何操作都不会影响到对方

argmax 函数展开目录

argmax 寻找最大值的下标,argmax (axis=0) 表示按列寻找,argmax (axis=1) 表示按行寻找

  • import numpy as np
  • data = np.arange(12).reshape((3,4))
  • print (data.argmax(axis = 0))#[2 2 2 2]

tile 函数展开目录

tile (data,(n,m)),将 data 矩阵扩展 n 行 m 列个

  • import numpy as np
  • data = np.arange(10,40,10)
  • print (np.tile(data,(2,2)))

sort 函数展开目录

排序

  • import numpy as np
  • a = np.array([[4,3,5],[1,2,1]])
  • b = np.sort(a,axis = 1)
  • print (b)
  • #[[3 4 5]
  • # [1 1 2]]
Last Modified: May 12, 2021
Archives Tip
QR Code for this page
Tipping QR Code
Leave a Comment

已有 1 条评论
  1. 科学计算库 Numpy 基础 & 提升(理解+重要函数讲解) | 呱唧呱唧网

    [...] 科学计算库 Numpy - mathor (wmathor.com)[...]