【numpy.lexsort】使用之无脑也能看懂小笔记

numpy. lexsort ( keysaxis=-1 )

Perform an indirect sort using a sequence of keys.

Given multiple sorting keys, which can be interpreted as columns in a spreadsheet, lexsort returns an array of integer indices that describes the sort order by multiple columns. The last key in the sequence is used for the primary sort order, the second-to-last key for the secondary sort order, and so on. The keys argument must be a sequence of objects that can be converted to arrays of the same shape. If a 2D array is provided for the keys argument, it’s rows are interpreted as the sorting keys and sorting is according to the last row, second last row etc.

Parameters:

keys : (k, N) array or tuple containing k (N,)-shaped sequences

The k different “columns” to be sorted. The last column (or row if keys is a 2D array) is the primary sort key.

axis : int, optional

Axis to be indirectly sorted. By default, sort over the last axis.

Returns:

indices : (N,) ndarray of ints

Array of indices that sort the keys along the specified axis.



Example:

>>> a = [1,5,1,4,3,4,4] # First column
>>> b = [9,4,0,4,0,2,1] # Second column
>>> ind = np.lexsort((b,a)) # Sort by a, then by b
>>> print(ind)
[2 0 4 6 5 3 1]

脑残的我一开始看没反应过来这玩意是咋整的,于是记录如下:


np.lexsort完成了这下面几步:


index: 0 1 2 3 5 6

a: 1 5 1 3 4 4

先根据 a 内的数据对每个元素从小到大进行排序可以得到

index_sorted_v1: 4 5 1

但是可以看到0, 2的大小同为1,3,5,6大小同为4,一般argsort的话会按照原本的顺序来拍,但这里是lexsort,并且我们有b,所以利用b内对应位置元素的大小来进行排序

b_sorted_v1: 0 0 4 2 4

index_sorted_v2: 0 5 1

这就是我们得到的结果ind。

这个是可以帮助我们在以一个标准来衡量某个属性的同时需要用一个亚属性来衡量的时候使用的。人话说就是,打比方我是个贫穷的炼金狗,我要请女朋友去吃饭。虽然女朋友对我很重要,但是我首先考虑的是餐厅价格这个因素,在选择了某个特定的价格区间之后,我再考虑的因素是舒适度价格对应的是上面的a,而舒适度对应的是上面的b。一般这个还可以和random结合,比如np.lexsort((random_array, target_array))。加入一定的随机性,即只要价格我可以接受,那么舒适度怎样老天决定吧。

初次接触是:smac package下面的smbo.py

选择random的configuration时的

indices = np.lexsort((random.flatten(), acq_values.flatten()))



嗯,结束,就是这么无聊,开喷吧......

你可能感兴趣的:(代码废的Python日志)