22點24分準時推送,第一時間送達
編輯:技術君 | 作者:逐夢er 來源:blog.csdn.net/qq_43328040/article/details/108700665
上一篇:
正文
一.數(shù)組上的迭代
NumPy 包含一個迭代器對象numpy.nditer。它是一個有效的多維迭代器對象,可以用于在數(shù)組上進行迭代。數(shù)組的每個元素可使用 Python 的標準Iterator接口來訪問。
import numpy as np
a = np.arange(0, 60, 5)
a = a.reshape(3, 4)
print(a)
for x in np.nditer(a):
print(x)
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
0
5
10
15
20
25
30
35
40
45
50
55
如果兩個數(shù)組是可廣播的,nditer組合對象能夠同時迭代它們。假設數(shù) 組a具有維度 3X4,并且存在維度為 1X4 的另一個數(shù)組b,則使用以下類型的迭代器(數(shù)組b被廣播到a的大小)。
import numpy as np
a = np.arange(0, 60, 5)
a = a.reshape(3, 4)
print(a)
b = np.array([1, 2, 3, 4], dtype=int)
print(b)
for x, y in np.nditer([a, b]):
print(x, y)
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
[1 2 3 4]
0 1
5 2
10 3
15 4
20 1
25 2
30 3
35 4
40 1
45 2
50 3
55 4
二.數(shù)組形狀修改函數(shù)
1.ndarray.reshape
函數(shù)在不改變數(shù)據(jù)的條件下修改形狀,參數(shù)如下:
ndarray.reshape(arr, newshape, order)
其中:
import numpy as np
a = np.arange(8)
print(a)
b = a.reshape(4, 2)
print(b)
2.ndarray.flat
函數(shù)返回數(shù)組上的一維迭代器,行為類似 Python 內建的迭代器。
import numpy as np
a = np.arange(0, 16, 2).reshape(2, 4)
print(a)
# 返回展開數(shù)組中的下標的對應元素
print(list(a.flat))
[[ 0 2 4 6]
[ 8 10 12 14]]
[0, 2, 4, 6, 8, 10, 12, 14]
3.ndarray.flatten
函數(shù)返回折疊為一維的數(shù)組副本,函數(shù)接受下列參數(shù):
ndarray.flatten(order)其中:order:‘C’ — 按行,‘F’ — 按列,‘A’ — 原順序,‘k’ —元素在內存中的出現(xiàn)順序。
import numpy as np
a = np.arange(8).reshape(2, 4)
print(a)
# default is column-major
print(a.flatten())
print(a.flatten(order='F'))
[[0 1 2 3]
[4 5 6 7]]
[0 1 2 3 4 5 6 7]
[0 4 1 5 2 6 3 7]
三.數(shù)組翻轉操作函數(shù)
1.numpy.transpose
函數(shù)翻轉給定數(shù)組的維度。如果可能的話它會返回一個視圖。函數(shù)接受下列參數(shù):
numpy.transpose(arr, axes)
其中:
arr:要轉置的數(shù)組
axes:整數(shù)的列表,對應維度,通常所有維度都會翻轉。
import numpy as np
a = np.arange(24).reshape(2, 3, 4)
print(a)
b = np.array(np.transpose(a))
print(b)
print(b.shape)
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
[[[ 0 12]
[ 4 16]
[ 8 20]]
[[ 1 13]
[ 5 17]
[ 9 21]]
[[ 2 14]
[ 6 18]
[10 22]]
[[ 3 15]
[ 7 19]
[11 23]]]
(4, 3, 2)
b = np.array(np.transpose(a, (1, 0, 2)))
print(b)
print(b.shape
[[[ 0 1 2 3]
[12 13 14 15]]
[[ 4 5 6 7]
[16 17 18 19]]
[[ 8 9 10 11]
[20 21 22 23]]]
(3, 2, 4)
2. numpy.ndarray.T
該函數(shù)屬于ndarray類,行為類似于numpy.transpose.
import numpy as np
a = np.arange(12).reshape(3, 4)
print(a)
print(a.T)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
3.numpy.swapaxes
函數(shù)交換數(shù)組的兩個軸。這個函數(shù)接受下列參數(shù):
– numpy.swapaxes(arr, axis1, axis2)
– 參數(shù):
arr:要交換其軸的輸入數(shù)組
axis1:對應第一個軸的整數(shù)
axis2:對應第二個軸的整數(shù)
import numpy as np
a = np.arange(8).reshape(2, 2, 2)
print(a)
print(np.swapaxes(a, 2, 0))
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
[[[0 4]
[2 6]]
[[1 5]
[3 7]]]
4.numpy.rollaxis
s 函數(shù)向后滾動特定的軸,直到一個特定位置。這個函數(shù)
接受三個參數(shù):
– numpy.rollaxis(arr, axis, start)
– 其中:
arr:輸入數(shù)組
axis:要向后滾動的軸,其它軸的相對位置不會改變
start:默認為零,表示完整的滾動。會滾動到特定位置。
import numpy as np
a = np.arange(8).reshape(2,2,2)
print(a)
print(np.rollaxis(a,2))
print(np.rollaxis(a,2,1))
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
[[[0 2]
[4 6]]
[[1 3]
[5 7]]]
[[[0 2]
[1 3]]
[[4 6]
[5 7]]]
四.數(shù)組修改維度函數(shù)
1.numpy.broadcast_to
函數(shù)將數(shù)組廣播到新形狀。它在原始數(shù)組上返回只 讀視圖。它通常不連續(xù)。如果新形狀不符合 NumPy 的廣播規(guī)則,該函數(shù)可能會拋出ValueError。該函數(shù)接受以下參數(shù):
– numpy.broadcast_to(array, shape, subok)
import numpy as np
a = np.arange(4).reshape(1,4)
print(a)
print(np.broadcast_to(a,(4,4)))
[[0 1 2 3]]
[[0 1 2 3]
[0 1 2 3]
[0 1 2 3]
[0 1 2 3]]
2.numpy.expand_dims
函數(shù)通過在指定位置插入新的軸來擴展數(shù)組形狀。該函數(shù)需要兩個參數(shù):
– numpy.expand_dims(arr, axis)
– 其中:
arr:輸入數(shù)組
axis:新軸插入的位置
import numpy as np
x = np.array(([1, 2], [3, 4]))
print(x)
y = np.expand_dims(x, axis=0)
print(y)
print(x.shape, y.shape)
y = np.expand_dims(x, axis=1)
print(y)
print(x.ndim, y.ndim)
print(x.shape, y.shape)
[[1 2]
[3 4]]
[[[1 2]
[3 4]]]
(2, 2) (1, 2, 2)
[[[1 2]]
[[3 4]]]
2 3
(2, 2) (2, 1, 2)
3.numpy.squeeze
函數(shù)從給定數(shù)組的形狀中刪除一維條目。此函數(shù)需要兩 個參數(shù)。
– numpy.squeeze(arr, axis)
– 其中:
arr:輸入數(shù)組
axis:整數(shù)或整數(shù)元組,用于選擇形狀中單一維度條目的子集
import numpy as np
x = np.arange(9).reshape(1, 3, 3)
print(x)
y = np.squeeze(x)
print(y)
print(x.shape, y.shape)
[[[0 1 2]
[3 4 5]
[6 7 8]]]
[[0 1 2]
[3 4 5]
[6 7 8]]
(1, 3, 3) (3, 3)
五.數(shù)組的連接操作
NumPy中數(shù)組的連接函數(shù)主要有如下四個:
concatenate 沿著現(xiàn)存的軸連接數(shù)據(jù)序列
stack 沿著新軸連接數(shù)組序列
hstack 水平堆疊序列中的數(shù)組(列方向)
vstack 豎直堆疊序列中的數(shù)組(行方向)
1.numpy.stack
函數(shù)沿新軸連接數(shù)組序列,需要提供以下參數(shù):
– numpy.stack(arrays, axis)
– 其中:
arrays:相同形狀的數(shù)組序列
axis:返回數(shù)組中的軸,輸入數(shù)組沿著它來堆疊
import numpy as np
a = np.array([[1,2],[3,4]])
print(a)
b = np.array([[5,6],[7,8]])
print(b)
print(np.stack((a,b),0))
print(np.stack((a,b),1))
[[1 2]
[3 4]]
[[5 6]
[7 8]]
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
[[[1 2]
[5 6]]
[[3 4]
[7 8]]]
2.numpy.hstack
是numpy.stack函數(shù)的變體,通過堆疊來生成水平的單個數(shù)組。
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a)
b = np.array([[5, 6], [7, 8]])
print(b)
print('水平堆疊:')
c = np.hstack((a, b))
print(c)
[[1 2]
[3 4]]
[[5 6]
[7 8]]
水平堆疊:
[[1 2 5 6]
[3 4 7 8]]
3.numpy.vstack
是numpy.stack函數(shù)的變體,通過堆疊來生成豎直的單個數(shù)組。
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a)
b = np.array([[5, 6], [7, 8]])
print(b)
print('豎直堆疊:')
c = np.vstack((a, b))
print(c)
[[1 2]
[3 4]]
[[5 6]
[7 8]]
豎直堆疊:
[[1 2]
[3 4]
[5 6]
[7 8]]
4.numpy.concatenate
函數(shù)用于沿指定軸連接相同形狀的兩個或多個數(shù)組。
該函數(shù)接受以下參數(shù)。
– numpy.concatenate((a1, a2, …), axis)
– 其中:
a1, a2, ...:相同類型的數(shù)組序列
axis:沿著它連接數(shù)組的軸,默認為 0
import numpy as np
a = np.array([[1,2],[3,4]])
print(a)
b = np.array([[5,6],[7,8]])
print(b)
print(np.concatenate((a,b)))
print(np.concatenate((a,b),axis = 1))
[[1 2]
[3 4]]
[[5 6]
[7 8]]
[[1 2]
[3 4]
[5 6]
[7 8]]
[[1 2 5 6]
[3 4 7 8]]
六.數(shù)組的分割操作
NumPy中數(shù)組的數(shù)組分割函數(shù)主要如下:
– split 將一個數(shù)組分割為多個子數(shù)組
– hsplit 將一個數(shù)組水平分割為多個子數(shù)組(按列)
– vsplit 將一個數(shù)組豎直分割為多個子數(shù)組(按行)
1.numpy.split
該函數(shù)沿特定的軸將數(shù)組分割為子數(shù)組。函數(shù)接受三個參數(shù):
– numpy.split(ary, indices_or_sections, axis)
ary:被分割的輸入數(shù)組
indices_or_sections:可以是整數(shù),表明要從輸入數(shù)組創(chuàng)建的,等大小的子數(shù)組的數(shù)量。如果此參數(shù)是一維數(shù)組,則其元素表明要創(chuàng)建新子數(shù)組的點。
axis:默認為 0
import numpy as np
a = np.arange(9)
print(a)
print('將數(shù)組分為三個大小相等的子數(shù)組:')
b = np.split(a,3)
print(b)
print('將數(shù)組在一維數(shù)組中表明的位置分割:')
b = np.split(a,[4,7])
print(b)
2.numpy.hsplit
split()函數(shù)的特例,其中軸為 1 表示水平分割。
import numpy as np
a = np.arange(16).reshape(4,4)
print(a)
print('水平分割:')
b = np.hsplit(a,2)
print(b)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
水平分割:
[array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]), array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])]
3.numpy.vsplit
split()函數(shù)的特例,其中軸為 0 表示豎直分割,無論輸入數(shù)組的維度是什么。
import numpy as np
a = np.arange(16).reshape(4,4)
print(a)
print('豎直分割:')
b = np.vsplit(a,2)
print(b)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
豎直分割:
[array([[0, 1, 2, 3],
[4, 5, 6, 7]]), array([[ 8, 9, 10, 11],
[12, 13, 14, 15]])]
七.數(shù)組元素操作
NumPy中數(shù)組操作函數(shù)主要如下:
– resize 返回指定形狀的新數(shù)組
– append 將值添加到數(shù)組末尾
– insert 沿指定軸將值插入到指定下標之前
– delete 返回刪掉某個軸的子數(shù)組的新數(shù)組
– unique 尋找數(shù)組內的唯一元素
1.numpy.resize
函數(shù)返回指定大小的新數(shù)組。如果新大小大于原始大小,則包含原始數(shù)組中的元素的重復副本。如果小于則去掉原始數(shù)組的部分數(shù)據(jù)。該函數(shù)接受以下參數(shù):
– numpy.resize(arr, shape)
– 其中:
arr:要修改大小的輸入數(shù)組
shape:返回數(shù)組的新形狀
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print(a)
print(a.shape)
b = np.resize(a, (3,2))
print(b)
print(b.shape)
print('修改第二個數(shù)組的大?。?#39;)
b = np.resize(a,(3,3))
print(b)
print('修改第三個數(shù)組的大?。?#39;)
b = np.resize(a,(2,2))
print(b)
[[1 2 3]
[4 5 6]]
(2, 3)
[[1 2]
[3 4]
[5 6]]
(3, 2)
修改第二個數(shù)組的大?。?/code>
[[1 2 3]
[4 5 6]
[1 2 3]]
修改第三個數(shù)組的大?。?/code>
[[1 2]
[3 4]]
2.numpy.append
函數(shù)在輸入數(shù)組的末尾添加值。附加操作不是原地的,而是分配新的數(shù)組。此外,輸入數(shù)組的維度必須匹配否則將生成ValueError。函數(shù)接受下列函數(shù):
– numpy.append(arr, values, axis)
– 其中:
arr:輸入數(shù)組
values:要向arr添加的值,比如和arr形狀相同(除了要添加的軸)
axis:沿著它完成操作的軸。如果沒有提供,兩個參數(shù)都會被展開。
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print(a)
print(np.append(a, [[7,8,9]],axis = 0))
print(np.append(a, [[5,5,5],[7,8,9]],axis = 1))
[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 2 3 5 5 5]
[4 5 6 7 8 9]]
3.numpy.insert
函數(shù)在給定索引之前,沿給定軸在輸入數(shù)組中插入值。如果值的類型轉換為要插入,則它與輸入數(shù)組不同。插入沒有原地的,函數(shù)會返回一個新數(shù)組。此外,如果未提供軸,則輸入數(shù)組會被展開。
insert()函數(shù)接受以下參數(shù):
– numpy.insert(arr, obj, values, axis)
arr:輸入數(shù)組
obj:在其之前插入值的索引
values:要插入的值
axis:沿著它插入的軸
import numpy as np
a = np.array([[1,2],[3,4],[5,6]])
print(a)
print(np.insert(a,3,[11,12]))
print(np.insert(a,1,[11],axis = 0))
print(np.insert(a,1,[11],axis = 1))
[[1 2]
[3 4]
[5 6]]
[ 1 2 3 11 12 4 5 6]
[[ 1 2]
[11 11]
[ 3 4]
[ 5 6]]
[[ 1 11 2]
[ 3 11 4]
[ 5 11 6]]
4.numpy.delete
函數(shù)返回從輸入數(shù)組中刪除指定子數(shù)組的新數(shù)組。與insert()函數(shù)的情況一樣,如果未提供軸參數(shù),則輸入數(shù)組將展開。該函 數(shù)接受以下參數(shù):
– Numpy.delete(arr, obj, axis)
arr:輸入數(shù)組
obj:可以被切片,整數(shù)或者整數(shù)數(shù)組,表明要從輸入數(shù)組刪除的子數(shù)組
axis:沿著它刪除給定子數(shù)組的軸
import numpy as np
a = np.array([[1,2],[3,4],[5,6]])
print(a)
print(np.delete(a,5))
print(np.delete(a,1,axis = 1))
[[1 2]
[3 4]
[5 6]]
[1 2 3 4 5]
[[1]
[3]
[5]]
5.numpy.unique
函數(shù)返回輸入數(shù)組中的去重元素數(shù)組。該函數(shù)能夠返回一個元組,包含去重數(shù)組和相關索引的數(shù)組。索引的性質取決于函數(shù)調用中返回參數(shù)的類型。
– numpy.unique(arr, return_index, return_inverse, return_counts)
arr:輸入數(shù)組,如果不是一維數(shù)組則會展開
return_index:如果為true,返回輸入數(shù)組中的元素下標
return_inverse:如果為true,返回去重數(shù)組的下標,它可以用于重構輸入數(shù)組
return_counts:如果為true,返回去重數(shù)組中的元素在原數(shù)組中的出現(xiàn)次數(shù)
import numpy as np
a = np.array([5,2,6,2,7,5,6,8,2,9])
u = np.unique(a)
print(u)
u,indices = np.unique(a, return_index = True)
print(u, indices)
u,indices = np.unique(a,return_inverse = True)
print(u, indices)
u,indices = np.unique(a,return_counts = True)
print(u, indices)
[2 5 6 7 8 9]
[2 5 6 7 8 9] [1 0 2 4 7 9]
[2 5 6 7 8 9] [1 0 2 0 3 1 2 4 0 5]
[2 5 6 7 8 9] [3 2 2 1 1 1]
八.NumPy - 字符串函數(shù)
以下函數(shù)用于對dtype為numpy.string_或numpy.unicode_的數(shù)組執(zhí)行向量 化字符串操作。它們基于 Python 內置庫中的標準字符串函數(shù)。字符數(shù)組類(numpy.char)中定義
import numpy as np
print(np.char.add(['hello'],[' xyz']))
print(np.char.add(['hello', 'hi'],[' abc', ' xyz']))
print(np.char.multiply('Hello ',3))
print(np.char.center('hello', 20,fillchar = '*'))
print(np.char.capitalize('hello world'))
print(np.char.title('hello how are you?'))
print(np.char.lower(['HELLO','WORLD']))
print(np.char.lower('HELLO'))
print(np.char.upper('hello'))
print(np.char.upper(['hello','world']))
print(np.char.split ('hello how are you?'))
print(np.char.split ('YiibaiPoint,Hyderabad,Telangana', sep = ','))
print(np.char.splitlines('hello\nhow are you?'))
print(np.char.splitlines('hello\rhow are you?'))
print(np.char.strip('ashok arora','a'))
print(np.char.strip(['arora','admin','java'],'a'))
print(np.char.join(':','dmy'))
print(np.char.join([':','-'],['dmy','ymd']))
print(np.char.replace ('He is a good boy', 'is', 'was'))
a = np.char.encode('hello', 'cp500')
print(a)
print(np.char.decode(a,'cp500'))
['hello xyz']
['hello abc' 'hi xyz']
Hello Hello Hello
*******hello********
Hello world
Hello How Are You?
['hello' 'world']
hello
HELLO
['HELLO' 'WORLD']
['hello', 'how', 'are', 'you?']
['YiibaiPoint', 'Hyderabad', 'Telangana']
['hello', 'how are you?']
['hello', 'how are you?']
shok aror
['ror' 'dmin' 'jav']
d:m:y
['d:m:y' 'y-m-d']
He was a good boy
b'\x88\x85\x93\x93\x96'
hello
九.NumPy - 算數(shù)函數(shù)
NumPy 包含大量的各種數(shù)學運算功能。NumPy 提供標準的三角函數(shù),算術運算的函數(shù),復數(shù)處理函數(shù)等。
– 三角函數(shù)
– 舍入函數(shù)
– 算數(shù)函數(shù)
1. NumPy -三角函數(shù)
NumPy 擁有標準的三角函數(shù),它為弧度制單位的給定角度返回三角函
數(shù)比值。arcsin,arccos,和arctan函數(shù)返回給定角度的sin,cos和tan的反
三角函數(shù)。這些函數(shù)的結果可以通過numpy.degrees()函數(shù)通過將弧度制 轉換為角度制來驗證。
import numpy as np
a = np.array([0,30,45,60,90])
# 通過乘 pi/180 轉化為弧度
print(np.sin(a*np.pi/180))
print(np.cos(a*np.pi/180))
print(np.tan(a*np.pi/180))
[ 0. 0.5 0.70710678 0.8660254 1. ]
[ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
6.12323400e-17]
[ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
1.63312394e+16]
2.NumPy -舍入函數(shù)
numpy.around()這個函數(shù)返回四舍五入到所需精度的值
– numpy.around(a,decimals) – a 輸入數(shù)組
– decimals 要舍入的小數(shù)位數(shù)。 默認值為0。 如果為負,整數(shù)將四舍五入到小數(shù)點左側的位置
numpy.floor() 函數(shù)返回不大于輸入?yún)?shù)的最大整數(shù)。
numpy.ceil() 函數(shù)返回輸入值的上限,大于輸入?yún)?shù)的最小整數(shù)。
import numpy as np
a = np.array([1.0, 5.55, 123, 0.567, 25.532])
print(np.around(a))
print(np.around(a, decimals=1))
print(np.floor(a))
print(np.ceil(a))
[ 1. 6. 123. 1. 26.]
[ 1. 5.6 123. 0.6 25.5]
[ 1. 5. 123. 0. 25.]
[ 1. 6. 123. 1. 26.]
3.NumPy - 算數(shù)運算
用于執(zhí)行算術運算(如add(),subtract(),multiply()和divide())的輸入數(shù)組必須具有相同的形狀或符合數(shù)組廣播規(guī)則。
– numpy.reciprocal() 函數(shù)返回參數(shù)逐元素的倒數(shù)。
– numpy.power() 函數(shù)將第一個輸入數(shù)組中的元素作為底數(shù),計算它與第二個輸入數(shù)組中相應元素的冪。
– numpy.mod() 函數(shù)返回輸入數(shù)組中相應元素的除法余數(shù)。
import numpy as np
a = np.array([0.25, 2, 1, 0.2, 100])
print(np.reciprocal(a))
print(np.power(a,2))
a = np.array([10,20,30])
b = np.array([3,5,7])
print(np.mod(a,b))
[ 4. 0.5 1. 5. 0.01]
[ 6.25000000e-02 4.00000000e+00 1.00000000e+00 4.00000000e-02
1.00000000e+04]
[1 0 2]
4.NumPy - 統(tǒng)計函數(shù)
NumPy 有很多有用的統(tǒng)計函數(shù),用于從數(shù)組中給定的元素中查找最小,最大,百分標準差和方差等。
– numpy.amin() , numpy.amax() 從給定數(shù)組中的元素沿指定軸返回最小值和最大值。
– numpy.ptp() 函數(shù)返回沿軸的值的范圍(最大值 - 最小值)。
– numpy.percentile() 表示小于這個值得觀察值占某個百分比
numpy.percentile(a, q, axis)
a 輸入數(shù)組;q 要計算的百分位數(shù),在 0 ~ 100 之間;axis 沿著它計算百分位數(shù)的軸
– numpy.median() 返回數(shù)據(jù)樣本的中位數(shù)。
– numpy.mean() 沿軸返回數(shù)組中元素的算術平均值。
– numpy.average() 返回由每個分量乘以反映其重要性的因子得到的加權平均值
import numpy as np
a = np.array([[3,7,5],[8,4,3],[2,4,9]])
print(np.amin(a,1))
print(np.amax(a,1))
print(np.ptp(a))
print(np.percentile(a,50))
print(np.median(a))
print(np.mean(a))
print(np.average(a))
print(np.std([1,2,3,4])) #返回數(shù)組標準差
print(np.var([1,2,3,4])) #返回數(shù)組方差
[3 3 2]
[7 8 9]
7
4.0
4.0
5.0
5.0
1.11803398875
1.25
十.NumPy排序、搜索和計數(shù)函數(shù)
NumPy中提供了各種排序相關功能。
– numpy.sort函數(shù)返回輸入數(shù)組的排序副本。numpy.sort(a, axis, kind, order)
a 要排序的數(shù)組;
axis 沿著它排序數(shù)組的軸,如果沒有數(shù)組會被展開,沿著最后的軸排序; kind 默認為'quicksort'(快速排序);
order 如果數(shù)組包含字段,則是要排序的字段
– numpy.argsort() 函數(shù)對輸入數(shù)組沿給定軸執(zhí)行間接排序,并使用指定排序類型返回數(shù)據(jù)的索引數(shù)組。這個索引數(shù)組用于構造排序后的數(shù)組。
– numpy.lexsort()函數(shù)使用鍵序列執(zhí)行間接排序。鍵可以看作是電子表格中的一列。該函數(shù)返回一個索引數(shù)組,使用它可以獲得排序數(shù)據(jù)。注意,最后一個鍵恰好是 sort 的主鍵。
– numpy.argmax() 和 numpy.argmin()這兩個函數(shù)分別沿給定軸返回最大和最小元素的索引。
– numpy.nonzero() 函數(shù)返回輸入數(shù)組中非零元素的索引。
– numpy.where() 函數(shù)返回輸入數(shù)組中滿足給定條件的元素的索引。
– numpy.extract() 函數(shù)返回滿足任何條件的元素。
import numpy as np
a = np.array([[3, 7, 3, 1], [9, 7, 8, 7]])
print(np.sort(a))
print(np.argsort(a))
print(np.argmax(a))
print(np.argmin(a))
print(np.nonzero(a))
print(np.where(a > 3))
nm = ('raju', 'anil', 'ravi', 'amar')
dv = ('f.y.', 's.y.', 's.y.', 'f.y.')
print(np.lexsort((dv, nm)))
[[1 3 3 7]
[7 7 8 9]]
[[3 0 2 1]
[1 3 2 0]]
4
3
(array([0, 0, 0, 0, 1, 1, 1, 1], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))
(array([0, 1, 1, 1, 1], dtype=int64), array([1, 0, 1, 2, 3], dtype=int64))
[3 1 0 2]
十一.NumPy IO文件操作
ndarray對象可以保存到磁盤文件并從磁盤文件加載。可用的 IO 功能有:
– numpy.save() 文件將輸入數(shù)組存儲在具有npy擴展名的磁盤文件中。
– numpy.load() 從npy文件中重建數(shù)組。
– numpy.savetxt()和numpy.loadtxt() 函數(shù)以簡單文本文件格式存儲和獲取數(shù)組數(shù)據(jù)。
import numpy as np
a = np.array([1,2,3,4,5])
np.save('outfile',a)
b = np.load('outfile.npy')
print(b)
a = np.array([1,2,3,4,5])
np.savetxt('out.txt',a)
b = np.loadtxt('out.txt')
print(b)
[1 2 3 4 5]
[ 1. 2. 3. 4. 5.]