当前位置:   article > 正文

python的numpy的用法总结_numpy在python中的用法

numpy在python中的用法

本文总结Numpy的用法,建议先学习python的container 基础。numpy可以理解列表或数组。

一个numpy数组是一个由不同数值组成的网格。网格中的数据都是同一种数据类型,可以通过非负整型数的元组来访问。维度的数量被称为数组的阶,数组的大小是一个由整型数构成的元组,可以描述数组不同维度上的大小。

1、创建一维数组

  1. import numpy as np
  2. a = np.array([1, 2, 3]) # Create a rank 1 array
  3. print type(a) # Prints "<type 'numpy.ndarray'>"
  4. print a.shape # Prints "(3,)"
  5. print a[0], a[1], a[2] # Prints "1 2 3"
  6. a[0] = 5 # Change an element of the array
  7. print a # Prints "[5, 2, 3]"

2、创建二维数组

  1. b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array
  2. print b # 显示一下矩阵b
  3. print b.shape # Prints "(2, 3)"
  4. print b[0, 0], b[0, 1], b[1, 0] # Prints "1 2 4"

3、Numpy还提供了很多其他创建数组的方法

  1. import numpy as np
  2. #创建2行2列,值均为0的数组
  3. a = np.zeros((2,2)) # Create an array of all zeros
  4. print a # Prints "[[ 0. 0.]
  5. # [ 0. 0.]]"
  6. #创建1行2列,值均为1的数组
  7. b = np.ones((1,2)) # Create an array of all ones
  8. print b # Prints "[[ 1. 1.]]"
  9. #创建2行2列,值均为7的数组
  10. c = np.full((2,2), 7) # Create a constant array
  11. print c # Prints "[[ 7. 7.]
  12. # [ 7. 7.]]"
  13. #创建2行2列,斜线为1的数组
  14. d = np.eye(2) # Create a 2x2 identity matrix
  15. print d # Prints "[[ 1. 0.]
  16. # [ 0. 1.]]"
  17. #创建2行2列的随机数组
  18. e = np.random.random((2,2)) # Create an array filled with random values
  19. print e # Might print "[[ 0.91940167 0.08143941]

4、数组切片

Python列表类似,numpy数组可以使用切片语法。因为数组可以是多维的,所以你必须为每个维度指定好切片。

  1. import numpy as np
  2. # 创建2维表,形状 3行4列
  3. # [[ 1 2 3 4]
  4. # [ 5 6 7 8]
  5. # [ 9 10 11 12]]
  6. a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
  7. # 使用切片得到2行2列的子数组b
  8. # :2指取0至1行,1:3指取1至2列
  9. # [[2 3]
  10. # [6 7]]
  11. b = a[:2, 1:3]
  12. # 0指取0行,1指取1列
  13. print a[0, 1] # Prints "2"
  14. b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1]
  15. #当子数组更新,父数组也相应更新
  16. print a[0, 1] # Prints "77"

5、整型和切片语法

你可以同时使用整型和切片语法来访问数组。但是,这样做会产生一个比原数组低阶的新数组。需要注意的是,这里和MATLAB中的情况是不同的

  1. # [[ 1 2 3 4]
  2. # [ 5 6 7 8]
  3. # [ 9 10 11 12]]
  4. a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
  5. # Two ways of accessing the data in the middle row of the array.
  6. # Mixing integer indexing with slices yields an array of lower rank,
  7. # while using only slices yields an array of the same rank as the
  8. # original array:
  9. row_r1 = a[1, :] # Rank 1 view of the second row of a
  10. row_r2 = a[1:2, :] # Rank 2 view of the second row of a
  11. print row_r1, row_r1.shape # Prints "[5 6 7 8] (4,)"
  12. print row_r2, row_r2.shape # Prints "[[5 6 7 8]] (1, 4)"
  13. # We can make the same distinction when accessing columns of an array:
  14. col_r1 = a[:, 1]
  15. col_r2 = a[:, 1:2]
  16. print col_r1, col_r1.shape # Prints "[ 2 6 10] (3,)"
  17. print col_r2, col_r2.shape # Prints "[[ 2]
  18. # [ 6]
  19. # [10]] (3, 1)"

6、整型数组访问

