Python Numpy 和 SciPy-入门教程

本文简单介绍了Python的基本概念、Numpy和SciPy的简单用法,非常适合初学者以及已经入门需要复习的Python学习者。文章来源于CS231n课程给予初学者的Python的初级教程,斯坦福大学的CS231n( Convolutional Neural Networks for Visual Recogniton),开课者是著名计算机视觉学者李飞飞教授。


原文链接:http://cs231n.github.io/python-numpy-tutorial/

目录

  • Python
    • 基本数据类型
    • 容器(Containers)
      • 列表(Lists)
      • 字典(Dictionaries)
      • 集合(Sets)
      • 元组(Tuples)
    • 函数(Functions)
    • 类(Classes)
  • Numpy
    • 数组(Arrays)
    • 数组索引(Array indexing)
    • 数据类型
    • Array math
    • 广播(broadcasting)
  • SciPy
    • 图像处理(Image operations)
    • 点之间的距离
  • Matplotlib
    • 作图(Plotting)
    • 子图(Subplots)
    • 图像(Images)

Python

Python是一种动态、多参数的高级编程语言。在Python里,用几行简单易读的代码就能让你天马行空的想法实现,所以有人说Python读起来就像是伪代码不无道理。

例如,在Python实现经典快排算法只需要这么几行

1
2
3
4
5
6
7
8
9
10
11
def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) / 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)

print quicksort([3,6,8,10,1,2,1])
# Prints "[1, 1, 2, 3, 6, 8, 10]"

基本数据类型

与大多数编程语言类似,Python有许多数据类型,包括整数(integer)、浮点数(float)、布尔数(boolean)和字符串(string)。这些数据类型与其他编程语言的相比也没什么两样。

数字(Numbers):整数和浮点数的操作与其他编程语言的也没什么两样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
x = 3
print type(x) # Prints "<type 'int'>"打印数据的类型
print x # Prints "3"
print x + 1 # Addition; prints "4"
print x - 1 # Subtraction; prints "2"
print x * 2 # Multiplication; prints "6"
print x ** 2 # Exponentiation; prints "9" 指数运算
x += 1
print x # Prints "4"
x *= 2
print x # Prints "8"
y = 2.5
print type(y) # Prints "<type 'float'>"
print y, y + 1, y * 2, y ** 2 # Prints "2.5 3.5 5.0 6.25"

注意,与其他编程语言不同,Python没有自增(++)和自减(—)的运算。

Python也还有为长整型和复杂数据准备的自建类型,你可以在这个文档链接中找到相关细节信息。

