全网最简单易懂且最综合的Python Numpy新手教程

Numpy是Python中涉及科学计算的核心代码库,使用频次颇高,尤其是在机器学习领域。但是对于初学者(我)而言,找不到一个简单且综合的教程一直使我痛心疾首。最近看到一个英文网页写的颇有大家风范,特意翻译下来与大家共赏,也做自己以后的参考之用,原网页在:http://cs231n.github.io/python-numpy-tutorial/#numpy 。如果你对MATLAB很熟悉,这个教程或许可以帮你快速上手:https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html 。

Arrays

numpy array就是一个元素集合,所有的元素都具有同样的类型(参考C/C++里的数组),元素的索引是一个tuple,tuple里的值为非负整数。array的维度数目就是该array的rank,array的形状(shape)用一个tuple来表示,tuple里的数字一一对应array各个维度中元素的数目。

我们可以用Python list来初始化一个array,用方括号来访问array中的元素:

import numpy as np

a = np.array([1, 2, 3])   # 创建一个1维array
print(type(a))            # Prints "<class 'numpy.ndarray'>"
print(a.shape)            # Prints "(3,)"
print(a[0], a[1], a[2])   # Prints "1 2 3"
a[0] = 5                  # 改变array中的一个元素
print(a)                  # Prints "[5, 2, 3]"

b = np.array([[1,2,3],[4,5,6]])    # Create a rank 2 array,即2维array
print(b.shape)                     # Prints "(2, 3)"
print(b[0, 0], b[0, 1], b[1, 0])   # Prints "1 2 4"

Numpy也提供了很多创建array的函数:

import numpy as np

a = np.zeros((2,2))   # Create an array of all zeros
print(a)              # Prints "[[ 0.  0.]
                    #          [ 0.  0.]]"

b = np.ones((1,2))    # Create an array of all ones
print(b)              # Prints "[[ 1.  1.]]"

c = np.full((2,2), 7)  # Create a constant array
print(c)               # Prints "[[ 7.  7.]
                    #          [ 7.  7.]]"

d = np.eye(2)         # Create a 2x2 identity matrix(单位矩阵)
print(d)              # Prints "[[ 1.  0.]
                    #          [ 0.  1.]]"

e = np.random.random((2,2))  # Create an array filled with random values
print(e)                     # Might print "[[ 0.91940167  0.08143941]
                            #               [ 0.68744134  0.87236687]]"

更多array的创建方法可以在这里找到:https://docs.scipy.org/doc/numpy/user/basics.creation.html#arrays-creation 。

Array索引

Numpy提供了很多索引array的方法。

Slicing(切片)

与Python list的切片方法类似。因为array是多维数组,所以你在索引的时候必须给每个维度都切一次片:

import numpy as np

# Create the following rank 2 array with shape (3, 4)
# [[ 1  2  3  4]
#  [ 5  6  7  8]
#  [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

# 使用切片来获取包含前两行以及中间两列的子数组 b of shape (2, 2):
# [[2 3]
#  [6 7]]
b = a[:2, 1:3]

# A slice of an array is a view into the same data, 修改切片子数组也会同时修改原array
print(a[0, 1])   # Prints "2"
b[0, 0] = 77     # b[0, 0] is the same piece of data as a[0, 1]
print(a[0, 1])   # Prints "77"

你也可以混合使用整数索引和切片索引。需要注意的是,这种索引方式会降低输出array的维度(rank):

import numpy as np

# Create the following rank 2 array with shape (3, 4)
# [[ 1  2  3  4]
#  [ 5  6  7  8]
#  [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

# 提取上述array的中间行有两种方式:
# Mixing integer indexing with slices yields an array of lower rank,
# while using only slices yields an array of the same rank as the
# original array:
row_r1 = a[1, :]    # Rank 1 view of the second row of a
row_r2 = a[1:2, :]  # Rank 2 view of the second row of a
print(row_r1, row_r1.shape)  # Prints "[5 6 7 8] (4,)"
print(row_r2, row_r2.shape)  # Prints "[[5 6 7 8]] (1, 4)"

# 提取中间列的效果类似:
col_r1 = a[:, 1]
col_r2 = a[:, 1:2]
print(col_r1, col_r1.shape)  # Prints "[ 2  6 10] (3,)"
print(col_r2, col_r2.shape)  # Prints "[[ 2]
                            #          [ 6]
                            #          [10]] (3, 1)"
整数数组索引

当你使用切片来索引array时,输出的array是原array的一个子数组。与此相反,整数数组(Integer array)索引可以让你使用一个array的数据来完全重构另一个array。下面是几个例子:

import numpy as np

a = np.array([[1,2], [3, 4], [5, 6]])

# 整数数组索引的一个例子
# The returned array will have shape (3,) and
print(a[[0, 1, 2], [0, 1, 0]])  # Prints "[1 4 5]"