当我们使用切片语法访问数组时,得到的总是原数组的一个子集。整型数组访问允许我们利用其它数组的数据构建一个新的数组:

  1. import numpy as np
  2. a = np.array([[1,2], [3, 4], [5, 6]])
  3. # An example of integer array indexing.
  4. # The returned array will have shape (3,) and
  5. # [0, 1, 2]指第0,1,2行,[0, 1, 0]指第0,1,0列
  6. print a[[0, 1, 2], [0, 1, 0]] # Prints "[1 4 5]"
  7. # The above example of integer array indexing is equivalent to this:
  8. print np.array([a[0, 0], a[1, 1], a[2, 0]]) # Prints "[1 4 5]"
  9. # When using integer array indexing, you can reuse the same
  10. # element from the source array:
  11. a[[0, 0], [1, 1]] 相当于 a[0, 1]
  12. print a[[0, 0], [1, 1]] # Prints "[2 2]"
  13. # Equivalent to the previous integer array indexing example
  14. print np.array([a[0, 1], a[0, 1]]) # Prints "[2 2]"

7、选择或者更改元素

整型数组访问语法还有个有用的技巧,可以用来选择或者更改矩阵中每行中的一个元素:

  1. import numpy as np
  2. # Create a new array from which we will select elements
  3. a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
  4. print a # prints "array([[ 1, 2, 3],
  5. # [ 4, 5, 6],
  6. # [ 7, 8, 9],
  7. # [10, 11, 12]])"
  8. # Create an array of indices
  9. b = np.array([0, 2, 0, 1])
  10. # Select one element from each row of a using the indices in b
  11. #np.arange(4)指按顺序从0至3数,相当于数组[0,1,2,3],即是指定了第0-3行
  12. #b [0, 2, 0, 1] 是取a数组的第0,2,0,1列
  13. print a[np.arange(4), b] # Prints "[ 1 6 7 11]"
  14. # Mutate one element from each row of a using the indices in b
  15. a[np.arange(4), b] += 10
  16. #以上指定的元素,均加10
  17. print a # prints "array([[11, 2, 3],
  18. # [ 4, 5, 16],
  19. # [17, 8, 9],
  20. # [10, 21, 12]])

8、布尔型数组访问

布尔型数组访问可以让你选择数组中任意元素。通常,这种访问方式用于选取数组中满足某些条件的元素,举例如下:

  1. import numpy as np
  2. a = np.array([[1,2], [3, 4], [5, 6]])
  3. bool_idx = (a > 2) # Find the elements of a that are bigger than 2;
  4. # this returns a numpy array of Booleans of the same
  5. # shape as a, where each slot of bool_idx tells
  6. # whether that element of a is > 2.
  7. print bool_idx # Prints "[[False False]
  8. # [ True True]
  9. # [ True True]]"
  10. # We use boolean array indexing to construct a rank 1 array
  11. # consisting of the elements of a corresponding to the True values
  12. # of bool_idx
  13. print a[bool_idx] # Prints "[3 4 5 6]"
  14. # We can do all of the above in a single concise statement:
  15. print a[a > 2] # Prints "[3 4 5 6]"

有很多数组访问的细节我们没有详细说明,可以查看文档

9、数据类型

每个Numpy数组都是数据类型相同的元素组成的网格。Numpy提供了很多的数据类型用于创建数组。当你创建数组的时候,Numpy会尝试猜测数组的数据类型,你也可以通过参数直接指定数据类型,例子如下:

  1. import numpy as np
  2. x = np.array([1, 2]) # Let numpy choose the datatype
  3. print x.dtype # Prints "int64"
  4. x = np.array([1.0, 2.0]) # Let numpy choose the datatype
  5. print x.dtype # Prints "float64"
  6. x = np.array([1, 2], dtype=np.int64) # Force a particular datatype
  7. print x.dtype # Prints "int64"

更多细节查看文档

10、数组计算

基本数学计算函数会对数组中元素逐个进行计算,既可以利用操作符重载,也可以使用函数方式:

  1. import numpy as np
  2. x = np.array([[1,2],[3,4]], dtype=np.float64)
  3. y = np.array([[5,6],[7,8]], dtype=np.float64)
  4. # Elementwise sum; both produce the array
  5. # [[ 6.0 8.0]
  6. # [10.0 12.0]]
  7. print x + y
  8. print np.add(x, y)
  9. # Elementwise difference; both produce the array
  10. # [[-4.0 -4.0]
  11. # [-4.0 -4.0]]
  12. print x - y
  13. print np.subtract(x, y)
  14. # Elementwise product; both produce the array
  15. # [[ 5.0 12.0]
  16. # [21.0 32.0]]
  17. print x * y
  18. print np.multiply(x, y)
  19. # Elementwise division; both produce the array
  20. # [[ 0.2 0.33333333]
  21. # [ 0.42857143 0.5 ]]
  22. print x / y
  23. print np.divide(x, y)
  24. # Elementwise square root; produces the array
  25. # [[ 1. 1.41421356]
  26. # [ 1.73205081 2. ]]
  27. print np.sqrt(x)

