python numpy np.lexsort()(使用键序列执行间接稳定排序)(具体没太搞懂区别?)
生活随笔
收集整理的這篇文章主要介紹了
python numpy np.lexsort()(使用键序列执行间接稳定排序)(具体没太搞懂区别?)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
from numpy\core_multiarray_umath.py
@array_function_from_c_func_and_dispatcher(_multiarray_umath.lexsort) def lexsort(keys, axis=None):"""lexsort(keys, axis=-1)Perform an indirect stable 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.給定多個排序鍵(可以將其解釋為電子表格中的列),lexsort返回一個整數索引數組,該數組描述按多個列排序的順序。 序列中的最后一個鍵用于主排序順序,倒數第二個鍵用于輔助排序順序,依此類推。 keys參數必須是可以轉換為相同形狀的數組的對象序列。 如果為keys參數提供了2D數組,則將其行解釋為排序鍵,并根據最后一行,倒數第二行等進行排序。Parameters----------keys : (k, N) array or tuple containing k (N,)-shaped sequencesThe `k` different "columns" to be sorted. The last column (or row if `keys` is a 2D array) is the primary sort key.(k,N)個包含k(N,)形序列的數組或元組要排序的k個不同的“列”。 最后一列(如果keys是2D數組,則為行)是主排序鍵。axis : int, optionalAxis to be indirectly sorted. By default, sort over the last axis.要間接排序的軸。 默認情況下,對最后一個軸進行排序。Returns-------indices : (N,) ndarray of intsArray of indices that sort the keys along the specified axis.(N,)個整數的ndarray沿指定軸對鍵進行排序的索引數組。See Also--------argsort : Indirect sort.ndarray.sort : In-place sort.sort : Return a sorted copy of an array.Examples--------Sort names: first by surname, then by name.對名稱進行排序:首先按姓氏,然后按名稱。(就是如果姓氏相同,就按名稱排)>>> surnames = ('Hertz', 'Galilei', 'Hertz')>>> first_names = ('Heinrich', 'Galileo', 'Gustav')>>> ind = np.lexsort((first_names, surnames))>>> indarray([1, 2, 0])>>> [surnames[i] + ", " + first_names[i] for i in ind]['Galilei, Galileo', 'Hertz, Gustav', 'Hertz, Heinrich']Sort two columns of numbers:對兩列數字進行排序:>>> 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>>> indarray([2, 0, 4, 6, 5, 3, 1])>>> [(a[i],b[i]) for i in ind][(1, 0), (1, 9), (3, 0), (4, 1), (4, 2), (4, 4), (5, 4)]Note that sorting is first according to the elements of ``a``.Secondary sorting is according to the elements of ``b``.請注意,排序首先是根據``a''的元素進行的。二級排序是根據``b''的元素進行的。A normal ``argsort`` would have yielded:正常的``argsort''會產生:>>> [(a[i],b[i]) for i in np.argsort(a)][(1, 9), (1, 0), (3, 0), (4, 4), (4, 2), (4, 1), (5, 4)]Structured arrays are sorted lexically by ``argsort``:>>> x = np.array([(1,9), (5,4), (1,0), (4,4), (3,0), (4,2), (4,1)],... dtype=np.dtype([('x', int), ('y', int)]))>>> np.argsort(x) # or np.argsort(x, order=('x', 'y'))array([2, 0, 4, 6, 5, 3, 1])"""if isinstance(keys, tuple):return keyselse:return (keys,)示例:
# -*- coding: utf-8 -*- """ @File : plot.py @Time : 2020/2/24 8:55 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """import matplotlib.pyplot as pltimport numpy as np# 如發現格式不對可用記事本或notepad批量替換 keyword = {'11:30.0': (50000, 13.96), '12:16.0': (54500, 13.20), '13:15.0': (47500, 12.48),'14:22.0': (55450, 12.44), '14:35.0': (55430, 13.72), '17:03.0': (13990, 11.00),'17:38.0': (9058, 11.60), '17:57.0': (5044, 12.46), '18:20.0': (1300, 13.80),'18:25.0': (900, 13.90), '18:28.0': (700, 13.96), '18:40.0': (200, 13.34),'18:42.0': (150, 13.10), '18:44.0': (100, 11.80), '18:44.2': (90, 11.34),'18.44.4': (80, 11.38), '18:44.8': (70, 9.50), '18:45.0': (60, 9.20),'18:46.0': (50, 11.9), '18:46.3': (40, 10.8), '18:46.6': (30, 9.20),'18:49.0': (20, 9.70), '18:49.6': (15, 6.90), '18:50.3': (13, 4.70),'18:50.9': (12, 3.80), '18:51.5': (11, 2.60), '18:52.2': (10, 1.70),'18:52.9': (9, 1.00), '18:53.6': (8, 0.2), '18:54.3': (7, 0.06),'18:55.0': (6, 0.02)}data = []for key in keyword:data.append(keyword[key])data = np.array(data) # print(data) # [[5.000e+04 1.396e+01] # [5.450e+04 1.320e+01] # [4.750e+04 1.248e+01] # [5.545e+04 1.244e+01] # [5.543e+04 1.372e+01] # [1.399e+04 1.100e+01] # [9.058e+03 1.160e+01] # [5.044e+03 1.246e+01] # [1.300e+03 1.380e+01] # [9.000e+02 1.390e+01] # [7.000e+02 1.396e+01] # [2.000e+02 1.334e+01] # [1.500e+02 1.310e+01] # [1.000e+02 1.180e+01] # [9.000e+01 1.134e+01] # [8.000e+01 1.138e+01] # [7.000e+01 9.500e+00] # [6.000e+01 9.200e+00] # [5.000e+01 1.190e+01] # [4.000e+01 1.080e+01] # [3.000e+01 9.200e+00] # [2.000e+01 9.700e+00] # [1.500e+01 6.900e+00] # [1.300e+01 4.700e+00] # [1.200e+01 3.800e+00] # [1.100e+01 2.600e+00] # [1.000e+01 1.700e+00] # [9.000e+00 1.000e+00] # [8.000e+00 2.000e-01] # [7.000e+00 6.000e-02] # [6.000e+00 2.000e-02]]x = data[:, 0] # print(x) # [5.000e+04 5.450e+04 4.750e+04 5.545e+04 5.543e+04 1.399e+04 9.058e+03 # 5.044e+03 1.300e+03 9.000e+02 7.000e+02 2.000e+02 1.500e+02 1.000e+02 # 9.000e+01 8.000e+01 7.000e+01 6.000e+01 5.000e+01 4.000e+01 3.000e+01 # 2.000e+01 1.500e+01 1.300e+01 1.200e+01 1.100e+01 1.000e+01 9.000e+00 # 8.000e+00 7.000e+00 6.000e+00] y = data[:, 1] # print(y) # [13.96 13.2 12.48 12.44 13.72 11. 11.6 12.46 13.8 13.9 13.96 13.34 # 13.1 11.8 11.34 11.38 9.5 9.2 11.9 10.8 9.2 9.7 6.9 4.7 # 3.8 2.6 1.7 1. 0.2 0.06 0.02]ind = np.lexsort((x,)) # print(ind) # [30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 # 6 5 2 0 1 4 3]data_sort = [(x[i], y[i]) for i in ind]# print(data_sort) # [(6.0, 0.02), (7.0, 0.06), (8.0, 0.2), (9.0, 1.0), (10.0, 1.7), (11.0, 2.6), (12.0, 3.8), (13.0, 4.7), (15.0, 6.9), (20.0, 9.7), (30.0, 9.2), (40.0, 10.8), (50.0, 11.9), (60.0, 9.2), (70.0, 9.5), (80.0, 11.38), (90.0, 11.34), (100.0, 11.8), (150.0, 13.1), (200.0, 13.34), (700.0, 13.96), (900.0, 13.9), (1300.0, 13.8), (5044.0, 12.46), (9058.0, 11.6), (13990.0, 11.0), (47500.0, 12.48), (50000.0, 13.96), (54500.0, 13.2), (55430.0, 13.72), (55450.0, 12.44)]x_sort, y_sort = np.array(data_sort)[:, 0], np.array(data_sort)[:, 1]# 用3次多項式擬合 可以改為5 次多項式。。。。 返回三次多項式系數 z1 = np.polyfit(x_sort, y_sort, 5) p1 = np.poly1d(z1)# 在屏幕上打印擬合多項式 print(p1) # 3 2 # 2.534e-13 x - 2.506e-08 x + 0.000714 x + 7.821# 設置繪制間隔 x_lin = np.arange(0, 60000, 5)yvals = p1(x_lin) # 也可以使用yvals=np.polyval(z1,x)plot1 = plt.plot(x_sort, y_sort, '*', label='original values') plot2 = plt.plot(x_lin, yvals, 'r', label='polyfit values')# 限制繪制上下限 plt.ylim(0, 16)plt.xlabel('Illumination/lm')plt.ylabel('Detect num/pcs')plt.legend(loc=4) # 指定legend的位置,讀者可以自己help它的用法plt.title('polyfitting')plt.show()plt.savefig('p1.png')
參考文章:python numpy np.argsort()(返回將對數組進行排序的索引)(不懂區別?)
總結
以上是生活随笔為你收集整理的python numpy np.lexsort()(使用键序列执行间接稳定排序)(具体没太搞懂区别?)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python matplotlib.py
- 下一篇: python numpy np.args