分享

斯坦福CS231n深度学习课程笔记翻译(一)

 湖唐 2018-06-08
 CS231n全称: Convolutional Neural Networks for Visual Recognition,即面向视觉识别的卷积神经网络。该课程是 斯坦福大学计算机视觉实验室推出的课程。

课程描述

计算机视觉在社会中已经逐渐普及,并广泛运用于搜索检索、图像理解、手机应用、地图导航、医疗制药、无人机和无人驾驶汽车等领域。而这些应用的核心技术就是图像分类、图像定位和图像探测等视觉识别任务。近期神经网络(也就是“深度学习”)方法上的进展极大地提升了这些代表当前发展水平的视觉识别系统的性能。 本课程将深入讲解深度学习框架的细节问题,聚焦面向视觉识别任务(尤其是图像分类任务)的端到端学习模型。在10周的课程中,学生们将会学习如何实现、训练和调试他们自己的神经网络,并建立起对计算机视觉领域的前沿研究方向的细节理解。

终的作业将包括训练一个有几百万参数的卷积神经网络,并将其应用到最大的图像分类数据库(ImageNet)上。我们将会聚焦于教授如何确定图像识别问题,学习算法(比如反向传播算法),对网络的训练和精细调整(fine-tuning)中的工程实践技巧,指导学生动手完成课程作业和最终的课程项目。本课程的大部分背景知识和素材都来源于 ImageNet Challenge竞赛。


课程内容 :

官方课程安 排及资源获取请点击 这里 ,课程视频请在Youtube上查看 Andrej Karpathy 创建的 播放列表

官方笔记:共9篇,本文为第1篇

原文地址: python/numpy tutorial

翻译:Python Numpy教程

这篇教程由Justin Johnson创作。

我们将使用Python编程语言来完成本课程的所有作业。Python是一门伟大的通用编程语言,在一些常用库(numpy, scipy, matplotlib)的帮助下,它又会变成一个强大的科学计算环境。

我们期望你们中大多数人对于Python语言和Numpy库比较熟悉,而对于没有Python经验的同学,这篇教程可以帮助你们快速了解Python编程环境和如何使用Python作为科学计算工具。

一部分同学对于Matlab有一定经验。对于这部分同学,我们推荐阅读 numpy for Matlab users页面。

你们还可以查看本教程的IPython notebook版。该教程是由Volodymyr KuleshovIsaac Caswell为课程CS 228创建的。

内容列表:

· Python

· 基本数据类型

· 容器· 列表· 字典 · 集合· 元组

· 函数

·

· Numpy· 数组· 访问数组· 数据类· 数组计算· 广播

· SciP· 图像操作· MATLAB文件· 点之间的距离

· Matplotlib· 绘制图形· 绘制多个图形· 图像

Python

Python是一种高级的,动态类型的多范型编程语言。很多时候,大家会说Python看起来简直和伪代码一样,这是因为你能够通过很少行数的代码表达出很有力的思想。举个例子,下面是用Python实现的经典的quicksort算法例子:

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

Python版本

Python有两个支持的版本,分别是2.7和3.4。这有点让人迷惑,3.0向语言中引入了很多不向后兼容的变化,2.7下的代码有时候在3.4下是行不通的。在这个课程中,我们使用的是2.7版本。

如何查看版本呢?使用python --version命令。

基本数据类型

和大多数编程语言一样,Python拥有一系列的基本数据类型,比如整型、浮点型、布尔型和字符串等。这些类型的使用方式和在其他语言中的使用方式是类似的。

数字:整型和浮点型的使用与其他语言类似。

 1x = 3
2print type(x) # Prints "<type 'int'>"
3print x # Prints "3"
4print x + 1 # Addition; prints "4"
5print x - 1 # Subtraction; prints "2"
6print x * 2 # Multiplication; prints "6"
7print x ** 2 # Exponentiation; prints "9"
8x += 1
9print x # Prints "4"
10x *= 2
11print x # Prints "8"
12y = 2.5
13print type(y) # Prints "<type 'float'>"
14print y, y + 1, y * 2, y ** 2 # Prints "2.5 3.5 5.0 6.25"
需要注意的是,Python中没有 x++ 和 x-- 的操作符。

Python也有内置的长整型和复杂数字类型,具体细节可以查看文档

布尔型:Python实现了所有的布尔逻辑,但用的是英语,而不是我们习惯的操作符(比如&&和||等)。

