当前位置:网站首页>Numpy sort search count set

Numpy sort search count set

2022-04-23 20:16:00 _ Carpediem

import numpy as np
x=np.random.randint(1,10,10)
print(x)
[2 2 6 2 6 4 2 3 1 6]
x = np.random.rand(5, 5) * 10 
x = np.around(x, 2) 
print(x)

[[8.68 2.14 3.02 1.86 7.85]
 [1.76 5.14 6.52 4.49 7.2 ]
 [8.16 7.3  2.34 2.37 7.31]
 [1.58 9.48 0.14 9.29 6.6 ]
 [9.18 4.98 9.1  0.7  7.94]]
y=np.sort(x)# Each row of elements is sorted by itself 
print(y)
[[1.86 2.14 3.02 7.85 8.68]
 [1.76 4.49 5.14 6.52 7.2 ]
 [2.34 2.37 7.3  7.31 8.16]
 [0.14 1.58 6.6  9.29 9.48]
 [0.7  4.98 7.94 9.1  9.18]]
print(np.sort(x,axis=0))# Each column element is sorted separately 
print(np.sort(x,axis=1))# Each row of elements is sorted separately 
[[1.58 2.14 0.14 0.7  6.6 ]
 [1.76 4.98 2.34 1.86 7.2 ]
 [8.16 5.14 3.02 2.37 7.31]
 [8.68 7.3  6.52 4.49 7.85]
 [9.18 9.48 9.1  9.29 7.94]]
[[1.86 2.14 3.02 7.85 8.68]
 [1.76 4.49 5.14 6.52 7.2 ]
 [2.34 2.37 7.3  7.31 8.16]
 [0.14 1.58 6.6  9.29 9.48]
 [0.7  4.98 7.94 9.1  9.18]]
x = np.random.randint(0, 10, 10) 
print(x) 
y=np.argsort(x)# Return sort ( Ascending ) Index subscript of the array after 
z=np.argsort(-x)# Descending 
print(y)
print(x[y])
print(z)
print(x[z])
[2 2 2 2 0 6 1 7 8 5]
[4 6 0 1 2 3 9 5 7 8]
[0 1 2 2 2 2 5 6 7 8]
[8 7 5 9 0 1 2 3 6 4]
[8 7 6 5 2 2 2 2 1 0]
x=np.array([[1,2],[0,2]])
y=np.nonzero(x)
print(y,type(y))
z=np.transpose(y)
print(z)
(array([0, 0, 1], dtype=int64), array([0, 1, 1], dtype=int64)) <class 'tuple'>
[[0 0]
 [0 1]
 [1 1]]
x = np.array([1, 5, 1, 4, 3, 4, 4]) 
y = np.array([9, 4, 0, 4, 0, 2, 1]) 
a = np.lexsort([x]) 
b = np.lexsort([-y]) # Descending 
print(a)
print(b)
[0 2 4 3 5 6 1]
[0 1 3 5 6 2 4]

aggregate :
numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None) Find the unique elements of an array.
return_index=True Returns the position of the new list element in the old list .
return_inverse=True Returns the position of the old list element in the new list .
return_counts=True Indicates the number of times the new list element appears in the old list .
numpy.in1d(ar1, ar2, assume_unique=False, invert=False) Test whether each element of a 1-D array is also present in a second array.
Whether the preceding array is included in the following array , Returns a Boolean value . The returned value is for the array of the first parameter , So the dimension is consistent with the first parameter , Boolean values also correspond to the element positions of the array one by one .

Find the intersection of two sets :
numpy.intersect1d(ar1, ar2, assume_unique=False, return_indices=False) Find the intersection of two arrays.
Return the sorted, unique values that are in both of the input arrays.
// Find the uniqueness of two arrays + Find the intersection + Sorting function .

import numpy as np
from functools import reduce

x = np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])
print(x)  # [1 3]

x = np.array([1, 1, 2, 3, 4])
y = np.array([2, 1, 4, 6])
xy, x_ind, y_ind = np.intersect1d(x, y, return_indices=True)
print(x_ind)  # [0 2 4]
print(y_ind)  # [1 0 2]
print(xy)  # [1 2 4]
print(x[x_ind])  # [1 2 4]
print(y[y_ind])  # [1 2 4]

x = reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
print(x)  # [3]

Find the union of two sets :
numpy.union1d(ar1, ar2) Find the union of two arrays.
Return the unique, sorted array of values that are in either of the two input arrays.

// Calculate the union of two sets , Uniqueness and sorting .

import numpy as np
from functools import reduce

x = np.union1d([-1, 0, 1], [-2, 0, 2])
print(x)  # [-2 -1 0 1 2]
x = reduce(np.union1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
print(x)  # [1 2 3 4 6]
''' functools.reduce(function, iterable[, initializer])  Two parameters of  function  Apply cumulatively from left to right to  iterable  The entry of , To reduce the iteratable object to a single value .  for example ,reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])  It's calculation  ((((1+2)+3)+4)+5)  Value .  Parameters on the left  x  Is the accumulated value, and the parameter on the right  y  It's from  iterable  The update value of .  If there are options  initializer, It will be placed before the entry of the iteratable object involved in the calculation , It is used as the default value when the iteratable object is empty .  If not given  initializer  also  iterable  Contains only one entry , The first item... Will be returned .  Roughly equivalent to : def reduce(function, iterable, initializer=None): it = iter(iterable) if initializer is None: value = next(it) else: value = initializer for element in it: value = function(value, element) return value '''

Find the difference set of two sets :
numpy.setdiff1d(ar1, ar2, assume_unique=False) Find the set difference of two arrays.
Return the unique values in ar1 that are not in ar2.
// The difference in the set , That is, the element exists in the first function and does not exist in the second function .

import numpy as np

a = np.array([1, 2, 3, 2, 4, 1])
b = np.array([3, 4, 5, 6])
x = np.setdiff1d(a, b)
print(x)  # [1 2]

Find the XOR of two sets :
setxor1d(ar1, ar2, assume_unique=False) Find the set exclusive-or of two arrays.
// The symmetry difference of a set , That is, the complement of the intersection of two sets . in short , Is a collection of elements that are owned by each of the two arrays .

import numpy as np

a = np.array([1, 2, 3, 2, 4, 1])
b = np.array([3, 4, 5, 6])
x = np.setxor1d(a, b)
print(x)  # [1 2 5 6]

版权声明
本文为[_ Carpediem]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210553546768.html