- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 numpy 的帮助下编写了以下代码,我想提高 numba 的性能。我不确定为什么它不起作用,因为我已经按照 numba 系统设置了所有变量。我正在尝试加快此代码的速度,因为我将来会处理大型数据集。
import numpy as np
import math
from numba import jitclass
from numba import float64,int64
spec =[
('spacing',float64),
('n_iterations',int64),
('np_emptyhouses',float64[:,:]),
('np_agenthouses',float64[:,:]),
('similarity_threshhold',float64),
('n_changes',int64)
]
@jitclass(spec)
class geo_schelling_update:
def __init__(self,n_iterations,spacing,np_agenthouses,np_emptyhouses,similarity_threshhold):
self.spacing=spacing
self.n_iterations=n_iterations
self.np_emptyhouses=np_emptyhouses
self.np_agenthouses=np_agenthouses
self.similarity_threshhold=similarity_threshhold
def distance_vectorize(self,pointA1, pointA2,agent):
x_square=np.square(pointA1-agent[0])
y_square=np.square(pointA2-agent[1])
dist=np.sqrt(np.array(x_square,dtype=np.float32)+np.array(y_square,dtype=np.float32))
return np.round(dist,4)
def is_unsatisfied_vectorize(self,x,y):
race = np.extract(np.logical_and(np.equal(self.np_agenthouses[:,0],x),np.equal(self.np_agenthouses[:,1],y)),self.np_agenthouses[:,2])[0]
euclid_distance1=round(math.hypot(self.spacing,self.spacing),4)
euclid_distance2=self.spacing
total_agents=np.extract(np.logical_or(np.equal(np.round(np.hypot((self.np_agenthouses[:,0]-(x)),(self.np_agenthouses[:,1]-(y))),4),euclid_distance1),np.equal(np.round(np.hypot((self.np_agenthouses[:,0]-(x)),(self.np_agenthouses[:,1]-(y))),4),euclid_distance2)),self.np_agenthouses[:,2])
if total_agents.size ==0:
return False
else:
return total_agents[total_agents==race].size/total_agents.size<self.similarity_threshhold
def move_to_empty(self,x,y):
race = np.extract(np.logical_and(np.equal(self.np_agenthouses[:,0],x),np.equal(self.np_agenthouses[:,1],y)),self.np_agenthouses[:,2])[0]
x_new,y_new=self.np_emptyhouses[np.random.choice(self.np_emptyhouses.shape[0],1),:][0]
self.np_agenthouses=self.np_agenthouses[~(np.logical_and(self.np_agenthouses[:,0]==x, self.np_agenthouses[:,1]==y))]
self.np_agenthouses=np.vstack([self.np_agenthouses,[x_new,y_new,race]])
self.np_emptyhouses=self.np_emptyhouses[~(np.logical_and(self.np_emptyhouses[:,0]==x_new, self.np_emptyhouses[:,1]==y_new))]
self.np_emptyhouses=np.vstack([self.np_emptyhouses,[x,y]])
def update_helper(self,agent):
if self.is_unsatisfied_vectorize(agent[0],agent[1]):
self.move_to_empty(agent[0],agent[1])
return 1
else:
return 0
def update(self):
for i in np.arange(self.n_iterations):
np_oldagenthouses=self.np_agenthouses.copy()
n_changes=0
for row in np_oldagenthouses:
n=self.update_helper(row)
n_changes+=n
print(n_changes)
print(i)
if n_changes == 0:
break
np_agenthouses=np.array([[-71.8, 41.4, 2.0],
[-71.6, 41.4, 2.0],
[-71.6, 41.6, 2.0],
[-71.4, 41.6, 1.0],
[-71.6, 41.8, 1.0],
[-71.4, 41.8, 2.0],
[-71.6, 42.0, 2.0],
[-71.4, 42.0, 1.0],
[-71.4, 41.4, 2.0],
[-71.2, 41.4, 1.0]])
np_emptyhouses=np.array([[-71.8, 41.3],[-71.8, 41.4],[-71.5, 41.5],
[-71.5, 41.6],[-71.7, 41.8],[-71.7, 41.9],
[-71.5, 41.9],[-71.2, 41.4],[-71.6, 41.7]])
spacing=0.1
similarity_threshhold=0.65
n_iterations=100
schelling= geo_schelling_update(n_iterations,
spacing,
np_agenthouses,
np_emptyhouses,similarity_threshhold)
schelling.update()
这是我得到的错误:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<function round_ at 0x000001909ED270D0>) with argument(s) of type(s): (array(float64, 1d, C), Literal[int](4))
* parameterized
In definition 0:
All templates rejected with literals.
In definition 1:
All templates rejected without literals.
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function(<function round_ at 0x000001909ED270D0>)
[2] During: typing of call at C:/Users/ksharma/Documents/geoschelling/test2.py (42)
File "test2.py", line 42:
def is_unsatisfied_vectorize(self,x,y):
<source elided>
euclid_distance2=self.spacing
total_agents=np.extract(np.logical_or(np.equal(np.round(np.hypot((self.np_agenthouses[:,0]-(x)),(self.np_agenthouses[:,1]-(y))),4),euclid_distance1),np.equal(np.round(np.hypot((self.np_agenthouses[:,0]-(x)),(self.np_agenthouses[:,1]-(y))),4),euclid_distance2)),self.np_agenthouses[:,2])
^
[1] During: resolving callee type: BoundFunction((<class 'numba.types.misc.ClassInstanceType'>, 'is_unsatisfied_vectorize') for instance.jitclass.geo_schelling_update#190b49eec18<spacing:float64,n_iterations:int64,np_emptyhouses:array(float64, 2d, A),np_agenthouses:array(float64, 2d, A),similarity_threshhold:float64,n_changes:int64>)
[2] During: typing of call at C:/Users/ksharma/Documents/geoschelling/test2.py (57)
[3] During: resolving callee type: BoundFunction((<class 'numba.types.misc.ClassInstanceType'>, 'is_unsatisfied_vectorize') for instance.jitclass.geo_schelling_update#190b49eec18<spacing:float64,n_iterations:int64,np_emptyhouses:array(float64, 2d, A),np_agenthouses:array(float64, 2d, A),similarity_threshhold:float64,n_changes:int64>)
[4] During: typing of call at C:/Users/ksharma/Documents/geoschelling/test2.py (57)
File "test2.py", line 57:
def update_helper(self,agent):
if self.is_unsatisfied_vectorize(agent[0],agent[1]):
^
[1] During: resolving callee type: BoundFunction((<class 'numba.types.misc.ClassInstanceType'>, 'update_helper') for instance.jitclass.geo_schelling_update#190b49eec18<spacing:float64,n_iterations:int64,np_emptyhouses:array(float64, 2d, A),np_agenthouses:array(float64, 2d, A),similarity_threshhold:float64,n_changes:int64>)
[2] During: typing of call at C:/Users/ksharma/Documents/geoschelling/test2.py (68)
[3] During: resolving callee type: BoundFunction((<class 'numba.types.misc.ClassInstanceType'>, 'update_helper') for instance.jitclass.geo_schelling_update#190b49eec18<spacing:float64,n_iterations:int64,np_emptyhouses:array(float64, 2d, A),np_agenthouses:array(float64, 2d, A),similarity_threshhold:float64,n_changes:int64>)
[4] During: typing of call at C:/Users/ksharma/Documents/geoschelling/test2.py (68)
File "test2.py", line 68:
def update(self):
<source elided>
for row in np_oldagenthouses:
n=self.update_helper(row)
^
[1] During: resolving callee type: BoundFunction((<class 'numba.types.misc.ClassInstanceType'>, 'update') for instance.jitclass.geo_schelling_update#190b49eec18<spacing:float64,n_iterations:int64,np_emptyhouses:array(float64, 2d, A),np_agenthouses:array(float64, 2d, A),similarity_threshhold:float64,n_changes:int64>)
[2] During: typing of call at <string> (3)
[3] During: resolving callee type: BoundFunction((<class 'numba.types.misc.ClassInstanceType'>, 'update') for instance.jitclass.geo_schelling_update#190b49eec18<spacing:float64,n_iterations:int64,np_emptyhouses:array(float64, 2d, A),np_agenthouses:array(float64, 2d, A),similarity_threshhold:float64,n_changes:int64>)
[4] During: typing of call at <string> (3)
我也在 IDE 中运行这段代码。如果上述代码不适用于 numba,那么使该代码工作以获得相同结果的最佳方法是什么。
最佳答案
问题出在 np.round
上。从文档中并不完全清楚,但您可以通过查看 source 来了解,如果您在数组输入上使用该函数,则需要提供所有 3 个参数。所以以下不起作用:
nb.jit(nopython=True)
def func(x):
return np.round(x)
但以下内容按预期工作:
nb.jit(nopython=True)
def func(x):
out = np.empty_like(x)
np.round(x, 0, out)
return out
参见 docs for np.around
完整的描述。我将在 numba 问题跟踪器上提出一个问题,因为从查看文档时这并不明显。
关于python - 为什么这个 python 类不能与 numba jitclass 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57435833/
我是 javascript 的新手(今天开始弄乱它)。 我正在尝试更改名为“bar”的元素(div)的高度。条形图将成为图表的一部分。 我可以毫无问题地将按钮连接到更改栏高度的函数。一切正常,除了条形
错误 -> “UIVIew”没有名为“addSubView”的成员 override func viewDidLoad() { super.viewDidLoad() // Do an
我在命令行工具项目中复制并粘贴了 main.swift 下面链接中的代码。 How do you use CGEventTapCreate in Swift? 它构建没有错误,但是当我运行时, gua
我在尝试编译我的代码时遇到以下错误。 ERROR! ..\myCode\CPOI.cpp:68:41: error: cannot dynamic_cast 'screenType' (of type
我正在尝试将多个字符串连接到一个我已为其分配内存的字符串指针。这是一个例子: char *finalNumString = malloc(sizeof(char)*1024); finalNumStr
我在使用 dup2() 和 pipe() 时遇到问题。 当我尝试将管道的写入端 dup2 到 STDOUT_FILENO 时,我收到了 EBADF。 我用 gdb 在 dup2(pout[1], ST
首先,我应该说我运行的是 Windows 7。 因此,今天早上我尝试像往常一样从我的存储库中提取数据,但我做不到。我得到了错误: The authenticity of host 'github.co
刚开始在虚拟环境中运行Python,乱用Django,无法激活虚拟环境。 花了最后 4 个小时尝试在本地终端/VS 代码上激活虚拟环境 (venv),但没有成功。 避免使用“sudo pip inst
Tidyverse 的粉丝经常给出使用小标题而不是数据框的几个优点。它们中的大多数似乎旨在保护用户免于犯错误。例如,与数据框不同,小标题: 不需要 ,drop=FALSE不从数据中删除维度的论据。 不
我一直在对 Elm 应用程序进行 docker 化时遇到问题。据我所知,我已经创建了一个完整且有效的 Docker 文件……但它不起作用。 我会解释的。 所以我的脚本在 3 个文件中运行。 首先是启动
我可以在 Controller 中使用@Autowired,例如 @RestController public class Index { @Autowired HttpServlet
我定义了一个方法和一个函数: def print(str:String) = println val intToString = (n:Int) => n.toString 现在我想创作它们。 我的问
当我控制台单独记录变量“pokemons”时,它确实返回一个数组。但是当我尝试映射它时,出现错误: TypeError: pokemons.map is not a function 我的代码: im
每当我尝试在 Python 解释器中导入 smtplib 时,都会收到此错误: ImportError: cannot import name fix_eols 我该如何解决这个问题? 编辑:这是完整
我正在使用 Meteor.js 开发一个项目,但在使用 Handlebar 时遇到了一些问题:我想检索集合的最后一项,并显示字段:其中包含 html 的文本: 这是我的javascript代码: Te
你好,我想使用 Service 实现 GestureDetector 但是我有这个错误The method onTouchEvent(MotionEvent) of type GestureServi
我正在尝试在 Controller bean 中 Autowiring 接口(interface) 在我放置的上下文配置文件中 和 我的 Controller 类是 @Controller pub
我试图在 mainwindow.cpp 中包含 QtSvg,但是当我编译时它说无法打开包含文件:QtSvg。我已经在我的 *.pro 文件中添加了这个(QT += svg)。我可以知道可能是什么问题吗
鉴于以下 PostgreSQL 代码,我认为这段代码不容易受到 SQL 注入(inject)攻击: _filter 'day' _start 1 _end 10 _sort 'article_name
我想执行以下操作。这在 MySQL 中是非法的。 PostGRESQL 中关联的 CTE(“with”子句)有效。这里的假设是 MySQL 中的子查询不是完全限定的 CTE。 请注意:这个查询显然非常
我是一名优秀的程序员,十分优秀!