我需要一个从数组中返回非NaN值的函数.目前我这样做:
>>> a = np.array([np.nan, 1, 2])
>>> a
array([ NaN, 1., 2.])
>>> np.invert(np.isnan(a))
array([False, True, True], dtype=bool)
>>> a[np.invert(np.isnan(a))]
array([ 1., 2.])
Run Code Online (Sandbox Code Playgroud)
Python:2.6.4 numpy:1.3.0
如果您有更好的方法,请分享,谢谢
我在循环中生成一维numpy数组的列表,然后将此列表转换为2d numpy数组.如果我提前知道项目的数量,我会预先分配一个2d numpy数组,但我没有,因此我将所有内容都放在列表中.
模拟如下:
>>> list_of_arrays = map(lambda x: x*ones(2), range(5))
>>> list_of_arrays
[array([ 0., 0.]), array([ 1., 1.]), array([ 2., 2.]), array([ 3., 3.]), array([ 4., 4.])]
>>> arr = array(list_of_arrays)
>>> arr
array([[ 0., 0.],
[ 1., 1.],
[ 2., 2.],
[ 3., 3.],
[ 4., 4.]])
Run Code Online (Sandbox Code Playgroud)
我的问题如下:
是否有更好的方法(性能方面)来完成收集顺序数字数据(在我的情况下是numpy数组)的任务,而不是将它们放在一个列表中然后从中创建一个numpy.array(我正在创建一个新的obj并复制数据)?在经过良好测试的模块中是否有"可扩展"矩阵数据结构?
我的2d矩阵的典型大小将介于100x10和5000x10浮点之间
编辑:在这个例子中我使用map,但在我的实际应用程序中,我有一个for循环
我想得到包含NaN的numpy数组的最小值的索引,我希望它们被忽略
>>> a = array([ nan, 2.5, 3., nan, 4., 5.])
>>> a
array([ NaN, 2.5, 3. , NaN, 4. , 5. ])
Run Code Online (Sandbox Code Playgroud)
如果我运行argmin,它将返回第一个NaN的索引
>>> a.argmin()
0
Run Code Online (Sandbox Code Playgroud)
我用Infs代替NaNs然后运行argmin
>>> a[isnan(a)] = Inf
>>> a
array([ Inf, 2.5, 3. , Inf, 4. , 5. ])
>>> a.argmin()
1
Run Code Online (Sandbox Code Playgroud)
我的困境如下:我宁愿不将NaNs改为Infs然后在我完成argmin后再回来(因为NaNs后来在代码中有意义).有一个更好的方法吗?
还有应该的结果是什么,如果所有的原始值的问题,一个为NaN?在我的实现中,答案是0
我可以将单个排序参数传递给pyes中的搜索查询,如下所示:
s = MatchAllQuery()
conn.search(query=Search(s), indexes=["test"], sort='_score')
Run Code Online (Sandbox Code Playgroud)
但我需要传递一个额外的参数来对具有相同分数的文档进行排序,如下所示:
{
"sort": [
"_score",
{
"extra_param": {
"order": "asc"
}
}
],
"query": {
"term": {
"match_all": {}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么能在pyes中做到这一点?
谢谢
我有一个值列表,我想把它们放在一个字典中,将每个值映射到它的索引.
我可以这样做:
>>> t = (5,6,7)
>>> d = dict(zip(t, range(len(t))))
>>> d
{5: 0, 6: 1, 7: 2}
Run Code Online (Sandbox Code Playgroud)
这不错,但我正在寻找更优雅的东西.
我遇到过以下内容,但它与我需要的相反:
>>> d = dict(enumerate(t))
>>> d
{0: 5, 1: 6, 2: 7}
Run Code Online (Sandbox Code Playgroud)
请分享您的解决方案,
谢谢
编辑:Python 2.6.4
对于包含1000个元素的列表,dict(zip)版本是最快的,生成器和列表推导版本几乎完全相同,它们慢约1.5倍,功能映射(反向)相当慢.
$ python -mtimeit -s"t = range(int(1e3))""d = dict(zip(t,range(len(t))))"
1000循环,最佳3:277每循环usec
$ python -mtimeit -s"t = range(int(1e3))""d = dict([(y,x)代表x,y代表枚举(t)])"
1000循环,最佳3:426 usec per环
$ python -mtimeit -s"t = range(int(1e3))""d = dict((y,x)代表x,y代表枚举(t))"
1000循环,最佳3:437每循环usec
$ python -mtimeit -s"t = range(int(1e3))""d = dict(map(反向,枚举(t)))"
100循环,最佳3:3.66毫秒每循环
我尝试对较长的列表和较短的列表(1e2,1e4,1e5)运行相同的测试,并且每个循环的时间与列表的长度成线性比例.
有人可以时间py 2.7+版本吗?
我有使用RQuantlib库的R代码.为了从python运行它我使用RPy2.我知道python有自己的quantlib绑定(quantlib-python).我想完全从R切换到python.
请告诉我如何使用quantlib-python运行以下命令
import rpy2.robjects as robjects
robjects.r('library(RQuantLib)')
x = robjects.r('x<-EuropeanOptionImpliedVolatility(type="call", value=11.10, underlying=100,strike=100, dividendYield=0.01, riskFreeRate=0.03,maturity=0.5, volatility=0.4)')
print x
Run Code Online (Sandbox Code Playgroud)
样品运行:
$ python vol.py
Loading required package: Rcpp
Implied Volatility for EuropeanOptionImpliedVolatility is 0.381
Run Code Online (Sandbox Code Playgroud) 我正在处理这里的pyes用法示例
我正在使用四个文档索引测试索引,然后使用不同的偏移量查询.在启动参数不会改变我的偏移,我继续得到同样的结果,无论它的价值.为什么会这样?
from pyes import *
conn = ES(["localhost:9200"])
try:
conn.delete_index('test-index')
except:
pass
conn.create_index('test-index')
mapping = {u'name': {'boost': 1.0,
'index': 'analyzed',
'store': 'yes',
'type': u'string',
"term_vector" : "with_positions_offsets"},
u'title': {'boost': 1.0,
'index': 'analyzed',
'store': 'yes',
'type': u'string',
"term_vector" : "with_positions_offsets"},
u'pos': {'store': 'yes',
'type': u'integer'},
u'uuid': {'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': u'string'}}
conn.put_mapping("test-type", {'properties':mapping}, ["test-index"])
conn.index({"name":"Joe Tester", "uuid":"11111", "position":1}, "test-index", "test-type", 1)
conn.index({"name":"Bill Baloney", "uuid":"22222", "position":2}, "test-index", "test-type", 2)
conn.index({"name":"Joe Joseph", "uuid":"33333", …Run Code Online (Sandbox Code Playgroud) 我有一个接受POST的pylons控制器动作
@restrict('POST')
def myaction(self):
payload = json.loads(request.body)
Run Code Online (Sandbox Code Playgroud)
我在文件中放置了正确形成的JSON(我可以从python命令行执行json.loads).
我使用以下命令将其发送到控制器:
$ curl -F payload=@./myfile -X POST -H 'Content-type:application/json' -v http://localhost:5000/mycontroller/myaction
Run Code Online (Sandbox Code Playgroud)
在控制器方面,我期待良好形成的JSON,但不是在请求中获取JSON.我得到一个字符串与其他东西像
----------------------- 6588b6680ebb\r \nConContent-Disposition:form-data;
在包含我发送到myaction的JSON字符串表示的字符串之前
我究竟做错了什么?
python ×7
arrays ×3
numpy ×3
nan ×2
controller ×1
curl ×1
dictionary ×1
enumerate ×1
json ×1
list ×1
pagination ×1
performance ×1
post ×1
pylons ×1
quantlib ×1
r ×1
rpy2 ×1
scipy ×1
sorting ×1