11、dot矩阵乘法

和MATLAB不同,*是元素逐个相乘,而不是矩阵乘法。在Numpy中使用dot来进行矩阵乘法,计算矩阵乘法,请将第一个矩阵行元素(或数字)乘以第二个矩阵列元素,然后计算其总和:

  1. import numpy as np
  2. x = np.array([[1,2],[3,4]])
  3. y = np.array([[5,6],[7,8]])
  4. v = np.array([9,10])
  5. w = np.array([11, 12])
  6. # Inner product of vectors; both produce 219
  7. print v.dot(w)
  8. print np.dot(v, w)
  9. # Matrix / vector product; both produce the rank 1 array [29 67]
  10. print x.dot(v)
  11. print np.dot(x, v)
  12. # Matrix / matrix product; both produce the rank 2 array
  13. # [[19 22]
  14. # [43 50]]
  15. #计算过程:1*5+2*7=19,1*6+2*8=22,其他依此类计算
  16. print x.dot(y)
  17. print np.dot(x, y)

12、sum函数

Numpy提供了很多计算数组的函数,其中最常用的一个是sum

  1. import numpy as np
  2. x = np.array([[1,2],[3,4]])
  3. print np.sum(x) # Compute sum of all elements; prints "10"
  4. #axis=0 列相加;axis=1 行相加。
  5. print np.sum(x, axis=0) # Compute sum of each column; prints "[4 6]"
  6. print np.sum(x, axis=1) # Compute sum of each row; prints "[3 7]"

想要了解更多函数,可以查看文档

13、T转置矩阵

除了计算,我们还常常改变数组或者操作其中的元素。其中将矩阵转置是常用的一个,在Numpy中,使用T来转置矩阵:

  1. import numpy as np
  2. x = np.array([[1,2], [3,4]])
  3. print x # Prints "[[1 2]
  4. # [3 4]]"
  5. print x.T # Prints "[[1 3]
  6. # [2 4]]"
  7. # Note that taking the transpose of a rank 1 array does nothing:
  8. v = np.array([1,2,3])
  9. print v # Prints "[1 2 3]"
  10. print v.T # Prints "[1 2 3]"

Numpy还提供了更多操作数组的方法,请查看文档

14、广播Broadcasting

广播是一种强有力的机制,它让Numpy可以让不同大小的矩阵在一起进行数学计算。我们常常会有一个小的矩阵和一个大的矩阵,然后我们会需要用小的矩阵对大的矩阵做一些计算。

举个例子,如果我们想要把一个向量加到矩阵的每一行,我们可以这样做:

  1. import numpy as np
  2. # We will add the vector v to each row of the matrix x,
  3. # storing the result in the matrix y
  4. x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
  5. v = np.array([1, 0, 1])
  6. y = np.empty_like(x) # Create an empty matrix with the same shape as x
  7. # Add the vector v to each row of the matrix x with an explicit loop
  8. for i in range(4):
  9. y[i, :] = x[i, :] + v
  10. # Now y is the following
  11. # [[ 2 2 4]
  12. # [ 5 5 7]
  13. # [ 8 8 10]
  14. # [11 11 13]]
  15. print y

这样是行得通的,但是当x矩阵非常大,利用循环来计算就会变得很慢很慢。我们可以换一种思路:

  1. import numpy as np
  2. # We will add the vector v to each row of the matrix x,
  3. # storing the result in the matrix y
  4. x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
  5. v = np.array([1, 0, 1])
  6. vv = np.tile(v, (4, 1)) # Stack 4 copies of v on top of each other
  7. print vv # Prints "[[1 0 1]
  8. # [1 0 1]
  9. # [1 0 1]
  10. # [1 0 1]]"
  11. y = x + vv # Add x and vv elementwise
  12. print y # Prints "[[ 2 2 4
  13. # [ 5 5 7]
  14. # [ 8 8 10]
  15. # [11 11 13]]"