1t = True
2f = False
3print type(t) # Prints "<type 'bool'>"
4print t and f # Logical AND; prints "False"
5print t or f # Logical OR; prints "True"
6print not t # Logical NOT; prints "False"
7print t != f # Logical XOR; prints "True"
字符串:Python对字符串的支持非常棒。
1hello = 'hello'   # String literals can use single quotes
2world = "world" # or double quotes; it does not matter.
3print hello # Prints "hello"
4print len(hello) # String length; prints "5"
5hw = hello + ' ' + world # String concatenation
6print hw # prints "hello world"
7hw12 = '%s %s %d' % (hello, world, 12) # sprintf style string formatting
8print hw12 # prints "hello world 12"

字符串对象有一系列有用的方法,比如:

1s = "hello"
2print s.capitalize() # Capitalize a string; prints "Hello"
3print s.upper() # Convert a string to uppercase; prints "HELLO"
4print s.rjust(7) # Right-justify a string, padding with spaces; prints " hello"
5print s.center(7) # Center a string, padding with spaces; prints " hello "
6print s.replace('l', '(ell)') # Replace all instances of one substring with another;
7 # prints "he(ell)(ell)o"
8print ' world '.strip() # Strip leading and trailing whitespace; prints "world"

如果想详细查看字符串方法,请看文档

容器Containers

译者注:有知友建议container翻译为复合数据类型,供读者参考。

Python有以下几种容器类型:列表(lists)、字典(dictionaries)、集合(sets)和元组(tuples)。

列表Lists

列表就是Python中的数组,但是列表长度可变,且能包含不同类型元素。

1xs = [3, 1, 2]   # Create a list
2print xs, xs[2] # Prints "[3, 1, 2] 2"
3print xs[-1] # Negative indices count from the end of the list; prints "2"
4xs[2] = 'foo' # Lists can contain elements of different types
5print xs # Prints "[3, 1, 'foo']"
6xs.append('bar') # Add a new element to the end of the list
7print xs # Prints
8x = xs.pop() # Remove and return the last element of the list
9print x, xs # Prints "bar [3, 1, 'foo']"

列表的细节,同样可以查阅文档

切片Slicing:为了一次性地获取列表中的元素,Python提供了一种简洁的语法,这就是切片。

1nums = range(5)    # range is a built-in function that creates a list of integers
2print nums # Prints "[0, 1, 2, 3, 4]"
3print nums[2:4] # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
4print nums[2:] # Get a slice from index 2 to the end; prints "[2, 3, 4]"
5print nums[:2] # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
6print nums[:] # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]"
7print nums[:-1] # Slice indices can be negative; prints ["0, 1, 2, 3]"
8nums[2:4] = [8, 9] # Assign a new sublist to a slice
9print nums # Prints "[0, 1, 8, 8, 4]"
在Numpy数组的内容中,我们会再次看到切片语法。

循环Loops:我们可以这样遍历列表中的每一个元素:

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

如果想要在循环体内访问每个元素的指针,可以使用内置的 enumerate函数
1animals = ['cat', 'dog', 'monkey']
2for idx, animal in enumerate(animals):
3 print '#%d: %s' % (idx + 1, animal)
4# Prints "#1: cat", "#2: dog", "#3: monkey", each on its own line
列表推导List comprehensions:在编程的时候,我们常常想要将一种数据类型转换为另一种。下面是一个简单例子,将列表中的每个元素变成它的平方。
1nums = [0, 1, 2, 3, 4]
2squares = []
3for x in nums:
4 squares.append(x ** 2)
5print squares # Prints [0, 1, 4, 9, 16]
使用列表推导,你就可以让代码简化很多:
1nums = [0, 1, 2, 3, 4]
2squares = [x ** 2 for x in nums]
3print squares # Prints [0, 1, 4, 9, 16]
列表推导还可以包含条件:
1nums = [0, 1, 2, 3, 4]
2even_squares = [x ** 2 for x in nums if x % 2 == 0]
3print even_squares # Prints "[0, 4, 16]"

字典Dictionaries

字典用来储存(键, 值)对,这和Java中的Map差不多。你可以这样使用它: 
 1d = {'cat': 'cute', 'dog': 'furry'}  # Create a new dictionary with some data
2print d['cat'] # Get an entry from a dictionary; prints "cute"
3print 'cat' in d # Check if a dictionary has a given key; prints "True"
4d['fish'] = 'wet' # Set an entry in a dictionary
5print d['fish'] # Prints "wet"
6# print d['monkey'] # KeyError: 'monkey' not a key of d
7print d.get('monkey', 'N/A') # Get an element with a default; prints "N/A"
8print d.get('fish', 'N/A') # Get an element with a default; prints "wet"
9del d['fish'] # Remove an element from a dictionary
10print d.get('fish', 'N/A') # "fish" is no longer a key; prints "N/A"

