受Haskell的启发:
如何在Python中使用numpy数组实现以下内容?
In [13]: [(x if x>3 else None) for x in range(10)]
Out[13]: [None, None, None, None, 4, 5, 6, 7, 8, 9]
换句话说,我正在寻找一个numpy函数,该函数在Haskell中具有签名:f:[a]->(a->Maybe a)->[Maybe a]
,其中[a]
将是一个numpy列表。
我正在尝试这个:
np.apply_along_axis(lambda x:x if x>3 else None,0,np.arange(10))
但它不起作用:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
答案 0 :(得分:5)
NumPy的where()
可以解决问题:
abstract class A
{
public function getVars()
{
return get_object_vars($this);
}
}
class B extends A
{
private $a;
private $b;
private $c;
public function data()
{
...
foreach($this->getVars() as $var) {
...
}
}
}
上面的代码创建了一个新数组。如果您希望修改In [429]: import numpy as np
In [430]: arr = np.arange(10, dtype=np.object)
In [431]: np.where(arr > 3, arr, None)
Out[431]: array([None, None, None, None, 4, 5, 6, 7, 8, 9], dtype=object)
到位,可以使用布尔索引arr
(正如@Chris Mueller所指出的)或putmask()
:
arr[arr < 4] = None
除非您被限制使用In [432]: np.putmask(arr, arr < 4, None)
In [433]: arr
Out[433]: array([None, None, None, None, 4, 5, 6, 7, 8, 9], dtype=object)
作为“旗帜”值,否则我建议您坚持@ ev-br的建议并使用None
代替。我会按照这种方法来评估绩效:
np.nan
请注意,我使用了更大的数组来进一步突出效率差异。获胜者是...... boolean indexing。
答案 1 :(得分:5)
我建议您重新审视您希望None
处于一个numpy数组中的前提:您需要一个dtype=object
数组来存储None
,即您&#39 ; ll将python对象存储在一个数组中。通过这种方式,您将失去numpy提供给普通列表的大部分优势。
如果你想要的是一个哨兵价值信号&#34;不可用&#34;或者&#34;不知道&#34;,你可以将其他值作为浮点数,那么你最好使用np.nan
>>> x = np.arange(10, dtype=float)
>>> x[x < 3] = np.nan
>>> x
array([ nan, nan, nan, 3., 4., 5., 6., 7., 8., 9.])
答案 2 :(得分:3)
与Tonechas&#39;略有不同的技术。它使用numpy的布尔索引功能。
>>> import numpy as np
>>> x = np.arange(10).astype(np.object)
>>> x[x<4] = None
返回
array([None, None, None, None, 4, 5, 6, 7, 8, 9], dtype=object)