1. 程式人生 > 其它 >Python之numpy進階版(一)

Python之numpy進階版(一)

技術標籤:Pythonpythonnumpy

Python之numpy進階版(一)

本文需要有numpy的基礎知識儲備

一.廣播法則

廣播法則能使通用函式有意義地處理不具有相同形狀的輸入。

  1. 廣播第一法則:如果所有的輸入陣列維度不都相同,一個“1”將被重複地新增在維度較小的陣列上直至所有的陣列擁有一樣的維度。
  2. 廣播第二法則:確定長度為1的陣列沿著特殊的方向表現地好像它有沿著那個方向最大形狀的大小。對陣列來說,沿著那個維度的陣列元素的值理應相同。

應用廣播法則之後,所有陣列的大小必須匹配。

二.花哨的索引和索引技巧

Numpy比普通Python序列提供更多的索引功能。除了索引整數和切片,正如我們之前看到的,陣列可以被整數陣列和布林陣列索引。

(一) 通過陣列索引

1、處理一維array

◆ 一維顯示
from numpy import *
a = arange(12)*2                   # the first 12 multiply numbers
i = array( [ 0,1,3,8,5 ] )         # an array of indices
print(a)                           #輸出原陣列
print(a[i])

執行結果
在這裡插入圖片描述

◆ 二維顯示
j = array( [ [ 3, 4], [ 9, 7 ] ] )         # a bidimensional array of indices
print(a[j]) # the same shape as j

執行結果
在這裡插入圖片描述

◆ 多維顯示
palette = array( [ [0,0,0],                # 黑色
                   [255,0,0],              # 紅色
                   [0,255,0],              # 綠色
                   [0,0,255],              # 藍色
                   [255,255,255
] ] ) # 白色 image = array( [ [ 0, 1, 2, 0 ], # each value corresponds to a color in the palette [ 0, 3, 4, 0 ] ] ) print(palette[image])

執行結果
在這裡插入圖片描述

2、處理多維array

◆ 情形1
from numpy import *
a = arange(12).reshape(3,4)
print(a)
i = array( [ [0,1],          # indices for the first dim of a
             [1,2] ] )
j = array( [ [2,1],          # indices for the second dim
             [3,3] ] )
print(a[i,2])

執行結果
在這裡插入圖片描述
解釋說明 (自行理解)
對於a[i,2]情況,依次輸出
第一行:(0,2)(1,2)
第二行: (1,2) (2,2)

◆ 情形2
print(a)
print(a[i,j])                # i and j must have equal shape

執行結果
在這裡插入圖片描述
解釋說明 (自行理解)
對於a[i,j]情況,依次輸出
第一行:(0,2)(1,1)
第二行: (1,3) (2,3)

◆ 情形3
print(a)
print(a[:,j])

執行結果
解釋說明(自行理解)
a為3*4所以a[:,j]是一個三維陣列(一個二維陣列組成的一維陣列)
第一個元素:第一行:(0,2)(0,1)
第二行:(0,3)(0,3)
第二個元素:第一行:(1,2)(1,1)
第二行:(1,3)(1,3)
第三個元素:第一行:(2,2)(2,1)
第二行:(2,3)(2,3)

3、搜尋時間序列最大值

◆ 情形1
from numpy import *
time = linspace(20, 145, 5)                 # time scale
print(time)

執行結果
在這裡插入圖片描述

◆ 情形2
data = sin(arange(20)).reshape(5,4)         # 4 time-dependent series
print(data)

執行結果
在這裡插入圖片描述

◆ 情形3
incl = data.argmax(axis=0)                   # index of the maxima for each column
print(incl)

inrw = data.argmax(axis=1)                   # index of the maxima for each row
print(inrw)

執行結果
在這裡插入圖片描述

4、作為目標來賦值

◆ 情形1
from numpy import *
a = arange(5)
print(a)
a[[1,3,4]] = 0
print(a)

執行結果
在這裡插入圖片描述
注:當一個索引列表包含重複時,賦值被多次完成,保留最後的值
注:如果你想用Python的+=結構,可能結果並非你所期望

a = arange(5)
a[[0,0,2]]+=1
print(a)

執行結果
在這裡插入圖片描述
解釋說明
即使0在索引列表中出現兩次,索引為0的元素僅僅增加一次。這是因為Python要求a+=1和a=a+1等同。

(二) 通過布林陣列索引

當我們使用整數陣列索引陣列時,我們提供一個索引列表去選擇。通過布林陣列索引的方法是不同的我們顯式地選擇陣列中我們想要和不想要的元素。
我們能想到的使用布林陣列的索引最自然方式就是使用和原陣列一樣形狀的布林陣列。

1、通過布林陣列所有選擇想要和不想要的元素

◆ 情形一
from numpy import *
a = arange(12).reshape(3,4)
b = a > 4
print(b)       # b is a boolean with a's shape
print(a[b])

執行結果
在這裡插入圖片描述

◆ 情形2
a[b]=0
print(a)

執行結果
在這裡插入圖片描述

2、通過布林陣列切片

◆ 情形1
a = arange(12).reshape(3,4)
b1 = array([False,True,True])             # first dim selection
b2 = array([True,False,True,False])       # second dim selection
print(a[b1,:])                   # selecting rows
print(a[b1])
print(a[:,b2])					 # selecting columns

注:a[b1,:]與a[b1]相同
執行結果
在這裡插入圖片描述
注:一維陣列的長度必須和你想要切片的維度或軸的長度一致,在之前的例子中,b1是一個秩為1長度為三的陣列(a的行數),b2(長度為4)與a的第二秩(列)相一致。

結論:自學自用,希望可以喝大家積極溝通交流,如有錯誤還請指正,不喜勿噴