布尔型(Booleans):Python可以实现所有的布尔逻辑运算,不同于其他编程语言,它的运算符号都是英语单词,比如andornot,而不是(&&,||

1
2
3
4
5
6
7
t = True
f = False
print type(t) # Prints "<type 'bool'>"
print t and f # Logical AND; prints "False"
print t or f # Logical OR; prints "True"
print not t # Logical NOT; prints "False"
print t != f # Logical XOR; prints "True"

字符串(Strings)

1
2
3
4
5
6
7
8
hello = 'hello'   # String literals can use single quotes(单引号或双引号都没关系)
world = "world" # or double quotes; it does not matter.
print hello # Prints "hello"
print len(hello) # String length; prints "5"(字符串的长度)
hw = hello + ' ' + world # 用空格连接字符串
print hw # prints "hello world"
hw12 = '%s %s %d' % (hello, world, 12) # 打印字符串的特定类型
print hw12 # prints "hello world 12"

Python中的字符串还有一大堆好玩的玩法,比如

1
2
3
4
5
6
7
8
s = "hello"
print s.capitalize() # Capitalize a string; prints "Hello"(首字母大写)
print s.upper() # Convert a string to uppercase; prints "HELLO"
print s.rjust(7) # Right-justify a string, padding with spaces; prints " hello"
print s.center(7) # Center a string, padding with spaces; prints " hello "
print s.replace('l', '(ell)') # Replace all instances of one substring with another;替换子字符串
# prints "he(ell)(ell)o"
print ' world '.strip() # Strip leading and trailing whitespace; prints "world"跳过字符串前面的空格

你可以在这个链接中找到一些关于字符串的操作细节。

容器(Containers)

Python有几个自建容器类型:列表、字典、集合和元组。

列表(Lists)

Python中的列表等同于一个数组,然而列表可以改变大小,还可以容纳不同类型的元素,在Python中列表用[]围起来

1
2
3
4
5
6
7
8
9
xs = [3, 1, 2]   # Create a list
print xs, xs[2] # Prints "[3, 1, 2] 2"
print xs[-1] # Negative indices count from the end of the list; prints "2" # 负数索引从列表的最后开始算起
xs[2] = 'foo' # Lists can contain elements of different types
print xs # Prints "[3, 1, 'foo']"列表中可以容纳不同类型的元素
xs.append('bar') # Add a new element to the end of the list
print xs # Prints "[3, 1, 'foo', 'bar']"
x = xs.pop() # Remove and return the last element of the list,去掉列表中的最后一个元素
print x, xs # Prints "bar [3, 1, 'foo']"

分片(Slicing):除了使用索引访问单个元素,Python还可以使用分片操作来访问某个范围内的元素

1
2
3
4
5
6
7
8
9
nums = range(5)    # range is a built-in function that creates a list of integers
print nums # Prints "[0, 1, 2, 3, 4]"
print nums[2:4] # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
print nums[2:] # Get a slice from index 2 to the end; prints "[2, 3, 4]"
print nums[:2] # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
print nums[:] # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]"
print nums[:-1] # Slice indices can be negative; prints ["0, 1, 2, 3]"
nums[2:4] = [8, 9] # Assign a new sublist to a slice
print nums # Prints "[0, 1, 8, 9, 4]"

循环(Loops):针对列表中的元素,你也可以进行循环操作

1
2
3
4
animals = ['cat', 'dog', 'monkey']
for animal in animals:
print animal
# Prints "cat", "dog", "monkey", each on its own line.

如果你想通过循环体来获得列表中元素的索引,还不如使用内建函数enumerate

1
2
3
4
animals = ['cat', 'dog', 'monkey']
for idx, animal in enumerate(animals):
print '#%d: %s' % (idx + 1, animal)
# Prints "#1: cat", "#2: dog", "#3: monkey", each on its own line

列表解析(List comprehensions):当使用Python编程的时候,经常我们需要将一类数据转换成另一种类型的数据。举一个简单的例子,看看下面这段计算平方的代码

1
2
3
4
5
nums = [0, 1, 2, 3, 4]
squares = []
for x in nums:
squares.append(x ** 2)
print squares # Prints [0, 1, 4, 9, 16]

(是不是很简单?对在Python下面就是这么通俗易懂,所以说“人生苦短,我用Python”是不无道理的。)
你也可以通过列表解析让代码变得更简单

1
2
3
nums = [0, 1, 2, 3, 4]
squares = [x ** 2 for x in nums]
print squares # Prints [0, 1, 4, 9, 16]

列表解析在有条件限制的情形下也可以使用

1
2
3
nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0]
print even_squares # Prints "[0, 4, 16]"

字典(Dictionaries)

Python中的字典数据类型存放键(key)、值(value),与Java中的map和JavaScript中的对象(object)相似,在Python中你可以这样来使用它:

1
2
3
4
5
6
7
8
9
10
d = {'cat': 'cute', 'dog': 'furry'}  # Create a new dictionary with some data
print d['cat'] # Get an entry from a dictionary; prints "cute"
print 'cat' in d # Check if a dictionary has a given key; prints "True"
d['fish'] = 'wet' # Set an entry in a dictionary
print d['fish'] # Prints "wet"
# print d['monkey'] # KeyError: 'monkey' not a key of d
print d.get('monkey', 'N/A') # 字典中并没有该键,故也没有值; prints "N/A"
print d.get('fish', 'N/A') # Get an element with a default; prints "wet"
del d['fish'] # Remove an element from a dictionary
print d.get('fish', 'N/A') # "fish" is no longer a key; prints "N/A"

如果你想知道得更多关于字典的细节,访问文档链接

循环(Loops):在字典中遍历所有的键(keys)非常简单

1
2
3
4
5
d = {'person': 2, 'cat': 4, 'spider': 8}
for animal in d:
legs = d[animal]
print 'A %s has %d legs' % (animal, legs) # %s是字符串通配符,%d是整形通配符,对象格式化输出
# Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs"

如果你想获取字典中的键以及相应的值,使用iteritems方法:

1
2
3
4
d = {'person': 2, 'cat': 4, 'spider': 8}
for animal, legs in d.iteritems():
print 'A %s has %d legs' % (animal, legs)
# Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs"

字典解析(Dictionary comprehensions):与列表解析相似,结构化字典也相当简单。比如

1
2
3
nums = [0, 1, 2, 3, 4]
even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}
print even_num_to_square # Prints "{0: 0, 2: 4, 4: 16}"