想要知道字典的其他特性,请查阅文档

循环Loops:在字典中,用键来迭代更加容易。

1d = {'person': 2, 'cat': 4, 'spider': 8}
2for animal in d:
3 legs = d[animal]
4 print 'A %s has %d legs' % (animal, legs)
5# Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs"
如果你想要访问键和对应的值,那就使用 iteritems方法:
1d = {'person': 2, 'cat': 4, 'spider': 8}
2for animal, legs in d.iteritems():
3 print 'A %s has %d legs' % (animal, legs)
4# Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs"

字典推导Dictionary comprehensions:和列表推导类似,但是允许你方便地构建字典。

1nums = [0, 1, 2, 3, 4]
2even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}
3print even_num_to_square # Prints "{0: 0, 2: 4, 4: 16}"
集合Sets
集合是独立不同个体的无序集合。示例如下: 
 1animals = {'cat', 'dog'}
2print 'cat' in animals # Check if an element is in a set; prints "True"
3print 'fish' in animals # prints "False"
4animals.add('fish') # Add an element to a set
5print 'fish' in animals # Prints "True"
6print len(animals) # Number of elements in a set; prints "3"
7animals.add('cat') # Adding an element that is already in the set does nothing
8print len(animals) # Prints "3"
9animals.remove('cat') # Remove an element from a set
10print len(animals) # Prints "2"
和前面一样,要知道更详细的,查看 文档

循环Loops:在集合中循环的语法和在列表中一样,但是集合是无序的,所以你在访问集合的元素的时候,不能做关于顺序的假设。

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

集合推导Set comprehensions:和字典推导一样,可以很方便地构建集合:

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

元组Tuples

元组是一个值的有序列表(不可改变)。从很多方面来说,元组和列表都很相似。和列表最重要的不同在于,元组可以在字典中用作键,还可以作为集合的元素,而列表不行。例子如下: 
1d = {(x, x + 1): x for x in range(10)}  # Create a dictionary with tuple keys
2print d
3t = (5, 6) # Create a tuple
4print type(t) # Prints "<type 'tuple'>"
5print d[t] # Prints "5"
6print d[(1, 2)] # Prints "1"
文档有更多元组的信息。

函数Functions

Python函数使用def来定义函数: 
 1def sign(x):
2 if x > 0:
3 return 'positive'
4 elif x < 0:
5 return 'negative'
6 else:
7 return 'zero'
8for x in [-1, 0, 1]:
9 print sign(x)
10# Prints "negative", "zero", "positive"
我们常常使用可选参数来定义函数:
1def hello(name, loud=False):
2 if loud:
3 print 'HELLO, %s' % name.upper()
4 else:
5 print 'Hello, %s!' % name
6hello('Bob') # Prints "Hello, Bob"
7hello('Fred', loud=True) # Prints "HELLO, FRED!"

函数还有很多内 容,可以查看文档

类Classes

Python对于类的定义是简单直接的:

 1class Greeter(object):
2 # Constructor
3 def __init__(self, name):
4 self.name = name # Create an instance variable
5 # Instance method
6 def greet(self, loud=False):
7 if loud:
8 print 'HELLO, %s!' % self.name.upper()
9 else:
10 print 'Hello, %s' % self.name
11g = Greeter('Fred') # Construct an instance of the Greeter class
12g.greet() # Call an instance method; prints "Hello, Fred"
13g.greet(loud=True) # Call an instance method; prints "HELLO, FRED!"

更多类的信息请查阅文档

Numpy

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

数组Arrays

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

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

 1import numpy as np
2a = np.array([1, 2, 3]) # Create a rank 1 array
3print type(a) # Prints "<type 'numpy.ndarray'>"
4print a.shape # Prints "(3,)"
5print a[0], a[1], a[2] # Prints "1 2 3"
6a[0] = 5 # Change an element of the array
7print a # Prints "[5, 2, 3]"
8b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array
9print b # 显示一下矩阵b
10print b.shape # Prints "(2, 3)"
11print b[0, 0], b[0, 1], b[1, 0] # Prints "1 2 4"

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

 1import numpy as np
2a = np.zeros((2,2)) # Create an array of all zeros
3print a # Prints "[[ 0. 0.]
4 # [ 0. 0.]]"
5b = np.ones((1,2)) # Create an array of all ones
6print b # Prints "[[ 1. 1.]]"
7c = np.full((2,2), 7) # Create a constant array
8print c # Prints "[[ 7. 7.]
9 # [ 7. 7.]]"
10d = np.eye(2) # Create a 2x2 identity matrix
11print d # Prints "[[ 1. 0.]
12 # [ 0. 1.]]"
13e = np.random.random((2,2)) # Create an array filled with random values
14print e # Might print "[[ 0.91940167 0.08143941]
15 # [ 0.68744134 0.87236687]]"

