到目前為止蛮原,我們了解了如何使用索引進(jìn)行切片以及選擇 ndarray 元素。當(dāng)我們知道要選擇的元素的確切索引時(shí)另绩,這些方法很有用儒陨。但是,在很多情況下笋籽,我們不知道要選擇的元素的索引蹦漠。例如,假設(shè)有一個(gè) 10,000 x 10,000 ndarray车海,其中包含從 1 到 15,000 的隨機(jī)整數(shù)津辩,我們只想選擇小于 20 的整數(shù)。這時(shí)候就要用到布爾型索引,對(duì)于布爾型索引喘沿,我們將使用邏輯參數(shù)(而不是確切的索引)選擇元素闸度。我們來看一些示例:
# We create a 5 x 5 ndarray that contains integers from 0 to 24
X = np.arange(25).reshape(5, 5)
# We print X
print()
print('Original X = \n', X)
print()
# We use Boolean indexing to select elements in X:
print('The elements in X that are greater than 10:', X[X > 10])
print('The elements in X that lees than or equal to 7:', X[X <= 7])
print('The elements in X that are between 10 and 17:', X[(X > 10) & (X < 17)])
# We use Boolean indexing to assign the elements that are between 10 and 17 the value of -1
X[(X > 10) & (X < 17)] = -1
# We print X
print()
print('X = \n', X)
print()
Original X =
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
The elements in X that are greater than 10: [11 12 13 14 15 16 17 18 19 20 21 22 23 24]
The elements in X that lees than or equal to 7: [0 1 2 3 4 5 6 7]
The elements in X that are between 10 and 17: [11 12 13 14 15 16]
X =
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 -1 -1 -1 -1]
[-1 -1 17 18 19]
[20 21 22 23 24]]
除了布爾型索引之外,NumPy 還允許進(jìn)行集合運(yùn)算蚜印≥航可以用來比較 ndarray,例如查找兩個(gè) ndarray 中的相同元素窄赋。我們來看一些示例:
# We create a rank 1 ndarray
x = np.array([1,2,3,4,5])
# We create a rank 1 ndarray
y = np.array([6,7,2,8,4])
# We print x
print()
print('x = ', x)
# We print y
print()
print('y = ', y)
# We use set operations to compare x and y:
print()
print('The elements that are both in x and y:', np.intersect1d(x,y))
print('The elements that are in x that are not in y:', np.setdiff1d(x,y))
print('All the elements of x and y:',np.union1d(x,y))
x = [1 2 3 4 5]
y = [6 7 2 8 4]
The elements that are both in x and y: [2 4]
The elements that are in x that are not in y: [1 3 5]
All the elements of x and y: [1 2 3 4 5 6 7 8]
我們還可以在 NumPy 中對(duì) ndarray 進(jìn)行排序哟冬。我們將了解如何使用 np.sort()
函數(shù)以不同的方式對(duì)秩為 1 和 2 的 ndarray 進(jìn)行排序。和我們之前看到的其他函數(shù)一樣忆绰,sort 函數(shù)也可以當(dāng)做方法使用浩峡。但是,對(duì)于此函數(shù)來說错敢,數(shù)據(jù)在內(nèi)存中的存儲(chǔ)方式有很大變化翰灾。當(dāng) np.sort()
當(dāng)做函數(shù)使用時(shí),它不會(huì)對(duì)ndarray進(jìn)行就地排序稚茅,即不更改被排序的原始 ndarray
纸淮。但是,如果將 sort
當(dāng)做方法
亚享,ndarray.sort()
會(huì)就地排序
ndarray咽块,即原始數(shù)組會(huì)變成排序后的數(shù)組。我們來看一些示例:
# We create an unsorted rank 1 ndarray
x = np.random.randint(1,11,size=(10,))
# We print x
print()
print('Original x = ', x)
# We sort x and print the sorted array using sort as a function.
print()
print('Sorted x (out of place):', np.sort(x))
# When we sort out of place the original array remains intact. To see this we print x again
print()
print('x after sorting:', x)
Original x = [9 6 4 4 9 4 8 4 4 7]
Sorted x (out of place): [4 4 4 4 4 6 7 8 9 9]
x after sorting: [9 6 4 4 9 4 8 4 4 7]
注意欺税,np.sort()
會(huì)對(duì)數(shù)組進(jìn)行排序侈沪,但是如果被排序的 ndarray 具有重復(fù)的值,np.sort()
將在排好序的數(shù)組中保留這些值晚凿。但是亭罪,我們可以根據(jù)需要,同時(shí)使用 sort
函數(shù)和 unique
函數(shù)僅對(duì) x 中的唯一元素進(jìn)行排序晃虫。我們來看看如何對(duì)上述 x 中的唯一元素進(jìn)行排序:
# We sort x but only keep the unique elements in x
print(np.sort(np.unique(x)))
[4 6 7 8 9]
最后皆撩,我們來看看如何將 sort 當(dāng)做方法,原地對(duì) ndarray 進(jìn)行排序:
# We create an unsorted rank 1 ndarray
x = np.random.randint(1,11,size=(10,))
# We print x
print()
print('Original x = ', x)
# We sort x and print the sorted array using sort as a method.
x.sort()
# When we sort in place the original array is changed to the sorted array. To see this we print x again
print()
print('x after sorting:', x)
Original x = [9 9 8 1 1 4 3 7 2 8]
x after sorting: [1 1 2 3 4 7 8 8 9 9]
在對(duì)秩為 2 的 ndarray 進(jìn)行排序時(shí)哲银,我們需要在 np.sort()
函數(shù)中指定是按行排序扛吞,還是按列排序。為此荆责,我們可以使用關(guān)鍵字 axis
滥比。我們來看一些示例:
# We create an unsorted rank 2 ndarray
X = np.random.randint(1,11,size=(5,5))
# We print X
print()
print('Original X = \n', X)
print()
# We sort the columns of X and print the sorted array
print()
print('X with sorted columns :\n', np.sort(X, axis = 0))
# We sort the rows of X and print the sorted array
print()
print('X with sorted rows :\n', np.sort(X, axis = 1))
Original X =
[[6 1 7 6 3]
[3 9 8 3 5]
[6 5 8 9 3]
[2 1 5 7 7]
[9 8 1 9 8]]
X with sorted columns :
[[2 1 1 3 3]
[3 1 5 6 3]
[6 5 7 7 5]
[6 8 8 9 7]
[9 9 8 9 8]]
X with sorted rows :
[[1 3 6 6 7]
[3 3 5 8 9]
[3 5 6 8 9]
[1 2 5 7 7]
[1 8 8 9 9]]