numpy入门教程(二)

基本参考于standford大学cs231n课程笔记cs231n-python numpy tutorial
略有删减与补充,欢迎交流指正

这一部分主要介绍numpy的常用操作方法

Numpy

Numpy是Python中用于科学计算的核心库。它提供了高性能的多维数组对象,以及相关工具。

Nmupy数组

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

我们可以从列表创建数组,然后利用方括号访问其中的元素:

1. Numpy数组的创建与索引

import numpy as np
a = np.array([1,2,3])
print type(a) #
print a.shape #元组描述其维度大小 (3L,) 3行1列
print a[0],a[1] #用方括号进行访问 1,2
a[0] = 5
print a[0] #5

b = np.array([[1,2,3],[4,5,6]]) #创建二维数组
print b
print b.shape #(2L, 3L) 2行3列
print b[0,0] #访问
  • 快速创建几种内置的数组样式
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]]

2. 多维数组的切片

# 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]])

# Use slicing to pull out the subarray consisting of the first 2 rows
# and columns 1 and 2; b is the following array of shape (2, 2):
# [[2 3]
#  [6 7]]

  • 注意!!:数组的切片指向的是原数组 所以改变了切片的值也会改变原数组的值
b = a[:2, 1:3]
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"
  • 两种切片方式:( 注意切片之后的维度对比)
# 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]])

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,)"  4行1列  一维数组
print row_r2, row_r2.shape  # Prints "[[5 6 7 8]] (1, 4)" 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)" 
  • 整型数组访问 注意对比 两者等效的访问方式
a = np.array([[1,2], [3, 4], [5, 6]])

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]"

print a[[0, 0], [1, 1]]  # Prints "[2 2]"
print np.array([a[0, 1], a[0, 1]])  # Prints "[2 2]"
  • 一种灵活的切片方式 去布尔值==True
a = np.array([[1,2], [3, 4], [5, 6]])
bool_idx = (a > 2) 
print bool_idx      # Prints "[[False False]
                    #          [ True  True]
                    #          [ True  True]]"

print a[bool_idx]  # Prints "[3 4 5 6]"
print a[a > 2]     # Prints "[3 4 5 6]"

3. 数组计算

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)
  • python中*是逐个相乘,矩阵乘法使用dot函数 如下
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) #两者结果一致
  • 求和函数sum的使用
x = np.array([[1,2],[3,4]])

print np.sum(x)  #  prints "10"
print np.sum(x, axis=0)  #  计算所有列之和 prints "[4 6]"
print np.sum(x, axis=1)  #  计算所有行之和 prints "[3 7]"
  • 矩阵转置
x = np.array([[1,2], [3,4]])
print x    # Prints "[[1 2]
           #          [3 4]]"
print x.T  # Prints "[[1 3]
           #          [2 4]]"

Numpy的广播机制

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

import numpy as np
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

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

但当矩阵过大时运行效率比较低,改进一下,不再使用循环,新建一个矩阵

# 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的广播机制可以让我们不用创建vv 就能直接运算
    规则如下
  1. 如果数组的秩不同,使用1来将秩较小的数组进行扩展,直到两个数组的尺寸的长度都一样。
  2. 如果两个数组在某个维度上的长度是一样的,或者其中一个数组在该维度上长度为1,那么我们就说这两个数组在该维度上是相容的。
  3. 如果两个数组在所有维度上都是相容的,他们就能使用广播。
  4. 如果两个输入数组的尺寸不同,那么注意其中较大的那个尺寸。因为广播之后,两个数组的尺寸将和那个较大的尺寸一样。
  5. 在任何一个维度上,如果一个数组的长度为1,另一个数组长度大于1,那么在该维度上,就好像是对第一个数组进行了复制。
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]]"
  • 广播机制例子
# 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 #规则1  w自动扩展为[4,5,1]


# Add a vector to each row of a matrix
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 #规则5 v扩展为[[1 2 3][1 2 3]]

print x * 2  
# [[ 2  4  6]
#  [ 8 10 12]]      

从官网文档摘一张实例图,方便理解,其实就是自动扩展数组使结果符合维度较大的数组,使得能够进行运算

>>> from numpy import array
>>> a = array([[ 0.0, 0.0, 0.0],
 [10.0,10.0,10.0],
[20.0,20.0,20.0],
[30.0,30.0,30.0]])
>>> b = array([1.0,2.0,3.0])
>>> a + barray([[ 1., 2., 3.], [ 11., 12., 13.], [ 21., 22., 23.], [ 31., 32., 33.]])
Paste_Image.png

你可能感兴趣的:(numpy入门教程(二))