集合(Sets)

在Python中,集合是由一组无序的各不相同的元素组成。举个简单例子,

1
2
3
4
5
6
7
8
9
10
animals = {'cat', 'dog'}
print 'cat' in animals # Check if an element is in a set; prints "True"
print 'fish' in animals # prints "False"
animals.add('fish') # Add an element to a set
print 'fish' in animals # Prints "True"
print len(animals) # Number of elements in a set; prints "3"
animals.add('cat') # 往集合中添加已经有的元素,集合不会发生任何变化
print len(animals) # Prints "3"
animals.remove('cat') # Remove an element from a set
print len(animals) # Prints "2"

如果你想知道得更多关于集合的细节,访问文档链接

循环:集合中的循环语法与字典中的类似;然而因为集合是无序的,所以你无法知道集合中元素的顺序:

1
2
3
4
animals = {'cat', 'dog', 'fish'}
for idx, animal in enumerate(animals):
print '#%d: %s' % (idx + 1, animal)
# Prints "#1: fish", "#2: dog", "#3: cat"

集合解析(Set comprehensions): 与列表和字典类似,我们可以通过Set comprehensions轻松构建集合

1
2
3
from math import sqrt
nums = {int(sqrt(x)) for x in range(30)}
print nums # Prints "set([0, 1, 2, 3, 4, 5])"

元组(Tuples)

元组与列表一样,也是一种序列,只不过,元组中的元素是不能被修改的。还有,在字典中元组可以作为键使用,在集合中当做元素,列表却不可以。比如

1
2
3
4
5
d = {(x, x + 1): x for x in range(10)}  # Create a dictionary with tuple keys, Prints "{(0, 1): 0, (1, 2): 1, (6, 7): 6, (5, 6): 5, (7, 8): 7, (8, 9): 8, (4, 5): 4, (2, 3): 2, (9, 10): 9, (3, 4): 3}" 是无序的
t = (5, 6) # Create a tuple
print type(t) # Prints "<type 'tuple'>"
print d[t] # Prints "5"
print d[(1, 2)] # Prints "1"

如果你想知道得更多关于元组的细节,访问文档链接

函数(Functions)

在Python中函数通过def来定义,比如,

1
2
3
4
5
6
7
8
9
10
11
def sign(x):
if x > 0:
return 'positive'
elif x < 0:
return 'negative'
else:
return 'zero'

for x in [-1, 0, 1]:
print sign(x)
# Prints "negative", "zero", "positive"

我们经常在定义函数时通常加上一些关键词声明(keywords augment),比如:

1
2
3
4
5
6
7
8
def hello(name, loud=False):
if loud:
print 'HELLO, %s!' % name.upper()
else:
print 'Hello, %s' % name

hello('Bob') # Prints "Hello, Bob"
hello('Fred', loud=True) # Prints "HELLO, FRED!"

如果你想知道得更多关于函数的细节,访问文档链接

类(Classes)

Python中定义类的语法非常直接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Greeter(object):

# Constructor
def __init__(self, name):
self.name = name # Create an instance variable

# Instance method
def greet(self, loud=False):
if loud:
print 'HELLO, %s!' % self.name.upper()
else:
print 'Hello, %s' % self.name

g = Greeter('Fred') # Construct an instance of the Greeter class
g.greet() # Call an instance method; prints "Hello, Fred"
g.greet(loud=True) # Call an instance method; prints "HELLO, FRED!"

如果你想知道得更多关于类的细节,访问文档链接


Numpy

Numpy是Python中负责科学计算的核心库之一。Numpy在处理多维数组上性能强大,它还有许多处理这些数组的工具。如果你事先熟悉了MATLAB,你可能发现会对你入门numpy有点点帮助。

数组(Arrays)

Numpy数组是相同类型的、有非负整数索引的、在网格状中的值。数据的维度等于数组的秩rank;数组的shape属性指的是数组的。。。。(说了这么多,按照我的理解,你就把Numpy中的数组看成是一个m*n的矩阵好了,shape属性是这个矩阵的行数和列数,索引的办法也是一层层的)

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np

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

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

Numpy也提供了许多建立(construct)数据的函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 indexing)

Numpy提供几个索引数组的方法

分片:与Python的列表相似,Numpy也可以被分片。因为数组可能是多维度的,你必须明确数组中每个维度的分片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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]])

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

# A slice of an array is a view into the same data, so modifying it
# will modify the original 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"

