numpy 是一個python 庫,用于 python 編程中的科學計算。在本教程中,你將學習如何在 numpy 數組上以多種方式添加、刪除、排序和操作元素。
numpy 提供了一個多維數組對象和其他派生數組,例如掩碼數組和掩碼多維數組。
為什么要用 numpy
numpy 提供了一個 ndarray 對象,可以使用它來對任何維度的數組進行操作。 ndarray 代表 n 維數組,其中 n 是任意數字。這意味著 numpy 數組可以是任何維度的。
與 python 的 list 相比,numpy 具有許多優勢。我們可以在 numpy 陣列上執行高性能操作,例如:
- 對數組成員進行排序
- 數學和邏輯運算
- 輸入/輸出功能
- 統計和線性代數運算
安裝 numpy
要安裝numpy,你的電腦上要先有 python 和 pip。
在終端中運行以下命令:
1
|
pip install numpy |
然后你就可以在腳本中導入 numpy 了,如下所示:
1
|
import numpy |
添加數組元素
可以用 numpy 模塊的 append() 方法向 numpy 數組中添加元素。
append() 的語法如下:
1
|
numpy.append(array, value, axis) |
value 會被追加到在數組的末尾,并返回一個包含所有元素的 ndarray。
參數 axis 是一個可選的整數,用于定義數組的顯示方式。如果沒有指定,則數組結構將展平,稍后會演示用法。
以下示例,其中首先聲明數組,然后用 append 方法向數組添加更多的值:
1
2
3
4
5
|
import numpy a = numpy.array([ 1 , 2 , 3 ]) newarray = numpy.append (a, [ 10 , 11 , 12 ]) print (newarray) # 輸出:[ 1 2 3 10 11 12] |
添加一列
也可以用numpy 的 append() 方法插入一列。
在下面的例子中,我們創建了一個二維數組并插入了兩列:
1
2
3
4
5
6
7
8
9
10
11
12
|
import numpy a = numpy.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]]) b = numpy.array([[ 400 ], [ 800 ]]) newarray = numpy.append(a, b, axis = 1 ) print (newarray) """ 輸出: [[ 1 2 3 400] [ 4 5 6 800]] """ |
如果沒有使用 axis 參數,則會輸出:
[ 1 2 3 4 5 6 400 800]
這就是數組結構的扁平化。
在 numpy 中,還可以用 insert() 方法插入元素或列。 兩者之間的區別在于 insert() 方法可以指定要在哪個索引處添加元素,但 append() 方法會在數組的末尾添加一個值。
consider the example below:
考慮以下示例:
1
2
3
4
5
|
import numpy a = numpy.array([ 1 , 2 , 3 ]) newarray = numpy.insert(a, 1 , 90 ) print (newarray) # 輸出:[ 1 90 2 3] |
這里 insert() 方法在索引1處添加元素。在python中數組索引從0開始。
追加一行
也可以用 append() 方法向數組添加行,就像在數組中附加元素一樣簡單:
1
2
3
4
5
6
7
8
9
10
|
import numpy a = numpy.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]]) newarray = numpy.append(a, [[ 50 , 60 , 70 ]], axis = 0 ) print (newarray) """ 輸出“ [[ 1 2 3] [ 4 5 6] [50 60 70]] """ |
刪除元素
可以用 numpy 模塊的 delete() 方法刪除 numpy 數組元素:
1
2
3
4
5
|
import numpy a = numpy.array([ 1 , 2 , 3 ]) newarray = numpy.delete(a, 1 , axis = 0 ) print (newarray) # 輸出:[1 3] |
在本例子中,我們有一個一維數組,用 delete() 方法從數組中刪除了索引 1 處的元素。
刪除一行
同樣,你也可以用 delete() 方法刪除行。
下面的例子中我們從二維數組中刪除了一行:
1
2
3
4
5
6
7
8
9
|
import numpy a = numpy.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 10 , 20 , 30 ]]) newarray = numpy.delete(a, 1 , axis = 0 ) print (newarray) """ 輸出: [[ 1 2 3] [10 20 30]] """ |
在 delete() 方法中,首先給出數組,然后給出要刪除的元素的索引。在上例中,我們刪除了索引為 1 的元素。
檢查 numpy 數組是否為空
可以用 size 方法返回數組中元素的總數。
在下面的例子中有一個 if 語句,通過 ndarray.size 檢查數組中是否有元素,其中 ndarray 可以是任何給定的 numpy 數組:
1
2
3
4
5
6
7
8
|
import numpy a = numpy.array([ 1 , 2 , 3 ]) if (a.size = = 0 ): print ( "the given array is empty" ) else : print ( "the array = " , a) # 輸出:the array = [1 2 3] |
在上面的代碼中,數組中有三個元素,因此它不是空的,判斷條件將返回false。如果數組中沒有元素,則 if 條件會變為 true 并且將打印空消息。如果數組等于:
1
|
a = numpy.array([]) |
上述代碼將會輸出:
the given array is empty
查找值的索引
要查找值對應的索引,可以用 numpy 模塊的 where() 方法,如下例所示:
1
2
3
4
|
import numpy a = numpy.array([ 1 , 2 , 3 , 4 , 5 ]) print ( "5 is found at index: " , numpy.where(a = = 5 )) # 輸出:5 is found at index: (array([4]),) |
如果你只想得到索引,可以這樣寫:
1
2
3
4
5
6
|
import numpy a = numpy.array([ 1 , 2 , 3 , 4 , 5 ]) index = numpy.where(a = = 5 ) print ( "5 is found at index: " , index[ 0 ]) #輸出: 5 is found at index: [4] |
numpy 數組切片
數組切片是從給定數組中提取子集的過程。你可以用冒號( : )運算符對數組進行切片,并指定數組索引的開始和結束位置,例如:
1
|
array[ from :to] |
下面的例子中提取從索引 2 到索引 5 的元素:
1
2
3
4
|
import numpy a = numpy.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]) print ( "a subset of array a = " , a[ 2 : 5 ]) # 輸出:a subset of array a = [3 4 5] |
如果想要提取最后三個元素,可以通過用負切片來完成操作,如下所示:
1
2
3
4
|
import numpy a = numpy.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]) print ( "a subset of array a = " , a[ - 3 :]) # 輸出:a subset of array a = [6 7 8] |
將函數作用于所有數組元素
在下面的例子中,我們將創建一個 lambda 函數,并傳入一個數組,以其應用于所有元素:
1
2
3
4
5
|
import numpy addition = lambda x: x + 2 a = numpy.array([ 1 , 2 , 3 , 4 , 5 , 6 ]) print ( "array after addition function: " , addition(a)) # 輸出:array after addition function: [3 4 5 6 7 8] |
在此例中,創建了一個 lambda 函數,它使每個元素都遞增 2。
numpy 數組的長度
要得到 numpy 數組的長度,可以用 size 屬性,如下所示:
1
2
3
4
|
import numpy a = numpy.array([ 1 , 2 , 3 , 4 , 5 , 6 ]) print ( "the size of array = " , a.size) # 輸出:the size of array = 6 |
從 list 創建 numpy 數組
假設你有一個列表:
l = [1, 2, 3, 4, 5]
現在要根據這個列表創建一個數組,可以用 numpy 模塊的 array() 方法:
1
2
3
4
5
|
import numpy l = [ 1 , 2 , 3 , 4 , 5 ] a = numpy.array(l) print ( "the numpy array from python list = " , a) # 輸出:the numpy array from python list = [1 2 3 4 5] |
同樣,使用 array() 方法,也可以從元組創建 numpy 數組。如下所示:
1
2
3
4
5
|
import numpy t = ( 1 , 2 , 3 , 4 , 5 ) a = numpy.array(t) print ( "the numpy array from python tuple = " , a) # 輸出:the numpy array from python tuple = [1 2 3 4 5] |
將 numpy 數組轉換為 list
要將數組轉換為list,可以使用 numpy 模塊的 tolist()方法。
1
2
3
4
|
import numpy a = numpy.array([ 1 , 2 , 3 , 4 , 5 ]) print ( "array to list = " , a.tolist()) # 輸出:array to list = [1, 2, 3, 4, 5] |
在這段代碼中,我們簡單地調用了 tolist() 方法,該方法將數組轉換為列表。然后將新創建的列表打印到輸出屏幕。
把 numpy 數組導出為 csv
要將數組導出為 csv 文件,可以用 numpy 模塊的 savetxt() 方法,如下所示:
1
2
3
|
import numpy a = numpy.array([ 1 , 2 , 3 , 4 , 5 ]) numpy.savetxt( "myarray.csv" , a) |
此代碼將在 python 代碼文件所在路徑下生成 csv 文件。當然你也可以指定路徑。
該文件的內容如下:
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
4.000000000000000000e+00
5.000000000000000000e+00
你可以把額外填充的零刪除,如下所示:
1
|
numpy.savetxt( "myarray.csv" , a,fmt = '%.2f' ) |
對 numpy 數組排序
可以用 numpy 模塊的 sort() 方法對 numpy 數組進行排序:
sort() 函數有一個可選參數 axis(整數),默認為 -1。axis 指定我們要對數組進行排序的軸。 -1 表示將根據最后一個軸對數組進行排序。
1
2
3
4
|
import numpy a = numpy.array([ 16 , 3 , 2 , 6 , 8 , 10 , 1 ]) print ( "sorted array = " , numpy.sort(a)) # 輸出:sorted array = [ 1 2 3 6 8 10 16] |
在這個例子中,我們在 print 語句中調用了 sort() 方法。數組 a 被傳遞給 sort 函數。
歸一化數組
歸一化數組是指將數組的值置于某個定義范圍的過程。例如,我們想要在 -1 和 1 之間對數組進行歸一化,依此類推。
歸一化的公式如下:
x = (x – xmin) / (xmax – xmin)
現在把這個公式用于我們的數組。要查找數組中的最大和最小項,可以分別用 numpy 的 max() 和 min() 方法。
1
2
3
4
5
6
7
8
9
10
11
12
|
import numpy x = numpy.array([ 400 , 800 , 200 , 700 , 1000 , 2000 , 300 ]) xmax = x. max () xmin = x. min () x = (x - xmin) / (xmax - xmin) print ( "after normalization array x = \n" , x) """ 輸出: after normalization array x = [0.11111111 0.33333333 0. 0.27777778 0.44444444 1. 0.05555556] """ |
數組索引
索引指向數組中的一個元素。在下面的例子中,分別用到了一維和二維數組中的索引:
1
2
3
4
|
import numpy a = numpy.array([ 20 , 13 , 42 , 86 , 81 , 9 , 11 ]) print ( "element at index 3 = " , a[ 3 ]) # 輸出:element at index 3 = 86 |
下面是二維數組:
1
2
3
4
|
import numpy a = numpy.array([[ 20 , 13 , 42 ], [ 86 , 81 , 9 ]]) print ( "element at index a[1][2] = " , a[ 1 ][ 2 ]) # 輸出:element at index a[1][2] = 9 |
索引 [1][2] 表示第二行和第三列(索引從 0 開始)。因此在屏幕上輸出 9 。
將 numpy 數組附加到另?一個數組上
可以用 append() 方法將 numpy 數組附加到另??一個 numpy 數組上。
1
2
3
4
5
6
|
import numpy a = numpy.array([ 1 , 2 , 3 , 4 , 5 ]) b = numpy.array([ 10 , 20 , 30 , 40 , 50 ]) newarray = numpy.append(a, b) print ( "the new array = " , newarray) # 輸出:the new array = [ 1 2 3 4 5 10 20 30 40 50] |
在此例中,創建兩個 numpy 數組 a, b 。然后把兩個數組傳給 append()。當數組 b 作為第二個參數傳遞時,將被添加到數組 a 的末尾。
總結
正如大家所見,numpy 數組用起來非常簡單。在使用很多機器學習庫時,numpy 數組非常重要。可以說numpy 是人工智能的大門。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000018975446