其他数组相关方法,请查看文档

访问数组

Numpy提供了多种访问数组的方法。

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

 1import numpy as np
2# Create the following rank 2 array with shape (3, 4)
3# [[ 1 2 3 4]
4# [ 5 6 7 8]
5# [ 9 10 11 12]]
6a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
7# Use slicing to pull out the subarray consisting of the first 2 rows
8# and columns 1 and 2; b is the following array of shape (2, 2):
9# [[2 3]
10# [6 7]]
11b = a[:2, 1:3]
12# A slice of an array is a view into the same data, so modifying it
13# will modify the original array.
14print a[0, 1] # Prints "2"
15b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1]
16print a[0, 1] # Prints "77"
你可以同时使用整型和切片语法来访问数组。但是,这样做会产生一个比原数组低阶的新数组。需要注意的是,这里和MATLAB中的情况是不同的:
 1import numpy as np
2# Create the following rank 2 array with shape (3, 4)
3# [[ 1 2 3 4]
4# [ 5 6 7 8]
5# [ 9 10 11 12]]
6a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
7# Two ways of accessing the data in the middle row of the array.
8# Mixing integer indexing with slices yields an array of lower rank,
9# while using only slices yields an array of the same rank as the
10# original array:
11row_r1 = a[1, :] # Rank 1 view of the second row of a
12row_r2 = a[1:2, :] # Rank 2 view of the second row of a
13print row_r1, row_r1.shape # Prints "[5 6 7 8] (4,)"
14print row_r2, row_r2.shape # Prints "[[5 6 7 8]] (1, 4)"
15# We can make the same distinction when accessing columns of an array:
16col_r1 = a[:, 1]
17col_r2 = a[:, 1:2]
18print col_r1, col_r1.shape # Prints "[ 2 6 10] (3,)"
19print col_r2, col_r2.shape # Prints "[[ 2]
20 # [ 6]
21 # [10]] (3, 1)"

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

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

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

 1import numpy as np
2# Create a new array from which we will select elements
3a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
4print a # prints "array([[ 1, 2, 3],
5 # [ 4, 5, 6],
6 # [ 7, 8, 9],
7 # [10, 11, 12]])"
8# Create an array of indices
9b = np.array([0, 2, 0, 1])
10# Select one element from each row of a using the indices in b
11print a[np.arange(4), b] # Prints "[ 1 6 7 11]"
12# Mutate one element from each row of a using the indices in b
13a[np.arange(4), b] += 10
14print a # prints "array([[11, 2, 3],
15 # [ 4, 5, 16],
16 # [17, 8, 9],
17 # [10, 21, 12]])

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

 1import numpy as np
2a = np.array([[1,2], [3, 4], [5, 6]])
3bool_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.
7print 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
13print a[bool_idx] # Prints "[3 4 5 6]"
14# We can do all of the above in a single concise statement:
15print a[a > 2] # Prints "[3 4 5 6]"

为了教程的简介,有很多数组访问的细节我们没有详细说明,可以查看文档

数据类型

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

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

数组计算

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

 1import numpy as np
2x = np.array([[1,2],[3,4]], dtype=np.float64)
3y = 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]]

7print x + y
8print np.add(x, y)
9# Elementwise difference; both produce the array
10# [[-4.0 -4.0]
11# [-4.0 -4.0]]

12print x - y
13print np.subtract(x, y)
14# Elementwise product; both produce the array
15# [[ 5.0 12.0]
16# [21.0 32.0]]

17print x * y
18print np.multiply(x, y)
19# Elementwise division; both produce the array
20# [[ 0.2 0.33333333]
21# [ 0.42857143 0.5 ]]

22print x / y
23print np.divide(x, y)
24# Elementwise square root; produces the array
25# [[ 1. 1.41421356]
26# [ 1.73205081 2. ]]

27print np.sqrt(x)
和MATLAB不同,*是元素逐个相乘,而不是矩阵乘法。在Numpy中使用dot来进行矩阵乘法:
 1import numpy as np
2x = np.array([[1,2],[3,4]])
3y = np.array([[5,6],[7,8]])
4v = np.array([9,10])
5w = np.array([11, 12])
6# Inner product of vectors; both produce 219
7print v.dot(w)
8print np.dot(v, w)
9# Matrix / vector product; both produce the rank 1 array [29 67]
10print x.dot(v)
11print np.dot(x, v)
12# Matrix / matrix product; both produce the rank 2 array
13# [[19 22]
14# [43 50]]