# 上述整数数组索引的效果与以下代码相同
print(np.array([a[0, 0], a[1, 1], a[2, 0]]))  # Prints "[1 4 5]"

# When using integer array indexing, you can reuse the same
# element from the source array:
print(a[[0, 0], [1, 1]])  # Prints "[2 2]"

# Equivalent to the previous integer array indexing example
print(np.array([a[0, 1], a[0, 1]]))  # Prints "[2 2]"

整数数组索引能做的一个有趣的事情,是在一个矩阵的每一行中选取或修改一个元素:

import numpy as np

# 创建一个新array
a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])

print(a)  # prints "array([[ 1,  2,  3],
        #                [ 4,  5,  6],
        #                [ 7,  8,  9],
        #                [10, 11, 12]])"

# Create an array of indices
b = np.array([0, 2, 0, 1])

# 使用b中的每个元素作为索引,在a的每一行选取一个数据
print(a[np.arange(4), b])  # Prints "[ 1  6  7 11]"

# Mutate one element from each row of a using the indices in b
a[np.arange(4), b] += 10

print(a)  # prints "array([[11,  2,  3],
        #                [ 4,  5, 16],
        #                [17,  8,  9],
        #                [10, 21, 12]])
Boolean数组索引

Boolean数组索引可以让你从一个array中选取任意个元素。这种索引方式经常被用来选取array里满足某些条件的元素:

import numpy as np

a = np.array([[1,2], [3, 4], [5, 6]])

bool_idx = (a > 2)   # Find the elements of a that are bigger than 2;
                    # this returns a numpy array of Booleans of the same
                    # shape as a, where each slot of bool_idx tells
                    # whether that element of a is > 2.

print(bool_idx)      # Prints "[[False False]
                    #          [ True  True]
                    #          [ True  True]]"

# We use boolean array indexing to construct a rank 1 array
# consisting of the elements of a corresponding to the True values
# of bool_idx
print(a[bool_idx])  # Prints "[3 4 5 6]"

# 上述操作可以在一句代码里完成:
print(a[a > 2])     # Prints "[3 4 5 6]"

本部分省略了array索引的很多细节,感兴趣的同学可以阅读一下官方教程:https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html 。

数据类型(Datatypes)

前面说过,array中所有元素都有同样的数据类型。Numpy提供了很多数据类型,当你构建一个array时,Numpy会为你定义的元素选择最匹配的类型。当然你也可以在构造array的函数中手动指定数据类型:

import numpy as np

x = np.array([1, 2])   # Let numpy choose the datatype
print(x.dtype)         # Prints "int64"

x = np.array([1.0, 2.0])   # Let numpy choose the datatype
print(x.dtype)             # Prints "float64"

x = np.array([1, 2], dtype=np.int64)   # Force a particular datatype
print(x.dtype)                         # Prints "int64"

最全面的数据类型永远在官方文档中:https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html 。

Array运算

Array中,基础数学运算都是元素级(elementwise )的操作,你可以使用运算符,也可以使用函数来进行这些运算:

import numpy as np

x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)

# Elementwise sum; both produce the array
# [[ 6.0  8.0]
#  [10.0 12.0]]
print(x + y)
print(np.add(x, y))

# Elementwise difference; both produce the array
# [[-4.0 -4.0]
#  [-4.0 -4.0]]
print(x - y)
print(np.subtract(x, y))

# Elementwise product; both produce the array
# [[ 5.0 12.0]
#  [21.0 32.0]]
print(x * y)
print(np.multiply(x, y))

# Elementwise division; both produce the array
# [[ 0.2         0.33333333]
#  [ 0.42857143  0.5       ]]
print(x / y)
print(np.divide(x, y))

# Elementwise square root; produces the array
# [[ 1.          1.41421356]
#  [ 1.73205081  2.        ]]
print(np.sqrt(x))

注意,Numpy的*运算符是元素级(elementwise)操作。vector的内积运算,vector与矩阵乘法,以及矩阵乘法都使用dot函数来做。dot函数可以是Numpy的模块内定函数,也可以是array对象的私有函数,两者效果相同:

import numpy as np

x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])

v = np.array([9,10])
w = np.array([11, 12])

# Inner product of vectors; both produce 219
print(v.dot(w))
print(np.dot(v, w))

# Matrix / vector product; both produce the rank 1 array [29 67]
print(x.dot(v))
print(np.dot(x, v))

# Matrix / matrix product; both produce the rank 2 array
# [[19 22]
#  [43 50]]
print(x.dot(y))
print(np.dot(x, y))

sum运算非常有用:

import numpy as np

x = np.array([[1,2],[3,4]])

print(np.sum(x))  # Compute sum of all elements; prints "10"
print(np.sum(x, axis=0))  # Compute sum of each column; prints "[4 6]"
print(np.sum(x, axis=1))  # Compute sum of each row; prints "[3 7]"

