Im trying to run a code like this,
from numba import njit
#njit
def fun(x):
one,*two = x
print(two)
fun([1,2,3])
But, when I tried to run , it give me the following error mesage:
UnsupportedError: Use of unsupported opcode (UNPACK_EX) found
I need that code structure because I´m using scipy.optimize.minimize that admits only 1 argument, and the lenght of x varies depending on the model
Related
I'm new to programming and did search a lot through the questions but couldn't find an answer to my present problem.
I am writing a little game in python 3.8 and use pytest to run some basic tests.
One of my tests is about a function using random.randint()
Here's an extract of my code :
import random
...
def hit(self, enemy, attack):
dmg = random.randint(self.weapon.damage_min, self.weapon.damage_max) + self.strength // 4
hit() does other things after that but the problem is with this first line.
I tried to use monkeypatching to get a fake random number for the test :
def test_player_hit_missed(monkeypatch, monster, hero):
monkeypatch.setattr('random.randint', -3)
hero.hit(monster, 'Scream')
assert monster.life == 55
When I run the test, I get this error :
TypeError: 'int' object is not callable
From what I understand, the monkey-patch did replace randint() by the number I indicated (-3), but then my function hit() did try to call it nonetheless.
I thought -3 would replace randint()'s result.
Can someone explain me :
- why this doesn't work (I probably haven't correctly understood the behavior of the monkeypatch) ?
- and how I can replace the call to random.randint() by the value -3 during the test ?
Thanks
I've used frozen Random Variables (RVs) from scipy.stats in Python. For reasons I can't understand I get different behavior between a script and an interactive session:
from scipy.stats import norm, lognormal
import math as math
RV = lognorm(s=.8325546, scale=math.exp(-.34657359)) # frozen RV with many attributes
print("\ntrial of lognorm: ")
print(" " + str(lnRV(2)))
fails, saying:
TypeError: 'rv_frozen' object is not callable
Oddly, I can get this to work OK in an interactive session, for both the normal and lognormal:
Any idea what's going on here?
Stupid on my part...I am defining an instance using RV, but then trying to call an instance using lnRV.
I'm writing this program to process some lab results for college written in a .txt file. However, when I launch the program,it says there's a recursion error and the python object exceeds recursion depth. How can I solve this?
from math import *
from numpy import *
D=0.946
iD=0.001
diametros=loadtxt("bolas.txt",delimiter=",")
ind=loadtxt("incertezas.txt",delimiter=",")
for d in diametros :
for incertd in ind:
a=1+(9*d/(4*D))+(9*d/(4*D))**2
def incerteza(D,d,incertd,iD):
incert= sqrt((((9/(4*D))+(9*d/(2*D**2))*incertd)**2+(((-9*d/(4*D**2))+(-9*d/(2*D**3)))*iD)**2))
return incerteza(D,d,incertd,iD);
j=incerteza(D,d,incertd,iD)
r.append(a)
i.append(j)
print (r,i)
The culprit is in here:
def incerteza(D,d,incertd,iD):
incert= sqrt((((9/(4*D))+(9*d/(2*D**2))*incertd)**2+(((-9*d/(4*D**2))+(-9*d/(2*D**3)))*iD)**2))
return incerteza(D,d,incertd,iD);
The function incerteza keeps calling itself, with the exact same parameters, over and over and over again.
Maybe you meant to return incert ?
I am quite new to the numba package in python. I am not sure if I am using the numba.jit correctly, but the code just runs too slow with 23.7s per loops over the line: Z1 = mmd(X,Y,20)
What is the correct way to optimize the code? I need your help guys. Thank you.
Here is my code:
import pandas as pd
import numba as nb
import numpy as np
#nb.jit
def mmd(array1, array2, n):
n1 = array1.shape[0]
MMD = np.empty(n1, dtype = 'float64')
for i in range(n-1,n1):
MMD[i] = np.average(abs(array1[i+1-n:i+1] - array2[i]))
return MMD
X = np.array([i**2 for i in range(1000000)])
Y = np.array([i for i in range(1000000)])
Z1 = mmd(X,Y,20)
EDIT: simplified the code even further
EDIT2: tried #nb.jit(nopython = True), then there is an error message:
KeyError: "<class 'numba.targets.cpu.CPUTargetOptions'> does not support option: 'nonpython'"
also tried:
#nb.jit(nb.float32[:](nb.float32[:],nb.float32[:],nb.int8))
To make Numba work well you need to use "nopython" mode, as you mentioned. To enable this, simply run the program with jit replaced by njit (or equivalently, jit(nopython=True), and fix the errors one by one:
np.empty() doesn't support the dtype='float64' argument in Numba. That's OK though, because float64 is the default. Just remove it.
np.average() is not supported in Numba. That's OK, since we are not passing any weights anyway, it's the same as np.mean(). Replace it.
The built-in abs() is not supported in Numba. Use np.abs() instead.
We end up with this:
#nb.njit
def mmd(array1, array2, n):
n1 = array1.shape[0]
MMD = np.empty(n1)
for i in range(n-1,n1):
MMD[i] = np.mean(np.abs(array1[i+1-n:i+1] - array2[i]))
return MMD
And it is 100x faster.
Bonus tips:
You can initialize your sample data more concisely and faster like this:
Y = np.arange(1000000)
X = Y * Y
The first n values in the result are uninitialized garbage. You might want to clean that up somehow.
Hi I am getting the following error.
TypeError: 'numpy.ndarray' object is not callable
I wrote a function module by myself,like this:
from numpy import *
import operator
def creatDataset() :
group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
labels = ['A','A','B','B']
return group,labels
then,I want to use this function in Microsoft's command window ,I've written some code, as follows:
import KNN
group,labels=KNN.creatDataset()
group()
when I input the code "group()",the error will appear.It's the first time that i describe the question and ask for help, maybe the description is not clear ,,please forgive me.
Since "group" is a numpy.array, you cannot call it like a function.
So "group()" will not work.
I assume, you want to see it's values, so you would have to use something like
"print(group)".