15print x.dot(y)
16print np.dot(x, y)

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

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

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

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

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

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

广播Broadcasting

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

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

 1import 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
4x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
5v = np.array([1, 0, 1])
6y = 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
8for 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]]
15print y

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

 1import 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
4x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
5v = np.array([1, 0, 1])
6vv = np.tile(v, (4, 1)) # Stack 4 copies of v on top of each other
7print vv # Prints "[[1 0 1]
8 # [1 0 1]
9 # [1 0 1]
10 # [1 0 1]]"
11y = x + vv # Add x and vv elementwise
12print y # Prints "[[ 2 2 4
13 # [ 5 5 7]
14 # [ 8 8 10]
15 # [11 11 13]]"

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

 1import 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
4x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
5v = np.array([1, 0, 1])
6y = x + v # Add v to each row of x using broadcasting
7print y # Prints "[[ 2 2 4]
8 # [ 5 5 7]
9 # [ 8 8 10]
10 # [11 11 13]]"

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

  1. 如果数组的秩不同,使用1来将秩较小的数组进行扩展,直到两个数组的尺寸的长度都一样。
  2. 如果两个数组在某个维度上的长度是一样的,或者其中一个数组在该维度上长度为1,那么我们就说这两个数组在该维度上是相容的。
  3. 如果两个数组在所有维度上都是相容的,他们就能使用广播。
  4. 如果两个输入数组的尺寸不同,那么注意其中较大的那个尺寸。因为广播之后,两个数组的尺寸将和那个较大的尺寸一样。
  5. 在任何一个维度上,如果一个数组的长度为1,另一个数组长度大于1,那么在该维度上,就好像是对第一个数组进行了复制。

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

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

下面是一些广播机制的使用:

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

Numpy文档

这篇教程涉及了你需要了解的numpy中的一些重要内容,但是numpy远不止如此。可以查阅numpy文献来学习更多。

SciPy

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

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

图像操作

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

 1from scipy.misc import imread, imsave, imresize
2# Read an JPEG image into a numpy array
3img = imread('assets/cat.jpg')
4print 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.
11img_tinted = img * [1, 0.95, 0.9]
12# Resize the tinted image to be 300 by 300 pixels.
13img_tinted = imresize(img_tinted, (300, 300))
14# Write the tinted image back to disk
15imsave('assets/cat_tinted.jpg', img_tinted)

译者注:如果运行这段代码出现类似ImportError: cannot import name imread的报错,那么请利用pip进行Pillow的下载,可以解决问题。命令:pip install Pillow。



左边是原始图片,右边是变色和变形的图片。

MATLAB文件

函数scipy.io.loadmatscipy.io.savemat能够让你读和写MATLAB文件。具体请查看文档

点之间的距离

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

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

 1import numpy as np
2from 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]]
7x = np.array([[0, 1], [1, 0], [2, 0]])
8print 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. ]]
15d = squareform(pdist(x, 'euclidean'))
16print d
具体细节请阅读 文档

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

Matplotlib

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

绘图

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

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

运行上面代码会产生下面的作图:


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

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


可以在文档中阅读更多关于plot的内容。

绘制多个图像

可以使用subplot函数来在一幅图中画不同的东西:

 1import numpy as np
2import matplotlib.pyplot as plt
3# Compute the x and y coordinates for points on sine and cosine curves
4x = np.arange(0, 3 * np.pi, 0.1)
5y_sin = np.sin(x)
6y_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.
9plt.subplot(2, 1, 1)
10# Make the first plot
11plt.plot(x, y_sin)
12plt.title('Sine')
13# Set the second subplot as active, and make the second plot.
14plt.subplot(2, 1, 2)
15plt.plot(x, y_cos)
16plt.title('Cosine')
17# Show the figure.
18plt.show()


关于subplot的更多细节,可以阅读文档

图像

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

 1import numpy as np
2from scipy.misc import imread, imresize
3import matplotlib.pyplot as plt
4img = imread('assets/cat.jpg')
5img_tinted = img * [1, 0.95, 0.9]
6# Show the original image
7plt.subplot(1, 2, 1)
8plt.imshow(img)
9# Show the tinted image
10plt.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.
14plt.imshow(np.uint8(img_tinted))
15plt.show()




- END -


转载声明:本文转载自知乎专栏: 智能单元
此系列笔记由 @杜客 、 @ShiqingFan ,@ 猴子 @堃堃 @李艺颖等共同翻译 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多