你也可以将分片索引和整数索引混合使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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]])

# Two ways of accessing the data in the middle row of the 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)"

# We can make the same distinction when accessing columns of an array:
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)"

整数数组索引:当你使用切片索引Numpy数组时,结果永远是初始数组的一个子数组。相反,整数数组索引允许你使用数组中的数据重新建立绝对数组。比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import numpy as np

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

# An example of integer array indexing.
# The returned array will have shape (3,) and
print a[[0, 1, 2], [0, 1, 0]] # Prints "[1 4 5]"

# The above example of integer array indexing is equivalent to this:
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]"

整数数组索引的一个有用技巧是选择或转换每一行的某一个元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import numpy as np

# Create a new array from which we will select elements
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])

# Select one element from each row of a using the indices in b
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]])

布尔型数组索引:布尔型数组索引能够让你挑选出数组的绝对元素。通常,这种类型的索引被用于选择数组中满足某些条件的元素。比如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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]"

# We can do all of the above in a single concise statement:
print a[a > 2] # Prints "[3 4 5 6]"

如果你想了解跟过关于数组索引的细节,访问文档链接

数据类型

每个Numpy数组都是一组相同类型的数据。Numpy提供了许多数值类型,你可以用它们来建立数组。当你建立一个数组之后,Numpy就会尝试着猜测它的数据类型,函数就不一样,函数在创建数组的时候回特别声明它所处理数据的类型。比如

1
2
3
4
5
6
7
8
9
10
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"

关于Numpy数据类型的更多细节,访问文档链接

Array math

基本的数学函数是可以适用数组的点乘的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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)

值得注意的是,不同于MATLAB,Numpy中的*是点乘运算,而非矩阵乘法运算。所以我们用点dot函数来计算向量的内积和进行矩阵和向量的乘法。点dot同时也可以作为Numpy模块中的函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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)

Numpy有很多计算数组的函数,其中一个便是sum

1
2
3
4
5
6
7
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]"

你可以在这个文档链接找到Numpy中所有的数学函数

除了用数组来进行数学函数计算之外,我们通常需要重塑或者对数组进行其他操作。矩阵的转置是一个最简单的例子,简单的用T就可以实现

1
2
3
4
5
6
7
8
9
10
11
12
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提供了许多函数,在这个文档找到更多细节。

广播(broadcasting)

广播是Python中很强大的一种机制,在进行算术运算的时候,它能够允许Numpy对不同shape属性的数组进行操作。一般情况下, 我们会有一个较大的数组和一个较小的数组,也就是数组的shape属性不同,而我们想通过多次使用较小的数组来对较大的数组进行算术运算。

比如,假设我们需要给矩阵的每一行元素加上一个常数向量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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) # 创建一个跟x一样shape属性的空白矩阵

# 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中计算这样的循环任务将会非常缓慢。因为给矩阵x的每一行加上一个v等同于直接在矩阵上叠加vv,然后对x和叠加的矩阵vv进行点对点的相加。如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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的广播操作则允许我们无需创建v的拷贝版本而直接进行计算。使用广播的话就简单很多了,广播通常也能够使你的代码更准确更迅速,换句话说,Python的广播机制使得运算更加方便

1
2
3
4
5
6
7
8
9
10
11
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]]"

是不是简化了许多!

对两个数组进行广播操作要遵循以下几个规则:

  1. 如果数组没有相同的秩(rank),那么将低秩数数组的shape属性加1,使得它们的shape属性一样;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    >>> import numpy as np
    >>> a = np.arange(0, 60, 10).reshape(-1, 1)
    >>> a
    array([[ 0], [10], [20], [30], [40], [50]])
    >>> a.shape
    (6, 1)

    >>> b = np.arange(0, 5)
    >>> b
    array([0, 1, 2, 3, 4])
    >>> b.shape
    (5,) # a与b的shape属性不同

    >>> c = a + b
    >>> c
    array([[ 0, 1, 2, 3, 4],
    [10, 11, 12, 13, 14],
    [20, 21, 22, 23, 24],
    [30, 31, 32, 33, 34],
    [40, 41, 42, 43, 44],
    [50, 51, 52, 53, 54]])
    >>> c.shape
    (6, 5)
    #二维数组a,其shape为(6,1),一维数组b,其shape为(5,),由于a和b的shape长度不同,根据规则1,需要让b的shape向a对齐,于是将b的shape前面加1,补齐为(1,5)
  2. 广播之后,输出数组的shape是输入数组shape的各个轴上的最大值;
    |A (4d array)|8 x 1 x 6 x 1|
    |:—|—:|
    |B (3d array)| 7 x 1 x 5|
    |Result (4d array)|8 x 7 x 6 x 5|

  3. 在任何维度,一个数组的大小为1而另一个数组的大小比1大,那么第一个数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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

