我知道我創建了一個 np.poly1d 物件。但是再次將其放回 np.poly1d() 中意味著什么?
import numpy as np
f = np.poly1d([1, 1, 1])
print(np.poly1d(f))
僅供參考,運行這個腳本,我得到了
2
1 x 1 x 1
uj5u.com熱心網友回復:
這是呼叫語法。
參考:https ://docs.python.org/3/reference/expressions.html#calls
np.poly1d是可呼叫f的和引數。
np.poly1d是一個類,可以像可呼叫物件一樣使用它來創建類的實體(什么是“可呼叫物件”?)。
在這種特殊情況下,f將被解釋為多項式系數的類陣列,從而產生一個新的多項式,它等價于f,因為將np.poly1d實體視為陣列會產生其系數的陣列。
>>> np.array(f)
array([1, 1, 1])
>>> g = np.poly1d(f)
>>> g == f
True
因此,在不了解更多背景關系的情況下,使用np.poly1d(f)代替f似乎毫無意義。如果意圖是創建多項式的副本以修改一個而不是另一個,則它可能很有用,因為f和g是不同的物件:
>>> g is f
False
uj5u.com熱心網友回復:
你有np.poly1d,這是一個class。
這樣做:
f = np.poly1d([1, 1, 1])
您正在初始化該類的一個實體,換句話說,您正在呼叫它__new__及其__init__方法。
當您()在something之后使用時,是因為某事是 a callable,這可以定義為@chepner評論中的那樣:
在 Python 中,任何定義
__call__方法的型別都是可呼叫的
例如,一個函式是可呼叫的,一個方法是可呼叫的,還有這樣的:
class MyClass:
def __call__(self):
...
是可呼叫的。
在 Python 中,您可以檢查是否可以使用內置函式呼叫某些內容callable。
uj5u.com熱心網友回復:
np.poly1d似乎是一個舊的,有點不標準的類定義。開頭有注釋
This forms part of the old polynomial API.
它也被編譯,因此沒有 Python 類定義可供閱讀。它也不以大寫字母開頭,這是正常的 Python 課程實踐。
您似乎正在從第一個示例開始作業:
>>> p = np.poly1d([1, 2, 3])
>>> print(np.poly1d(p))
2
1 x 2 x 3
為什么他們使用那個列印而不是print(p)是一個謎。可能只是一些沒有人愿意修復的馬虎。
np.poly1d([1,2,3])創建一個poly1d類物件。
In [63]: f = np.poly1d([1, 1, 1])
In [64]: type(f)
Out[64]: numpy.poly1d
In [65]: f
Out[65]: poly1d([1, 1, 1])
In [66]: print(f)
2
1 x 1 x 1
Out[65]是repr這個物件的顯示;Out[66]是str顯示幕。
In [67]: f1 = np.poly1d(f)
In [68]: type(f1)
Out[68]: numpy.poly1d
In [69]: id(f)
Out[69]: 139789855504368
In [70]: id(f1)
Out[70]: 139789855360240
將物件傳遞np.poly1d給類創建者似乎是創建一個新poly1d物件,但具有相同的屬性。我沒有看到記錄。
所以就 Python 語法而言,這兩行都是函式呼叫。發生與否的細節是np.poly1d.
編輯
普通類定義:
In [75]: class Foo:
...: def __init__(self, x):
...: self.x = x
...:
...: def __repr__(self):
...: return "A Foo <%s>" % self.x
In [76]: g = Foo("xxx")
In [77]: g
Out[77]: A Foo <xxx>
In [81]: type(g)
Out[81]: __main__.Foo
[76] 使用類名來實際呼叫Foo.__init__方法,回傳一個Foo實體。 print(g)或者在這種(ipython)案例中g顯示實體的repr.
參考您在另一個答案中的評論,我沒有__call__為該類定義方法Foo,因此它的實體不是callable.
In [94]: g()
Traceback (most recent call last):
Input In [94] in <module>
g()
TypeError: 'Foo' object is not callable
也不是可迭代的
In [96]: for x in g:
...: print(x)
Traceback (most recent call last):
Input In [96] in <module>
for x in g:
TypeError: 'Foo' object is not iterable
In contrast f, the np.poly1d instance is both callable and iterable:
In [97]: f()
Traceback (most recent call last):
Input In [97] in <module>
f()
TypeError: __call__() missing 1 required positional argument: 'val'
In [98]: f(3)
Out[98]: 13
In [99]: for x in f:
...: print(x)
1
1
1
In [100]: f.coeffs
Out[100]: array([1, 1, 1])
That functionality was defined in the compiled code for that class.
dbl edit
np.poly1d docs says the first arg is:
c_or_r : array_like
Usually that mean the argument is first passed through np.asarray.
In [147]: np.asarray(f)
Out[147]: array([1, 1, 1])
In [148]: id(_)
Out[148]: 139789855355984
In [149]: id(f.coeffs)
Out[149]: 139789855355984
In [150]: id(f1.coeffs)
Out[150]: 139789855355984
The f1 instance created in [67] has the coeffs array as f. np.poly1d(f) works it is effectively
np.poly1d(np.asarray(f))
np.poly1d(f.coeffs)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/436240.html
上一篇:使用Python進行最近鄰選擇
下一篇:優化幾何分布計算中的平凡求和