你可以在这个页面中找到所有的数学运算函数:https://docs.scipy.org/doc/numpy/reference/routines.math.html 。

除了对array进行数学运算之外,你可能还需要改变array的形状(reshape),或者操作array中的元素。例如,求矩阵的转置,可以使用array对象的T属性:

import numpy as np

x = np.array([[1,2], [3,4]])
print(x)    # Prints "[[1 2]
            #          [3 4]]"
print(x.T)  # Prints "[[1 3]
            #          [2 4]]"

# Note that taking the transpose of a rank 1 array does nothing:
v = np.array([1,2,3])
print(v)    # Prints "[1 2 3]"
print(v.T)  # Prints "[1 2 3]"

Numpy提供的所有操纵array的函数,都可以在这里找到:https://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html 。

广播(broadcasting)

广播是Numpy提供的一个非常重要的运算机制,它可以让你在两个形状不同的array之间做数学运算。例如我们有一个vector和一个矩阵,我们想把这个vector加到矩阵的每一行上:

import numpy as np

# We will add the vector v to each row of the matrix x,
# storing the result in the matrix y
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = np.empty_like(x)   # Create an empty matrix with the same shape as x

# Add the vector v to each row of the matrix x with an explicit loop
for i in range(4):
    y[i, :] = x[i, :] + v

# Now y is the following
# [[ 2  2  4]
#  [ 5  5  7]
#  [ 8  8 10]
#  [11 11 13]]
print(y)

上面的代码可行,但是如果矩阵x非常大,那么在Python中的循环会非常费时,尤其是在进行机器学习训练时。我们可以换种思路:垂直堆叠多个vector v,以获取一个与矩阵x相同形状的新矩阵vv,然后把xvv相加即可:

import numpy as np

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

Numpy的广播机制可以极大地缩减上述代码:

import numpy as np

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

因为广播机制,即使x的形状是(4, 3)v的形状是(3,)y = x + v仍然可以正常运算。广播机制的运算效果就是把v堆叠成与x形状相同的矩阵,然后再进行元素级数学运算。

广播机制遵循如下规则:

  1. 如果两个array的rank(维度)不同,堆叠低维array,直至两个array形状相同。
  2. 如果两个array在某个维度上的数据量(size)相同,或者其中一个array在某个维度上只有1个元素,那么这两个array是兼容的(compatible)。
  3. 如果两个array在所有维度上都是兼容的,那么它们会被同时广播(即会被同时堆叠出更大的矩阵)。
  4. 如果在任一维度上,一个array只有1个元素,另一个array的元素数目大于1,则在这个维度上堆叠第一个array。

关于广播的讲解如果您还不满意,那么请移步官方文档:https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html 。

支持广播的函数叫做universal functions。所有的universal functions都可以在这个页面找到:https://docs.scipy.org/doc/numpy/reference/ufuncs.html#available-ufuncs 。

以下是广播的一些应用:

import numpy as np

# Compute outer product of vectors
v = np.array([1,2,3])  # v has shape (3,)
w = np.array([4,5])    # w has shape (2,)
# To compute an outer product, we first reshape v to be a column
# vector of shape (3, 1); we can then broadcast it against w to yield
# an output of shape (3, 2), which is the outer product of v and w:
# [[ 4  5]
#  [ 8 10]
#  [12 15]]
print(np.reshape(v, (3, 1)) * w)

# 矩阵每一行都与一个vector相加
x = np.array([[1,2,3], [4,5,6]])
# x has shape (2, 3) and v has shape (3,) so they broadcast to (2, 3),
# giving the following matrix:
# [[2 4 6]
#  [5 7 9]]
print(x + v)

# 矩阵的每一列都与一个vector相加
# x has shape (2, 3) and w has shape (2,).
# If we transpose x then it has shape (3, 2) and can be broadcast
# against w to yield a result of shape (3, 2); transposing this result
# yields the final result of shape (2, 3) which is the matrix x with
# the vector w added to each column. Gives the following matrix:
# [[ 5  6  7]
#  [ 9 10 11]]
print((x.T + w).T)
# Another solution is to reshape w to be a column vector of shape (2, 1);
# we can then broadcast it directly against x to produce the same
# output.
print(x + np.reshape(w, (2, 1)))

# Multiply a matrix by a constant:
# x has shape (2, 3). Numpy treats scalars as arrays of shape ();
# these can be broadcast together to shape (2, 3), producing the
# following array:
# [[ 2  4  6]
#  [ 8 10 12]]
print(x * 2)

广播可以让你的代码更简洁高效,能用就用才是王道。

Numpy Documentation

本文涉及到很多重要的知识点,但是忽略掉了很多细节。请查看官方文档,以了解更多用法:https://docs.scipy.org/doc/numpy/reference/ 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值