@K1999
2016-09-22T01:40:22.000000Z
字数 16030
阅读 2059
深度学习 CS231n
原文地址:http://cs231n.github.io/python-numpy-tutorial/#matplotlib-subplots
Python语言可以用短短几行代码快速实现你的想法,写出易读易懂的代码。
def quicksort(arr):if len(arr) <= 1:return arrpivot = 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]"
x = 3print 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 += 1print x # Prints "4"x *= 2print x # Prints "8"y = 2.5print type(y) # Prints "<type 'float'>"print y, y + 1, y * 2, y ** 2 # Prints "2.5 3.5 5.0 6.25"
同大多数语言不一样,Python中没有自增(i++)自减(i--)操作。
Python还实现了所有的布尔逻辑,但是没有使用逻辑运算符(&&, ||, etc.)而是使用英文单词:
t = Truef = Falseprint 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"
Python对字符串的支持也非常好
hello = 'hello' # String literals can use single quotesworld = "world" # or double quotes; it does not matter.print hello # Prints "hello"print len(hello) # String length; prints "5"hw = hello + ' ' + world # String concatenationprint hw # prints "hello world"hw12 = '%s %s %d' % (hello, world, 12) # sprintf style string formattingprint hw12 # prints "hello world 12"
Python还有一系列字符串处理的有用的方法
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"
Python有许多内置的容器类型,如列表(lists)、字典(dictionaries)、集合(sets)和元组(tuples)等。
Python中的列表相当于数组,但是列表长度可变,而且列表中可以包含多种不同类型的元素。
xs = [3, 1, 2] # Create a listprint 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 typesprint xs # Prints "[3, 1, 'foo']"xs.append('bar') # Add a new element to the end of the listprint xs # Prints "[3, 1, 'foo', 'bar']"x = xs.pop() # Remove and return the last element of the listprint x, xs # Prints "bar [3, 1, 'foo']"
切片(Slicing):可以一次性的获得列表的部分元素。
nums = range(5) # range is a built-in function that creates a list of integersprint 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 sliceprint nums # Prints "[0, 1, 8, 9, 4]"
循环(Loops):我们可以用如下的方式遍历列表。
animals = ['cat', 'dog', 'monkey']for animal in animals:print animal# Prints "cat", "dog", "monkey", each on its own line.
如果想要在循环体内访问每个元素的指针,可以使用内置的enumerate函数。
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
列表还提供了一种快速的将列表数据元素转换的方法。
# 普通方法nums = [0, 1, 2, 3, 4]squares = []for x in nums:squares.append(x ** 2)print squares # Prints [0, 1, 4, 9, 16]# 简化方法nums = [0, 1, 2, 3, 4]squares = [x ** 2 for x in nums]print squares # Prints [0, 1, 4, 9, 16]
该方法中还可以嵌入条件语句:
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]"
字典可以存储键值对(键,值),类似于Java中的Map。
d = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some dataprint 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 dictionaryprint d['fish'] # Prints "wet"# print d['monkey'] # KeyError: 'monkey' not a key of dprint d.get('monkey', 'N/A') # Get an element with a default; 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 dictionaryprint d.get('fish', 'N/A') # "fish" is no longer a key; prints "N/A"
循环(Loops):可以用“键”来很容易的遍历字典。
d = {'person': 2, 'cat': 4, 'spider': 8}for animal in d:legs = d[animal]print 'A %s has %d legs' % (animal, legs)# Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs"
用iteritems( )方法可以很方便的访问字典的键和对应的值:
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"
和列表相同,可以很方便的构建字典。
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}"
集合是独立的不同个体无序的组合在一起的容器。
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 setprint 'fish' in animals # Prints "True"print len(animals) # Number of elements in a set; prints "3"animals.add('cat') # Adding an element that is already in the set does nothingprint len(animals) # Prints "3"animals.remove('cat') # Remove an element from a setprint len(animals) # Prints "2"
遍历集合元素:
animals = {'cat', 'dog', 'fish'}for idx, animal in enumerate(animals):print '#%d: %s' % (idx + 1, animal)# Prints "#1: fish", "#2: dog", "#3: cat"
快速建立集合
from math import sqrtnums = {int(sqrt(x)) for x in range(30)}print nums # Prints "set([0, 1, 2, 3, 4, 5])"
元组是一个值的有序列表(不可改变)。从很多方面来说,元组和列表都很相似。和列表最重要的不同在于,元组可以在字典中用作键,还可以作为集合的元素,而列表不行。元组中的元素值是不允许修改的。
d = {(x, x + 1): x for x in range(10)} # Create a dictionary with tuple keyst = (5, 6) # Create a tupleprint type(t) # Prints "<type 'tuple'>"print d[t] # Prints "5"print d[(1, 2)] # Prints "1"
函数用def()定义。
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"
函数中的参数是可以带默认值的。
def hello(name, loud=False):if loud:print 'HELLO, %s!' % name.upper()else:print 'Hello, %s' % namehello('Bob') # Prints "Hello, Bob"hello('Fred', loud=True) # Prints "HELLO, FRED!"
Python中定义类的语法是非常简单的。
class Greeter(object):# Constructordef __init__(self, name):self.name = name # Create an instance variable# Instance methoddef greet(self, loud=False):if loud:print 'HELLO, %s!' % self.name.upper()else:print 'Hello, %s' % self.nameg = Greeter('Fred') # Construct an instance of the Greeter classg.greet() # Call an instance method; prints "Hello, Fred"g.greet(loud=True) # Call an instance method; prints "HELLO, FRED!"
Numpy是利用Python进行科学计算的核心库。它提供了高性能的多维数组对象,以及相关工具。
首先是创建数据和访问数组中的元素:
import numpy as npa = np.array([1, 2, 3]) # Create a rank 1 arrayprint 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 arrayprint a # Prints "[5, 2, 3]"b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 arrayprint b.shape # Prints "(2, 3)"print b[0, 0], b[0, 1], b[1, 0] # Prints "1 2 4"
Numpy 还有一些其他的创建数组的方法。
import numpy as npa = np.zeros((2,2)) # 建立一个全0的数组print a # Prints "[[ 0. 0.]# [ 0. 0.]]"b = np.ones((1,4)) # 建立一个1行4列的数组print b # Prints "[[ 1. 1. 1. 1.]]"c = np.full((2,2), 7) # Create a constant arrayprint c # Prints "[[ 7. 7.]# [ 7. 7.]]"d = np.eye(2) # Create a 2x2 identity matrixprint d # Prints "[[ 1. 0.]# [ 0. 1.]]"e = np.random.random((2,2)) # Create an array filled with random valuesprint e # Might print "[[ 0.91940167 0.08143941]# [ 0.68744134 0.87236687]]"
切片(Slicing):和Python列表类似,Numpy中的数组也有切片操作。因为数组是多维的,所以必须为每个维度指定切片。
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是a的一个切片,取a的前两行和1、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"
还可以使用整型和切片相结合的方式访问数组元素。
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 arow_r2 = a[1:2, :] # Rank 2 view of the second row of aprint 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)"
可以通过整形数组索引的方式访问数组。
import numpy as npa = np.array([[1,2], [3, 4], [5, 6]])# a是一个二维数组print a[[0, 1, 2], [0, 1, 0]]# 相当于访问a[0][0],a[1][2],a[2][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]"
利用整形数组索引访问数组的一个小窍门:可以利用整形数组索引来选择或者修改数组中每一行中的元素。
import numpy as np# Create a new array from which we will select elementsa = 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 indicesb = np.array([0, 2, 0, 1])# Select one element from each row of a using the indices in bprint a[np.arange(4), b] # Prints "[ 1 6 7 11]"# Mutate one element from each row of a using the indices in ba[np.arange(4), b] += 10print a # prints "array([[11, 2, 3],# [ 4, 5, 16],# [17, 8, 9],# [10, 21, 12]])
还有一种布尔型数组索引的方式。布尔型数组访问可以让你选择数组中任意元素。
import numpy as npa = 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_idxprint 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会去尽力猜测这个数组中的数据类型。Numpy中还提供了一个可选参数,当建立数组时可以明确指出数据类型。
import numpy as npx = np.array([1, 2]) # Let numpy choose the datatypeprint x.dtype # Prints "int32"x = np.array([1.0, 2.0]) # Let numpy choose the datatypeprint x.dtype # Prints "float64"x = np.array([1, 2], dtype=np.int64) # Force a particular datatypeprint x.dtype # Prints "int64"
import numpy as npx = 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 + yprint np.add(x, y)# Elementwise difference; both produce the array# [[-4.0 -4.0]# [-4.0 -4.0]]print x - yprint np.subtract(x, y)# Elementwise product; both produce the array# [[ 5.0 12.0]# [21.0 32.0]]print x * yprint np.multiply(x, y)# Elementwise division; both produce the array# [[ 0.2 0.33333333]# [ 0.42857143 0.5 ]]print x / yprint np.divide(x, y)# Elementwise square root; produces the array# [[ 1. 1.41421356]# [ 1.73205081 2. ]]print np.sqrt(x)
在Numpy中,'*'是矩阵元素之间逐个相乘,不是矩阵乘法。矩阵相乘用dot()函数。
import numpy as npx = 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 219print 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()函数。
import numpy as npx = 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]"
还有一些数组中的函数用于改造或者操作数组中的元素。譬如可以用数组的属性'T'来进行矩阵转置。
import numpy as npx = 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]"
Broadcasting是Numpy中的一种强有力的机制,他可以让不同大小的矩阵在一起进行计算。
假设我们想要把一个常数向量加到矩阵的每一行,我们可以这样做:
import numpy as np# We will add the vector v to each row of the matrix x,# storing the result in the matrix yx = 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 loopfor 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
上面的方法用到了循环,当矩阵规模足够大的时候,此种方法的执行速度就慢了,可以采用下边的方法:
import numpy as np# We will add the vector v to each row of the matrix x,# storing the result in the matrix yx = 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 otherprint vv # Prints "[[1 0 1]# [1 0 1]# [1 0 1]# [1 0 1]]"y = x + vv # Add x and vv elementwiseprint 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 yx = 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 broadcastingprint y # Prints "[[ 2 2 4]# [ 5 5 7]# [ 8 8 10]# [11 11 13]]"
下边是广播机制的一些应用。
import numpy as np# Compute outer product of vectorsv = 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 matrixx = 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
Numpy包提供了高性能的多维数组以及计算和操作这些数组的基本工具。SciPy包基于Numpy,提供了大量的操作和计算numpy数组的函数,这些函数对于各种类型的科学和工程计算非常有用。
from scipy.misc import imread, imsave, imresize# Read an JPEG image into a numpy arrayimg = 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 diskimsave('assets/cat_tinted.jpg', img_tinted)
左边是原始图片,右边是变色和变形的图片。
SciPy定义了一些有用的函数,可以用于计算集合中点之间的距离。
函数scipy.spatial.distance.pdist能够计算给定集合中所有点之间的距离。
import numpy as npfrom 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
Matplotlib是Python中的一个作图包。
plot是matplotlib包中的一个重要的函数,可以用plot函数画出二维图形。
import numpy as npimport matplotlib.pyplot as plt# Compute the x and y coordinates for points on a sine curvex = np.arange(0, 3 * np.pi, 0.1)y = np.sin(x)# Plot the points using matplotlibplt.plot(x, y)plt.show() # You must call plt.show() to make graphics appear.
执行上面的代码,得出下边的图形:
再加上一点少量的工作,就可以一次画出多条线,并且加上标题等。
import numpy as npimport matplotlib.pyplot as plt# Compute the x and y coordinates for points on sine and cosine curvesx = np.arange(0, 3 * np.pi, 0.1)y_sin = np.sin(x)y_cos = np.cos(x)# Plot the points using matplotlibplt.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()
可以使用subplot函数将多幅图画组合在一张图里:
import numpy as npimport matplotlib.pyplot as plt# Compute the x and y coordinates for points on sine and cosine curvesx = 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 plotplt.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()
可以用imshow()函数显示图片。
import numpy as npfrom scipy.misc import imread, imresizeimport matplotlib.pyplot as pltimg = imread('assets/cat.jpg')img_tinted = img * [1, 0.95, 0.9]# Show the original imageplt.subplot(1, 2, 1)plt.imshow(img)# Show the tinted imageplt.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()