標籤:

Python Numpy 教程

這是一篇翻譯。

原文:Python Numpy Tutorial

翻譯:我

如果有什麼問題歡迎留言私信指正探討。

正文:

這篇教程是由Justin Johnson.貢獻的。

在本門課程中我將會使用Python完成所有作業。Python是一個偉大的通用性編程語言,但是在一些流行的庫(numpy, scipy, matplotlib)的幫助下Python成為了強大的科學計算的環境。

我們希望你對Python和numpy有些經驗。在本章只是一個關於Pyhton和使用Python進行科學計算的速成課程。

Python

Python 是一種高級的,動態的,多泛型的編程語言。Python代碼很多時候看起來就像是偽代碼一樣,因此你可以使用很少的幾行可讀性很高的代碼來實現一個非常強大的想法。舉個例子,下面是使用Python來實現非常經典的快速排序演算法的代碼。

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 版本

當前有兩種不同的Python版本被支持,分別是2.7和3.4。令人不解的是,Python3.0引入了一些不可向下兼容的變換,所以使用2.7寫的代碼不一定能在3.4版本上運行,反之亦然。在本課程中代碼都是運行在2.7版本上的。

你可以在命令行中使用一下命令來檢查一下自己的Python版本

python --version

基礎數據類型

和很多語言一樣,Python也有幾種數據類型包括:整型,浮點型,波爾型和字元型。這些數據類型的表現方式和大家熟悉的其他語言一樣。

Numbers:整型和浮點型的工作方式和其他語言一樣。

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沒有類似於++和--的一元運算的操作。

Python也內置了長整型和複數。你可以在這篇文檔里查看更多細節。

Booleans:Python的布爾型邏輯和其他語言都一樣,不過使用了英文單詞替換了符號(||,&&)

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"

Strings: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"

String 對象還有很多常用的方法,例如:

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"

你可以在這篇文章中找到包含string對象所有方法的列表。

Containers

Python包含了幾個內置的容器類型:lists, dictionaries, sets, and tuples

Lists

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

同樣,你可以在這篇文檔中找到關於list的所有細節。

Slicing:除了每次訪問一個列表元素,Python還提供了一種簡潔的語法去訪問子列表,這被稱為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]"

我們將會在numpy arrays章節中再看到slicing.

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,以及JavaScript中的Object。使用方法如下:

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"

Dictionary comprehensions:和推導列表差不多,不過可以更簡單的構建字典。

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

sets是離散元的無序集合。下面舉個簡單的例子

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"

和之前一樣,這篇文章的裡面有sets所有的內容。

Loops:迭代集合的語法和迭代列表的一樣,但是當集合是無序的時候,在便利集合中的元素時不要有排序的預期。

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:和列表以及字典一樣。我們可以很簡單的構建一個集合:

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

Tuples

元組是一個有序列表。在很多方面和列表一樣,有一點非常重要的不同就是元組可以在字典中被用作鍵而列表不行。例子:

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"

這篇文檔有很多關於

Functions

python 中方法的定義使用def關鍵字

def sign(x): if x > 0: return positive elif x < 0: return negative else: return zerofor 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!"

更多關於方法的信息都在這篇文檔中

Classes

定義類的語法在Python中很簡單。

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 classg.greet() # Call an instance method; prints "Hello, Fred"g.greet(loud=True) # Call an instance method; prints "HELLO, FRED!"

在這篇文檔中你可以獲得更多關於Python的類

Numpy

numpy 是Python中科學計算的核心庫。它提供一個高性能多維數據對象,以及操作這個對象的工具。如果你已經熟悉了MATLAB,你會發現本教程對於numpy起步很有用。

Arrays

numpy數組是一個柵格值(譯者註:可以理解為矩陣),所有類型都是一樣的,是一個被索引的非負實數的元祖。數組的維度大小是數組的rank,數組的shape是一個整型的元組,包含元組的大小和有幾個這樣的元組。

我們可以使用嵌套Python列表來初始化一個numpy數組,使用方括弧來訪問元素。

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還提供很多方法來創建array

import numpy as npa = np.zeros((2,2)) # Create an array of all zerosprint a # Prints "[[ 0. 0.] # [ 0. 0.]]" b = np.ones((1,2)) # Create an array of all onesprint b # Prints "[[ 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]]"

其他穿件array的方法你可以看這篇文檔

Array indexing

Numpy提供了多種方法來索引數組。

Slicing:和Python的列表一樣Numpy數組也是可以被切片的。因為數組可能是多維的,所以對數組的每一個元素都要指定slice

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"

你也可以混合整型索引和slice索引。然而這樣做將會得到一個相比原始數組rank更低的數組。

注意這和MATLAB處理數組slicing的方法有很大的不同。

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

整型矩陣索引:當你使用slicing索引numpy矩陣時,結果的矩陣視圖總是源矩陣的子矩陣。相比之下,整數數組索引允許您使用其他矩陣的數據構建任意陣列。例子:

import numpy as npa = 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 exampleprint 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])

print a[np.arange(4), b] # Prints "[ 1 6 7 11]"a[np.arange(4), b] += 10print a # prints "array([[11, 2, 3], # [ 4, 5, 16], # [17, 8, 9], # [10, 21, 12]])

Boolean array indexing:Boolean array indexing可以取得一個矩陣中的任意元素。通常這種索引方式用來選擇符合一定條件的元素。例如:

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 矩陣索引的細節,如果你想知道更多那麼閱讀這篇文檔吧

Datatypes

每一個numpy矩陣都是由具有相同數據類型的元素組成的網格。Numpy提供了一個很大的可以用來構建矩陣的數據類型的集合。

(未完待續)


推薦閱讀:

基於Git的文件自動同步的思考和實現
Flowpython針對lambda的一個修正
左手用R右手Python系列之—表格數據抓取之道
python爬蟲如何斷點繼續抓取?
好玩的Python:植樹問題,平閏年,雞兔同籠,百錢白雞,屬相問題

TAG:Python |