我想看看效果如何numpy.argsort()。
在檔案中,來源
numpy.argsort()在numpy.core.fromnumeric.py. 沒關系。 https://numpy.org/doc/stable/reference/generated/numpy.argsort.htmlcore.fromnumeric.argsort()有點復雜。
無論裝飾器如何,fromnumeric.argsort(arr)回傳_wrapfunc(arr, "argsort")然后回傳arr.argsort()。沒問題。
假設arr是numpy.ndarray,它可能在array_api.__init__.py. https://github.com/numpy/numpy/blob/v1.21.0/numpy/core/fromnumeric.pyarray_api.argsort()來自array_api._sorting_functions.argsort(). 好的。https://github.com/numpy/numpy/blob/main/numpy/array_api/__init__.py_sorting_functions.argsort()呼叫numpy.argsort()。這就是我最初想要的。 https://github.com/numpy/numpy/blob/main/numpy/array_api/_sorting_functions.py
額外的
在
numpy.__init__.pyi,numpy.argsort()來自core.fromnumerichttps://github.com/numpy/numpy/blob/main/numpy/__init__.pyi1.并且5.是一回事。
是回圈參考嗎?雖然我知道那行得通。是it might be in array_api.__init__.py.的2.錯嗎?那么,該實作的實際位置在哪里?
聚苯乙烯
我以為np.unique是慢的時候return_index=True。我想要np.unique一個排序的陣列,但我發現了np.uniquecalls np.argsort。然后,我嘗試區分np.argsortfromnp.sort以便我需要np.argsort.
一開始我不得不說這個。
uj5u.com熱心網友回復:
為什么要看原始碼?要在您自己的c代碼專案中實作它?我認為它不會幫助您在 python 中更有效地使用它。在我使用的 Ipython 會話中??
In [22]: np.argsort??
...
return _wrapfunc(a, 'argsort', axis=axis, kind=kind, order=order)
好的,這就是函式將責任傳遞給方法的典型案例。如有必要,函式版本會將輸入轉換為陣列,然后呼叫陣列的方法。通常功能版本有更完整的檔案,但功能基本相同。
In [21]: arr.argsort??
Type: builtin_function_or_method
通常這就是故事的結尾。
另一種方法是單擊[source]檔案上的鏈接。這導致了同樣的事情。
注意:
@array_function_dispatch(_argsort_dispatcher)
最近的版本增加了這dispatch一層;查看發行說明以獲取更多詳細資訊。根據我的經驗,這只會使搜索代碼變得更加困難。
另一個步驟是去github搜索。有時這會產生一些有用的資訊,但通常是一種瘋狂的追逐。
作為用戶,我不需要知道“如何”的細節。閱讀檔案很容易,如果我還有問題,可以做一些實驗。深入研究c代碼無助于更好地使用它。
至于你補充的問題:
所有ndarray物件都是“多陣列”,從 0 到 32 維。
github
在numpy github我搜索argsort,并選擇了最有希望的檔案,numpy/core/src/multiarray/methods.c
這有功能
array_argsort(PyArrayObject *self,
PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
跳過似乎處理輸入引數的代碼,看起來作業是在
res = PyArray_ArgSort(self, axis, sortkind);
這似乎是在 numpy/core/src/multiarray/item_selection.c 中定義的
PyArray_ArgSort(PyArrayObject *op, int axis, NPY_SORTKIND which)
...
if (argsort == NULL) {
if (PyArray_DESCR(op)->f->compare) {
switch (which) {
default:
case NPY_QUICKSORT:
argsort = npy_aquicksort;
break;
case NPY_HEAPSORT:
argsort = npy_aheapsort;
break;
case NPY_STABLESORT:
argsort = npy_atimsort;
break;
...
ret = _new_argsortlike(op2, axis, argsort, NULL, NULL, 0);
等等 ....
這些都沒有幫助我更好地使用它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/341073.html
標籤:Python 麻木的 numpy-ndarray numpy-ufunc