Numpy广播机制可以让我们不用创建vv,就能直接运算,看看下面例子:

  1. import numpy as np
  2. # We will add the vector v to each row of the matrix x,
  3. # storing the result in the matrix y
  4. x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
  5. v = np.array([1, 0, 1])
  6. y = x + v # Add v to each row of x using broadcasting
  7. print y # Prints "[[ 2 2 4]
  8. # [ 5 5 7]
  9. # [ 8 8 10]
  10. # [11 11 13]]"

对两个数组使用广播机制要遵守下列规则:

  1. 如果数组的秩不同,使用1来将秩较小的数组进行扩展,直到两个数组的尺寸的长度都一样。

  1. 如果两个数组在某个维度上的长度是一样的,或者其中一个数组在该维度上长度为1,那么我们就说这两个数组在该维度上是相容的。

  1. 如果两个数组在所有维度上都是相容的,他们就能使用广播。

  1. 如果两个输入数组的尺寸不同,那么注意其中较大的那个尺寸。因为广播之后,两个数组的尺寸将和那个较大的尺寸一样。

  1. 在任何一个维度上,如果一个数组的长度为1,另一个数组长度大于1,那么在该维度上,就好像是对第一个数组进行了复制。

如果上述解释看不明白,可以读一读文档和这个解释。译者注:强烈推荐阅读文档中的例子。

支持广播机制的函数是全局函数。哪些是全局函数可以在文档中查找。

  1. 下面是一些广播机制的使用:
  2. import numpy as np
  3. # Compute outer product of vectors
  4. v = np.array([1,2,3]) # v has shape (3,)
  5. w = np.array([4,5]) # w has shape (2,)
  6. # To compute an outer product, we first reshape v to be a column
  7. # vector of shape (3, 1); we can then broadcast it against w to yield
  8. # an output of shape (3, 2), which is the outer product of v and w:
  9. # [[ 4 5]
  10. # [ 8 10]
  11. # [12 15]]
  12. print np.reshape(v, (3, 1)) * w
  13. # Add a vector to each row of a matrix
  14. x = np.array([[1,2,3], [4,5,6]])
  15. # x has shape (2, 3) and v has shape (3,) so they broadcast to (2, 3),
  16. # giving the following matrix:
  17. # [[2 4 6]
  18. # [5 7 9]]
  19. print x + v
  20. # Add a vector to each column of a matrix
  21. # x has shape (2, 3) and w has shape (2,).
  22. # If we transpose x then it has shape (3, 2) and can be broadcast
  23. # against w to yield a result of shape (3, 2); transposing this result
  24. # yields the final result of shape (2, 3) which is the matrix x with
  25. # the vector w added to each column. Gives the following matrix:
  26. # [[ 5 6 7]
  27. # [ 9 10 11]]
  28. print (x.T + w).T
  29. # Another solution is to reshape w to be a row vector of shape (2, 1);
  30. # we can then broadcast it directly against x to produce the same
  31. # output.
  32. print x + np.reshape(w, (2, 1))
  33. # Multiply a matrix by a constant:
  34. # x has shape (2, 3). Numpy treats scalars as arrays of shape ();
  35. # these can be broadcast together to shape (2, 3), producing the
  36. # following array:
  37. # [[ 2 4 6]
  38. # [ 8 10 12]]
  39. print x * 2

广播机制能够让你的代码更简洁更迅速,能够用的时候请尽量使用!

15、SciPy

Numpy提供了高性能的多维数组,以及计算和操作数组的基本工具。SciPy基于Numpy,提供了大量的计算和操作数组的函数,这些函数对于不同类型的科学和工程计算非常有用。

熟悉SciPy的最好方法就是阅读文档。我们会强调对于本课程有用的部分。

    • 图像操作

