清晰易懂的Numpy進階教程
點擊上方“小白學(xué)視覺”,選擇加"星標"或“置頂”
重磅干貨,第一時間送達
Numpy是數(shù)據(jù)分析和科學(xué)計算的核心包,上文詳細介紹了Numpy的入門教程,本文將詳細介紹Numpy的高級特性,這些特性對于數(shù)據(jù)分析和處理非常重要。

1. 如何獲取滿足條設(shè)定件的索引
2. 如何將數(shù)據(jù)導(dǎo)入和導(dǎo)出csv文件
3. 如何保存和加載numpy對象
4. 如何按列或行拼接numpy數(shù)組
5. 如何按列對numpy數(shù)組進行排序
6. 如何用numpy處理日期
7.高階numpy函數(shù)介紹
8. 小結(jié)
上一文介紹根據(jù)數(shù)組是否滿足條件,輸出為True或False.
# 定義數(shù)組
import numpy as np
arr_rand = np.array([8, 8, 3, 7, 7, 0, 4, 2, 5, 2])
#根據(jù)數(shù)組是否大于4,滿足為True,不滿足為False
b = arr_rand > 4
b
#> array([ True, True, False, True, True, False, False, False, True,
False])
若我們想得到滿足條件的索引,用np.where函數(shù)實現(xiàn).
# 定位數(shù)組大于5的索引
index_gt5 = np.where(arr_rand > 5)
print("Positions?where?value?>?5:?", index_gt5)
#>?Positions?where?value?>?5:??(array([0,?1,?3,?4]),)?#?索引
由索引得到滿足條件的值.
# 根據(jù)索引得到滿足條件的數(shù)組值
arr_rand.take(index_gt5)
#> array([[8, 8, 7, 7]])
np.where也可以接受另兩個可選擇的參數(shù)x和y。當條件滿足時,輸出x,反之輸出y.
# 如果值大于5,輸出字符串'gt5',反之輸出'le5'
np.where(arr_rand > 5, 'gt5', 'le5')
#> array(['gt5', 'gt5', 'le5', 'gt5', 'gt5', 'le5', 'le5', 'le5', 'le5', 'le5'],
dtype=')
np.argmax和np.argmin分別獲取數(shù)組最大值和最小值的索引.
# 最大值索引
print('Position?of?max?value:?', np.argmax(arr_rand))
# 最小值索引
print('Position?of?min?value:?', np.argmin(arr_rand))
#>?Position?of?max?value:??0
#>?Position?of?min?value:??5
np.max和np.min分別獲取數(shù)組的最大值和最小值.
# 最大值
print('max?value:?', np.max(arr_rand))
# 最小值
print('min?value:?', np.min(arr_rand))
#>?max?value:??8
#>?min?value:??0
導(dǎo)入數(shù)據(jù)的標準方法是使用np.genfromtxt函數(shù),它可以從web URLs導(dǎo)入數(shù)據(jù),處理缺失值,多種分隔符,處理不規(guī)則的列數(shù)等功能。一個不太通用的版本是用np.loadtxt函數(shù)導(dǎo)入數(shù)據(jù),它假設(shè)數(shù)據(jù)集無缺失值.
作為示例,我們嘗試從下面的URL讀取.csv文件,由于numpy數(shù)組的所有元素都應(yīng)該是同一種類型,因此文本的最后一列默認為'nan'。通過設(shè)置參數(shù)'filling_values',你可以用其他值代替缺失值.
# 關(guān)閉數(shù)字的科學(xué)表示方法
np.set_printoptions(suppress=True)
# 從url的csv文件導(dǎo)入數(shù)據(jù)
path = 'https://raw.githubusercontent.com/selva86/datasets/master/Auto.csv'
# delimiter:分隔符,skip_header:從多少行開始讀數(shù)據(jù),以0開始,filling_values:缺失值表示,dtype:數(shù)據(jù)類型
data = np.genfromtxt(path, delimiter=',', skip_header=1, filling_values=-999, dtype='float')
data[:3] # 顯示前3行數(shù)據(jù)
#> array([[ 18. , 8. , 307. , 130. , 3504. , 12. , 70. ,
#> 1. , -999. ],
#> [ 15. , 8. , 350. , 165. , 3693. , 11.5, 70. ,
#> 1. , -999. ],
#> [ 18. , 8. , 318. , 150. , 3436. , 11. , 70. ,
#> 1. , -999. ]])
若設(shè)置參數(shù)dtype為'object'或'None',np.genfromtxt在未設(shè)置占位符的前提下能同時處理具有數(shù)字和文本列的數(shù)據(jù)集.
# data2 = np.genfromtxt(path, delimiter=',', skip_header=1, dtype='object')
data2 = np.genfromtxt(path, delimiter=',', skip_header=1, dtype=None)
data2[:3] # 顯示前三行
#> array([( 18., 8, 307., 130, 3504, 12. , 70, 1, b'"chevrolet chevelle malibu"'),
#> ( 15., 8, 350., 165, 3693, 11.5, 70, 1, b'"buick skylark 320"'),
#> ( 18., 8, 318., 150, 3436, 11. , 70, 1, b'"plymouth satellite"')],
#> dtype=[('f0', '
最后,'np.savetxt'將數(shù)據(jù)保存為csv文件.
# 保存數(shù)據(jù)為csv文件
np.savetxt("out.csv", data, delimiter=",")
Numpy提供了.npy和.npz文件類型來實現(xiàn)。如果保存一個ndarray數(shù)據(jù),使用np.save保存為.npy文件;若保存多個ndarray數(shù)據(jù),使用np.savez保存為.npz文件。加載numpy數(shù)據(jù),則統(tǒng)一用np.load函數(shù).
# 保存單一的numpy數(shù)據(jù),使用.npy文件
np.save('myarray.npy', arr2d)
# 保存多個numpy數(shù)據(jù),使用.npz文件
np.savez('array.npz', arr2d_f, arr2d_b)
# 加載.npy文件
a = np.load('myarray.npy')
print(a)
#> [[0 1 2]
#> [3 4 5]
#> [6 7 8]]
加載.npz文件,獲取特定的數(shù)組值.
# 加載.npz文件
b = np.load('array.npz')
print(b.files)
b['arr_0']
#> ['arr_0', 'arr_1']
#> array([[ 0., 1., 2.],
#> [ 3., 4., 5.],
#> [ 6., 7., 8.]])
雖然通過'arr_0'和'arr_1'獲取了數(shù)組值,但是我們對這兩個索引比較陌生,下面介紹手動設(shè)置索引保存和加載數(shù)組.
# 增加索引保存數(shù)據(jù)
b=np.savez('array.npz',arr2d_f=arr2d_f,arr2d_b=arr2d_b)
c = np.load('array.npz')
print(c.files)
c['arr2d_f']
#> ['arr2d_f', 'arr2d_b']
#> array([[1., 2., 3., 4.],
[3., 4., 5., 6.],
[5., 6., 7., 8.]])
本節(jié)介紹三種拼接numpy數(shù)組的方法:
方法1:設(shè)置np.concatenate參數(shù)axis的值為1或0,實現(xiàn)數(shù)組的列拼接或行拼接。
方法2:np.vstack和np.hstack
方法3:np.r_和np.c_
需要注意的是,np.r_和np.c_使用方括號來拼接數(shù)組,其他兩種方法使用括號。
首先,定義兩個需要拼接的數(shù)組.
# 定義兩個拼接的數(shù)組
a = np.zeros([4, 4])
b = np.ones([4, 4])
print(a)
print(b)
#> [[ 0. 0. 0. 0.]
#> [ 0. 0. 0. 0.]
#> [ 0. 0. 0. 0.]
#> [ 0. 0. 0. 0.]]
#> [[ 1. 1. 1. 1.]
#> [ 1. 1. 1. 1.]
#> [ 1. 1. 1. 1.]
#> [ 1. 1. 1. 1.]]
行拼接數(shù)組
# 行拼接數(shù)組
np.concatenate([a, b], axis=0)
np.vstack([a,b])
np.r_[a,b]
#> array([[ 0., 0., 0., 0.],
#> [ 0., 0., 0., 0.],
#> [ 0., 0., 0., 0.],
#> [ 0., 0., 0., 0.],
#> [ 1., 1., 1., 1.],
#> [ 1., 1., 1., 1.],
#> [ 1., 1., 1., 1.],
#> [ 1., 1., 1., 1.]])
列拼接數(shù)組
# 列拼接
np.concatenate([a, b], axis=1)
np.hstack([a,b])
np.c_[a,b]
#> array([[ 0., 0., 0., 0., 1., 1., 1., 1.],
#> [ 0., 0., 0., 0., 1., 1., 1., 1.],
#> [ 0., 0., 0., 0., 1., 1., 1., 1.],
#> [ 0., 0., 0., 0., 1., 1., 1., 1.]])
本節(jié)介紹三種按列排序方法:np.sort,np.argsort和np.lexsort。在介紹三種排序方法前,先定義一個2維數(shù)組.
arr = np.random.randint(1,6, size=[8, 4])
arr
#> array([[3, 3, 2, 1],
#> [1, 5, 4, 5],
#> [3, 1, 4, 2],
#> [3, 4, 5, 5],
#> [2, 4, 5, 5],
#> [4, 4, 4, 2],
#> [2, 4, 1, 3],
#> [2, 2, 4, 3]])
np.sort基于列對arr數(shù)組進行排序.
# axis=0表示列排序,1表示行排序
np.sort(arr, axis=0)
#> array([[1, 1, 1, 1],
#> [2, 2, 2, 2],
#> [2, 3, 4, 2],
#> [2, 4, 4, 3],
#> [3, 4, 4, 3],
#> [3, 4, 4, 5],
#> [3, 4, 5, 5],
#> [4, 5, 5, 5]])
由上面結(jié)果分析,np.sort排序函數(shù)認為所有列是相互獨立的,對所有列進行排序,破壞了每行的結(jié)構(gòu),
使用np.argsort函數(shù)可以保留行的完整性 .
# 對arr的第一列進行排序,返回索引
sorted_index_1stcol = arr[:, 0].argsort()
# 根據(jù)第一列的索引對數(shù)組排序,保留了行的完整性
arr[sorted_index_1stcol]
#> array([[1, 5, 4, 5],
#> [2, 4, 5, 5],
#> [2, 4, 1, 3],
#> [2, 2, 4, 3],
#> [3, 3, 2, 1],
#> [3, 1, 4, 2],
#> [3, 4, 5, 5],
#> [4, 4, 4, 2]])
倒轉(zhuǎn)argsort索引實現(xiàn)遞減排序
# 遞減排序
arr[sorted_index_1stcol[::-1]]
#> array([[4, 4, 4, 2],
#> [3, 4, 5, 5],
#> [3, 1, 4, 2],
#> [3, 3, 2, 1],
#> [2, 2, 4, 3],
#> [2, 4, 1, 3],
#> [2, 4, 5, 5],
#> [1, 5, 4, 5]])
若要基于多個列對數(shù)組進行排序,使用np.lexsort函數(shù),它的參數(shù)是元組類型,元組的每個元素表示數(shù)組的某一列,排序規(guī)則是:越靠近右邊的列,優(yōu)先級越高.
# 先比較第一列,第一列相同的情況下再比較第二列
lexsorted_index = np.lexsort((arr[:, 1], arr[:, 0]))
arr[lexsorted_index]
#> array([[1, 5, 4, 5],
#> [2, 2, 4, 3],
#> [2, 4, 5, 5],
#> [2, 4, 1, 3],
#> [3, 1, 4, 2],
#> [3, 3, 2, 1],
#> [3, 4, 5, 5],
#> [4, 4, 4, 2]])
np.datatime64創(chuàng)建日期對象,精確度達到納秒,你可以使用標準的YYYY-MM-DD格格式的字符串作為參數(shù)創(chuàng)建日期.
# 創(chuàng)建datetime64對象
date64 = np.datetime64('2018-02-04?23:10:10')
date64
#>?numpy.datetime64('2018-02-04T23:10:10')
從datatime64對象分離時間
# 從datatime64對象分離時間
dt64 = np.datetime64(date64, 'D')
dt64
#> numpy.datetime64('2018-02-04')
如果你想增加天數(shù)或其他任何時間單元,比如月份,小時,秒等,使用np.timedelta函數(shù)非常方便.
# np.delta建立多個時間單元
tenminutes = np.timedelta64(10, 'm') # 10 分鐘
tenseconds = np.timedelta64(10, 's') # 10 秒鐘
tennanoseconds = np.timedelta64(10, 'ns') # 10 納秒
print('Add?10?days:?', dt64 + 10) # 增加10天
print('Add?10?minutes:?', dt64 + tenminutes) # 增加10分鐘
print('Add?10?seconds:?', dt64 + tenseconds) # 增加10秒
print('Add?10?nanoseconds:?', dt64 + tennanoseconds) # 增加10納秒
#>?Add?10?days:??2018-02-14
#>?Add?10?minutes:??2018-02-04T00:10
#>?Add?10?seconds:??2018-02-04T00:00:10
#>?Add?10?nanoseconds:??2018-02-04T00:00:00.000000010
dt64對象轉(zhuǎn)化為字符串
# dt64轉(zhuǎn)化為字符串
np.datetime_as_string(dt64)
#> '2018-02-04'
np.is_busday函數(shù)判斷日期是否為工作日,工作日默認為周一至周五.
print('Date:?', dt64)
print("Is?it?a?business?day?:?", np.is_busday(dt64))
#>?Date:??2018-02-04
#>?Is?it?a?business?day?:??False????#?False表示不是
手動設(shè)置工作日的時間,如設(shè)置工作日為周六周日,其他時間為休息日.
date64 = np.datetime64('2019-04-14')
# 設(shè)置周六周日為工作日
np.is_busday(date64,weekmask='Sat Sun')
#> True
np.busday_offset查看后幾個工作日的日期.
# 周四
date64 = np.datetime64('2019-04-11')
# 查看兩個工作日后的日期
t = np.busday_offset(date64, 2)
t
#> numpy.datetime64('2019-04-15')
若當前工作日為非工作日,則默認是報錯的.
# 周六
date64 = np.datetime64('2019-04-13')
# 查看兩個工作日后的日期
t = np.busday_offset(date64, 2)
t
#>?ValueError:?Non-business?day?date?in?busday_offset??#?非工作日不能作為busday_offset的參數(shù)
可以增加參數(shù)forward或backward來報錯,forward的含義是若當前日期非工作日,那么往前尋找最接近當前日期的工作日,backward的含義則是往后尋找最接近當前日期的工作日.
#當前時間為周六(2019-04-13),往前最接近當前時間的工作日是2019-04-15,兩個工作日偏移后的日期是2019-04-17
print("Add?2?business?days,?rolling?forward?to?nearest?biz?day:?", np.busday_offset(date64, 2, roll='forward'))
#當前時間為周六(2019-04-13),往后最接近當前時間的工作日是2019-04-12,兩個工作日偏移后的日期是2019-04-16
print("Add?2?business?days,?rolling?backward?to?nearest?biz?day:?", np.busday_offset(date64, 2, roll='backward'))
#>?Add?2?business?days,?rolling?forward?to?nearest?biz?day:??2019-04-17
#>?Add?2?business?days,?rolling?backward?to?nearest?biz?day:??2019-04-16
6.1 如何創(chuàng)建日期序列
np.arange可簡單創(chuàng)建日期序列
# 建立日期序列
dates = np.arange(np.datetime64('2018-02-01'), np.datetime64('2018-02-10'))
print(dates)
# 檢查是否為工作日
np.is_busday(dates)
#> ['2018-02-01' '2018-02-02' '2018-02-03' '2018-02-04' '2018-02-05'
#> '2018-02-06' '2018-02-07' '2018-02-08' '2018-02-09']
array([ True, True, False, False, True, True, True, True, True], dtype=bool)
6.2 如何把numpy.data64對象轉(zhuǎn)化為datatime.datatime對象
# np.datetime64類型轉(zhuǎn)化為datetime.datetime類型
import datetime
dt = dt64.tolist()
dt
#> datetime.date(2018, 2, 4)
獲取datatime對象的年月日非常簡便
print('Year:?', dt.year)
print('Day?of?month:?', dt.day)
print('Month?of?year:?', dt.month)
print('Day?of?Week:?', dt.weekday()) # 周五
#>?Year:??2018
#>?Day?of?month:??4
#>?Month?of?year:??2#>?Day?of?Week:??6
7.1 標量函數(shù)的向量化
標量函數(shù)只能處理標量,不能處理數(shù)組
# 定義標量函數(shù)
def foo(x):
if x % 2 == 1:
return x**2
else:
return x/2
# On a scalar
print('x = 10 returns ', foo(10))
print('x = 11 returns ', foo(11))
#> x = 10 returns 5.0
#> x = 11 returns 121
# 函數(shù)不能處理數(shù)組
# print('x = [10, 11, 12] returns ', foo([10, 11, 12])) # 錯誤
np.vectorize使標量函數(shù)也能處理數(shù)組,可選參數(shù)otypes為輸出的類型.
# 函數(shù)向量化,向量化的輸出類型是float
foo_v = np.vectorize(foo, otypes=[float])
print('x = [10, 11, 12] returns ', foo_v([10, 11, 12]))
print('x = [[10, 11, 12], [1, 2, 3]] returns ', foo_v([[10, 11, 12], [1, 2, 3]]))
#> x = [10, 11, 12] returns [ 5. 121. 6.]
#> x = [[10, 11, 12], [1, 2, 3]] returns [[ 5. 121. 6.]
#> [ 1. 1. 9.]]
7.2 apply_along_axis函數(shù)
首先定義一個二維數(shù)組
# 定義一個4x10的隨機二維數(shù)組
np.random.seed(100)
arr_x = np.random.randint(1,10,size=[4,10])
arr_x
#> array([[9, 9, 4, 8, 8, 1, 5, 3, 6, 3],
#> [3, 3, 2, 1, 9, 5, 1, 7, 3, 5],
#> [2, 6, 4, 5, 5, 4, 8, 2, 2, 8],
#> [8, 1, 3, 4, 3, 6, 9, 2, 1, 8]])
如果我們要找數(shù)組每行或每列的最大值,numpy的一個最大特點是基于向量化操作的,因此我們使用np.apply_along_axis函數(shù)找每行或每列的最大值.
# 基于列操作,找每列的最大值
print('max?of?per?column:?', np.apply_along_axis(np.max, 0, arr=arr_x))
# 基于行操作,找每行的最大值
print('max?of?per?row:?', np.apply_along_axis(np.max, 1, arr=arr_x))
#>?max?of?per?column:??[9?9?4?8?9?6?9?7?6?8]
#>?max?of?per?row:??[9?9?8?9]
7.3 searchsorted函數(shù)
np.searchsorted函數(shù)返回某一變量在有序數(shù)組的位置,在該位置插入變量后數(shù)組仍然是有序的.
# 生成有序數(shù)組
x = np.arange(10)
print('Where?should?5?be?inserted?:?', np.searchsorted(x, 5))
# 若遇到相同大小的數(shù)值,輸入變量放在右邊位置
print('Where?should?5?be?inserted?(right)?:?', np.searchsorted(x, 5, side='right'))
#>?Where?should?5?be?inserted?:??5
#>?Where?should?5?be?inserted?(right)?:??6
7.4 如何增加數(shù)組維度
在不增加任何額外數(shù)據(jù)的前提下,np.newaxis函數(shù)可以增加數(shù)組的維數(shù),newaxis在的位置就是要增加的維度.
# 定義一維數(shù)組
x = np.arange(5)
print('Original?array:?', x)
# 數(shù)組維度為2
print('ndims?of?x:', x.ndim)
# 列維度增加
x_col = x[:, np.newaxis]
print(x_col)
# 數(shù)組維度為2
print('ndims?of?x_col:?', x_col.ndim)
print('x_col?shape:?', x_col.shape)
# 行維度增加
x_row = x[np.newaxis, :]
print(x_row)
print('x_row?shape:?', x_row.shape)
# 數(shù)組維度為2
print('ndims?of?x_row:?', x_row.ndim)
#>?Original?array:??[0?1?2?3?4]
#>?ndims?of?x:?1
#> [[0]
[1]
[2]
[3]
[4]]
#>?ndims?of?x_col:??2
#>?x_col?shape:??(5,?1)
#> [[0 1 2 3 4]]
#>?x_row?shape:??(1,?5)
#>?ndims?of?x_row:??2
7.5 Digitize函數(shù)
np.digitize函數(shù)返回數(shù)組每個元素屬于bin的索引位置.
# 構(gòu)建數(shù)組和bin
x = np.arange(10)
bins = np.array([0, 3, 6, 9])
# 返回bin索引位置
np.digitize(x, bins)
#> array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4])
可視化分析digitize算法原理:

如上圖,根據(jù)變量落在bin的區(qū)間范圍,返回對應(yīng)的索引.
7.7 Clip函數(shù)
np.clip函數(shù)將數(shù)字限制在給定截止范圍內(nèi),所有小于范圍下限的數(shù)被當成下限值,大于范圍上限的數(shù)被當成上限值.
# 限制x的所有元素位于3和8之間
np.clip(x, 3, 8)
#> array([3, 3, 3, 3, 4, 5, 6, 7, 8, 8])
7.8 Histogram函數(shù)和Bincount函數(shù)
np.bincount函數(shù)統(tǒng)計最小值到最大值的個數(shù),最小值為0.
# Bincount例子
x = np.array([1,1,2,3,2,4,4,5,6,6,6])
np.bincount(x) # 0出現(xiàn)0次, 1出現(xiàn)2次, 2出現(xiàn)2次, 3出現(xiàn)1次,4出現(xiàn)2次,5出現(xiàn)1次,6出現(xiàn)3次
#> array([0, 2, 3, 0, 2, 1, 3], dtype=int64)
np.histogram函數(shù)統(tǒng)計數(shù)據(jù)落入bins的區(qū)間,不考慮bins兩側(cè)的區(qū)間(如下圖所示).

x = np.array([1,1,2,3,2,4,4,5,6,6,6])
counts, bins = np.histogram(x, [0, 2, 4, 6, 8])
print('Counts:?', counts)
print('Bins:?', bins)
#>?Counts:??[2?3?3?3]
#>?Bins:??[0?2?4?6?8]
交流群
歡迎加入公眾號讀者群一起和同行交流,目前有SLAM、三維視覺、傳感器、自動駕駛、計算攝影、檢測、分割、識別、醫(yī)學(xué)影像、GAN、算法競賽等微信群(以后會逐漸細分),請掃描下面微信號加群,備注:”昵稱+學(xué)校/公司+研究方向“,例如:”張三?+?上海交大?+?視覺SLAM“。請按照格式備注,否則不予通過。添加成功后會根據(jù)研究方向邀請進入相關(guān)微信群。請勿在群內(nèi)發(fā)送廣告,否則會請出群,謝謝理解~

