【numpy.lexsort】使用之无脑也能看懂小笔记
2017-12-15 17:52
465 查看
numpy.
lexsort(keys, axis=-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. |
>>> 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 4 3 4 4
先根据 a 内的数据对每个元素从小到大进行排序可以得到
index_sorted_v1: 0 2 4 3 5 6 1
但是可以看到0, 2的大小同为1,3,5,6大小同为4,一般argsort的话会按照原本的顺序来拍,但这里是lexsort,并且我们有b,所以利用b内对应位置元素的大小来进行排序
b_sorted_v1: 9 0 0 4 2 1 4
index_sorted_v2: 2 0 4 6 5 3 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()))
嗯,结束,就是这么无聊,开喷吧......
相关文章推荐
- vector的sort使用——学习笔记
- Lex和Yacc使用学习笔记(二)
- numpy.zeros 使用方法 python学习笔记1
- python numpy 使用笔记 矩阵操作
- Numpy基础笔记(包括基础函数使用)
- 快速熟练使用numpy(学习笔记)
- networkx使用笔记(三)之好汉篇numpy(2)
- numpy.zeros(np.zeros)使用方法--python学习笔记31
- Lex和Yacc使用学习笔记(一)
- C++笔记(1):使用STL中sort()对struct排序
- python笔记 使用numpy中的savez(save)保存非数组数据
- 《自制编程语言》笔记:使用yacc与lex制作简单计算器
- (大卫的阅读笔记)C++中使用union的几点思考
- PHP学习笔记(3)----phpmyadmin的安装与使用[原创]
- User Interface Process (UIP) Application Block 使用笔记(转)
- C#中使用Oracle 存储过程笔记 2004.11
- emacs 使用笔记
- C#字符串的使用笔记
- JSP学习笔记(四)-----JSP动作的使用
- C#字符串的使用笔记