SciPy提供了一些操作图像的基本函数。比如,它提供了将图像从硬盘读入到数组的函数,也提供了将数组中数据写入的硬盘成为图像的函数。下面是一个简单的例子:

  1. from scipy.misc import imread, imsave, imresize
  2. # Read an JPEG image into a numpy array
  3. img = imread('assets/cat.jpg')
  4. print img.dtype, img.shape # Prints "uint8 (400, 248, 3)"
  5. # We can tint the image by scaling each of the color channels
  6. # by a different scalar constant. The image has shape (400, 248, 3);
  7. # we multiply it by the array [1, 0.95, 0.9] of shape (3,);
  8. # numpy broadcasting means that this leaves the red channel unchanged,
  9. # and multiplies the green and blue channels by 0.95 and 0.9
  10. # respectively.
  11. img_tinted = img * [1, 0.95, 0.9]
  12. # Resize the tinted image to be 300 by 300 pixels.
  13. img_tinted = imresize(img_tinted, (300, 300))
  14. # Write the tinted image back to disk
  15. imsave('assets/cat_tinted.jpg', img_tinted)
2、点之间的距离

SciPy定义了一些有用的函数,可以计算集合中点之间的距离。

函数scipy.spatial.distance.pdist能够计算集合中所有两点之间的距离:

  1. import numpy as np
  2. from scipy.spatial.distance import pdist, squareform
  3. # Create the following array where each row is a point in 2D space:
  4. # [[0 1]
  5. # [1 0]
  6. # [2 0]]
  7. x = np.array([[0, 1], [1, 0], [2, 0]])
  8. print x
  9. # Compute the Euclidean distance between all rows of x.
  10. # d[i, j] is the Euclidean distance between x[i, :] and x[j, :],
  11. # and d is the following array:
  12. # [[ 0. 1.41421356 2.23606798]
  13. # [ 1.41421356 0. 1. ]
  14. # [ 2.23606798 1. 0. ]]
  15. d = squareform(pdist(x, 'euclidean'))
  16. print d

具体细节请阅读文档

函数scipy.spatial.distance.cdist可以计算不同集合中点的距离,具体请查看文档

16、Matplotlib

Matplotlib是一个作图库。这里简要介绍matplotlib.pyplot模块,功能和MATLAB的作图功能类似。

绘图。matplotlib库中最重要的函数是Plot。该函数允许你做出2D图形,如下:

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # Compute the x and y coordinates for points on a sine curve
  4. x = np.arange(0, 3 * np.pi, 0.1)
  5. y = np.sin(x)
  6. # Plot the points using matplotlib
  7. plt.plot(x, y)
  8. plt.show() # You must call plt.show() to make graphics appear.

只需要少量工作,就可以一次画不同的线,加上标签,坐标轴标志等。

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # Compute the x and y coordinates for points on sine and cosine curves
  4. x = np.arange(0, 3 * np.pi, 0.1)
  5. y_sin = np.sin(x)
  6. y_cos = np.cos(x)
  7. # Plot the points using matplotlib
  8. plt.plot(x, y_sin)
  9. plt.plot(x, y_cos)
  10. plt.xlabel('x axis label')
  11. plt.ylabel('y axis label')
  12. plt.title('Sine and Cosine')
  13. plt.legend(['Sine', 'Cosine'])
  14. plt.show()

绘制多个图像,可以使用subplot函数来在一幅图中画不同的东西:

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # Compute the x and y coordinates for points on sine and cosine curves
  4. x = np.arange(0, 3 * np.pi, 0.1)
  5. y_sin = np.sin(x)
  6. y_cos = np.cos(x)
  7. # Set up a subplot grid that has height 2 and width 1,
  8. # and set the first such subplot as active.
  9. plt.subplot(2, 1, 1)
  10. # Make the first plot
  11. plt.plot(x, y_sin)
  12. plt.title('Sine')
  13. # Set the second subplot as active, and make the second plot.
  14. plt.subplot(2, 1, 2)
  15. plt.plot(x, y_cos)
  16. plt.title('Cosine')
  17. # Show the figure.
  18. plt.show()

你可以使用imshow函数来显示图像,如下所示:

  1. import numpy as np
  2. from scipy.misc import imread, imresize
  3. import matplotlib.pyplot as plt
  4. img = imread('assets/cat.jpg')
  5. img_tinted = img * [1, 0.95, 0.9]
  6. # Show the original image
  7. plt.subplot(1, 2, 1)
  8. plt.imshow(img)
  9. # Show the tinted image
  10. plt.subplot(1, 2, 2)
  11. # A slight gotcha with imshow is that it might give strange results
  12. # if presented with data that is not uint8. To work around this, we
  13. # explicitly cast the image to uint8 before displaying it.
  14. plt.imshow(np.uint8(img_tinted))
  15. plt.show()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/968934
推荐阅读
相关标签
  

闽ICP备14008679号