# 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

# Add a vector to each column of a matrix
# 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 row 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

其实这个地方我也没弄太明白,我觉得应该类似于矩阵里边的运算,两个矩阵进行运算,第一个矩阵的列数必须跟第二个矩阵的行数相等,不然无法运算。Python的广播机制应该跟这个意思差不太多,只不过它帮你省去了思考的时间,但是自己在使用数组运算的时候应该事先就要考虑好矩阵的维度,不然会得出截然不同的结果。

后面我会继续跟进这个话题。


SciPy

SciPy提供大量用于计算Numpy数组的函数,并且对于不同行业的科学和工程有广泛的应用

图像处理(Image operations)

对于图像处理,SciPy有几个基本的处理函数。比如,从磁盘中读取图像并存放在Numpy数组中,可以在磁盘中写入Numpy数组存储为图像,可以重新调整图像大小。(图片本来由一个个的像素点组成,这些像素点即是组成数组的元素)下面是一个简单的例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from scipy.misc import imread, imsave, imresize

# Read an JPEG image into a numpy array
img = imread('assets/cat.jpg')
print img.dtype, img.shape # Prints "uint8 (400, 248, 3)"

# We can tint the image by scaling each of the color channels
# by a different scalar constant. The image has shape (400, 248, 3);
# we multiply it by the array [1, 0.95, 0.9] of shape (3,);
# numpy broadcasting means that this leaves the red channel unchanged,
# and multiplies the green and blue channels by 0.95 and 0.9
# respectively.
img_tinted = img * [1, 0.95, 0.9]

# Resize the tinted image to be 300 by 300 pixels.
img_tinted = imresize(img_tinted, (300, 300))

# Write the tinted image back to disk
imsave('assets/cat_tinted.jpg', img_tinted)

左边是原始图像,右边是调整大小之后的图像

点之间的距离

给定一个集合,函数scipy.spatial.distance.pdist可以计算所有点之间的距离

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import numpy as np
from scipy.spatial.distance import pdist, squareform

# Create the following array where each row is a point in 2D space:
# [[0 1]
# [1 0]
# [2 0]]
x = np.array([[0, 1], [1, 0], [2, 0]])
print x

# Compute the Euclidean distance between all rows of x.
# d[i, j] is the Euclidean distance between x[i, :] and x[j, :],
# and d is the following array:
# [[ 0. 1.41421356 2.23606798]
# [ 1.41421356 0. 1. ]
# [ 2.23606798 1. 0. ]]
d = squareform(pdist(x, 'euclidean'))
print d

scipy.spatial.distance.cdist是一个相似的函数,computes the distance between all pairs across two sets of points


Matplotlib

Matplotlib是一个专门用来作图的库。这小节中会对matplotlib.pyplot函数简单介绍

作图(Plotting)

Matplotlib中最重要的一个函数是plot,它可用来处理二维数据。

1
2
3
4
5
6
7
8
9
10
import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)

# Plot the points using matplotlib
plt.plot(x, y)
plt.show() # You must call plt.show() to make graphics appear.

运行代码,得到如下图形

再添加一点点额外代码就可以轻松同时画出多条曲线、添加标题、图例、轴标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)

# Plot the points using matplotlib
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine'])
plt.show()

子图(Subplots)

你也可以使用subplot在同一张图片中画出不同的图像,下面是一个简单的例子,同时在一幅图中画出sincos的曲线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)

# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)

# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')

# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')

# Show the figure.
plt.show()

图像(Images)

使用imshow函数呈现图像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np
from scipy.misc import imread, imresize
import matplotlib.pyplot as plt

img = imread('assets/cat.jpg')
img_tinted = img * [1, 0.95, 0.9]

# Show the original image
plt.subplot(1, 2, 1)
plt.imshow(img)

# Show the tinted image
plt.subplot(1, 2, 2)

# A slight gotcha with imshow is that it might give strange results
# if presented with data that is not uint8. To work around this, we
# explicitly cast the image to uint8 before displaying it.
plt.imshow(np.uint8(img_tinted))
plt.show()

------本文结束,欢迎收藏本站、分享、评论或联系作者!------
点击查看
赠人玫瑰 手有余香
0%