Two different errors in Numba - python

I want to compute something using numpy and numba. This is my code:
import numpy as np
from numba import jit,double,int64
#jit(locals=dict(i=int64,j=int64,k=int64,l=int64,suma=double))
def omega_comp_arrays(omega,p_kl,eta,theta,K,L,links_by_ratings_array):
#new_omega = np.zeros(omega.shape)
for rating,links in enumerate(links_by_ratings_array):
for i,j in links:
suma = 0
for k in range(K):
for l in range(L):
omega[i,j,k,l] = p_kl[k,l,rating]*theta[i,k]*eta[j,l]
suma += omega[i,j,k,l]
omega[i,j,:,:] /= suma
return omega
N_nodes=2
N_veins=[1,1]
N_items=2
N_ratings=1
K=2
L=2
##Definim matrius
theta = np.random.rand(N_nodes,K)
eta = np.random.rand(N_items,L)
p_kl = np.random.rand(K,L,N_ratings)
suma = np.sum(theta,axis =1)
theta /=suma[:,np.newaxis]
suma = np.sum(eta,axis=1)
eta /= suma[:,np.newaxis]
suma = np.sum(p_kl,axis =2)
p_kl /=suma[:,:,np.newaxis]
links_by_ratings_array = [np.array([0,0])]
omega = np.ones((N_nodes,N_items,K,L))
omega = omega_comp_arrays(omega,p_kl,eta,theta,K,L,links_by_ratings_array)
The problem occurs when I run the code:
Traceback (most recent call last):
File "test_omega.py", line 39, in <module>
omega = omega_comp_arrays(omega,p_kl,eta,theta,K,L,links_by_ratings_array)
TypeError: 'numpy.int64' object is not iterable
Exception TypeError: "'NoneType' object is not callable" in <bound method ModuleRef.__del__ of <llvmlite.binding.module.ModuleRef object at 0x7f801123d3d0>> ignored
But if I activate the nopython mode, another error appears:
Traceback (most recent call last):
File "test_omega.py", line 39, in <module>
omega = omega_comp_arrays(omega,p_kl,eta,theta,K,L,links_by_ratings_array)
File "/usr/local/lib/python2.7/dist-packages/numba/dispatcher.py", line 330, in _compile_for_args
raise e
numba.errors.TypingError: Caused By:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/numba/compiler.py", line 240, in run
stage()
File "/usr/local/lib/python2.7/dist-packages/numba/compiler.py", line 454, in stage_nopython_frontend
self.locals)
File "/usr/local/lib/python2.7/dist-packages/numba/compiler.py", line 881, in type_inference_stage
infer.propagate()
File "/usr/local/lib/python2.7/dist-packages/numba/typeinfer.py", line 846, in propagate
raise errors[0]
TypingError: failed to unpack int64
File "test_omega.py", line 10
[1] During: typing of exhaust iter at test_omega.py (10)
Failed at nopython (nopython frontend)
failed to unpack int64
File "test_omega.py", line 10
[1] During: typing of exhaust iter at test_omega.py (10)
In other words, I can't do a loop of a list of arrays because the error is in the loop of links (for i,j in links). Any suggestion?

Related

Connectorx - Unable to convert key: num when using 'partition_on' argument

I'm using the following code:
import connectorx as cx
cx_con = f"sqlite:C:\\Users\\X\\PycharmProjects\\Bot\\Trading Data\\{get_trading_database()}.db"
df = cx.read_sql(conn=cx_con,query=f"SELECT * FROM daily_quotes;"partition_on="quoteTimeInLong")
and getting the following error when I add the "partition_on" argument:
Traceback (most recent call last): File
"C:\Users\X\PycharmProjects\Bot\main.py", line 1065, in
cx_df_dict = read_symbol_data_from_db(query_type='cx').values() File
"C:\Users\X\PycharmProjects\Bot\main.py", line 499, in
read_symbol_data_from_db df = cx.read_sql(conn=cx_con, File
"C:\Users\X\PycharmProjects\venv\lib\site-packages\connectorx\init.py",
line 224, in read_sql result = _read_sql( TypeError: argument
'partition_query': Unable to convert key: num
The query works just fine if I omit partition_on, but from my understanding I can speed things up further if I add this argument.

How i fix Traceback (most recent call last):

Hello I tried to fix this but I couldn't
This is my code:
import random
Cromosomas=[[random.randint(1, 1010) for i in range(3)] for j in range(800)]
Z=[]
Fitness=[]
P=[]
C=[]
Padres=[]
def funcOb(Arreglo):
return 0.2*Arreglo[0]**2+0.08*Arreglo[1]**2+0.18*Arreglo[2]**2+0.1*(Arreglo[0]*Arreglo[1])+0.04*(Arreglo[0]*Arreglo[2])+0.06*(Arreglo[1]*Arreglo[2])-0.14*Arreglo[0]-0.11*Arreglo[1]-0.1*Arreglo[2]+120+abs(Arreglo[0]+Arreglo[1]+Arreglo[2]-1000)-10
for i in range(0,800):
Z.append(funcOb(Cromosomas[i]))
for i in range(0,800):
Fitness.append(1/(1+Z[i]))
total=sum(Fitness)
for i in range(0,800):
P.append(Fitness[i]/total);
A=0
for i in P:
A=A+i
C.append(A)
R=[random.random()for i in range(800)]
for i in range(0,800):
for j in range(0,800):
if R[i]<=C[j]:
Cromosomas[i]=Cromosomas[j]
break
def mutar(Arreglo1,Arreglo2):
b=random.randint(0, 2)
if Arreglo1[b]<=Arreglo2[b]:
Arreglo2[b]=Arreglo1[b]
return Arreglo2
if Arreglo1[b]>=Arreglo2[b]:
Arreglo1[b]=Arreglo2[b]
return Arreglo1
for i in range(0,800):
r=random.random()
if r<(0.3):
n=Cromosomas[i]
if n not in Padres:
Padres.append(n)
r=0
for i in range(0,len(Padres)-1):
if len(Padres)==1:
Cromosomas[i]=[Padres[i][0],Padres[i][1],Padres[i][2]]
else:
lista=mutar(Padres[i],Padres[i+1])
Cromosomas[i]=[lista[0],lista[1],lista[2]]
for i in range(0,480):
Cromosomas[Cromosomas==random.randint(1, 2400)]=random.randint(1, 800)
for m in range(0,800):
print(funcOb(Cromosomas[m]))
error:
Traceback (most recent call last):
File "main.py", line 59, in <module>
print(funcOb(Cromosomas[m]))
File "main.py", line 10, in funcOb
return 0.2*Arreglo[0]**2+0.08*Arreglo[1]**2+0.18*Arreglo[2]
**2+0.1*(Arreglo[0]*Arreglo[1])+0.04*(Arreglo[0]*Arreglo[2])+0.
06*(Arreglo[1]*Arreglo[2])-0.14*Arreglo[0]-0.11*Arreglo[1]-0.1*
Arreglo[2]+120+abs(Arreglo[0]+Arreglo[1]+Arreglo[2]-1000)-10
TypeError: 'int' object has no attribute '__getitem__'
This error occurs when I try to run the last function of my code and I have no idea why the function that I call at the end is already one that I had already used and it does not give me a problem, also if I try other methods such as defining another function appears the same mistake.
Thank´s for help

TypeError: 'float' object is not callable error while calling a function

This is the code I currently have:
def uniform_distribution(users,radius):
user_coordinates_distance=[]
user_coordinates=[]
finding_shadowing=[]
r=radius*np.sqrt(np.random.uniform(0,1,users))
angle=2*np.pi*np.random.uniform(0,1,users)
x = r*np.cos(angle)
y = r*np.sin(angle)
user_distance = np.sqrt(x*x+y*y)
x_shadowing=1000*x
y_shadowing=1000*y
x_shadowing=(x_shadowing-x_shadowing%10)/10
y_shadowing=(y_shadowing-y_shadowing%10)/10
finding_shadowing=shadowing(x_shadowing,y_shadowing,shadowing_matrix)
print(finding_shadowing)
for i in range (0,users):
user_coordinates=[x[i],y[i],user_distance[i],finding_shadowing[i]]
user_coordinates_distance.append(user_coordinates)
return (user_coordinates_distance)
And this is the error I get when I run it:
Traceback (most recent call last):
File "C:\Users\Ajit\Desktop\maryland\Sem 2\ENTS 656\project656\main program and functions\main_1.py", line 136, in <module>
user_coordinates=uniform_distribution(attempts,cell_radius)#list [x,y,distance,shadowing]
File "C:\Users\Ajit\Desktop\maryland\Sem 2\ENTS 656\project656\main program and functions\main_1.py", line 81, in uniform_distribution
finding_shadowing=shadowing(x_shadowing,y_shadowing,shadowing_matrix)
TypeError: 'float' object is not callable
What exactly does this error mean?

how to use the post_mortem method of pdb?

I am trying to understand how to use the pdb.post_mortem() method.
for this given file
# expdb.py
import pdb
import trace
def hello():
a = 6 * 9
b = 7 ** 2
c = a * b
d = 4 / 0
print(c)
tracer = trace.Trace()
Command prompt
'''
# first Try
λ python -i expdb.py
>>> pdb.post_mortem()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Anaconda3\lib\pdb.py", line 1590, in post_mortem
raise ValueError("A valid traceback must be passed if no "
ValueError: A valid traceback must be passed if no exception is being handled
'''
'''
# Second Try
λ python -i expdb.py
>>> pdb.post_mortem(traceback=tracer.run('hello()') )
--- modulename: trace, funcname: _unsettrace
trace.py(77): sys.settrace(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Anaconda3\lib\trace.py", line 500, in run
self.runctx(cmd, dict, dict)
File "C:\Program Files\Anaconda3\lib\trace.py", line 508, in runctx
exec(cmd, globals, locals)
File "<string>", line 1, in <module>
File "expdb.py", line 8, in hello
d = 4 / 0
ZeroDivisionError: division by zero
>>>
The post_mortem method wants a traceback object, not a Trace object. Traceback objects can be acquired from sys.exc_info()[2] inside of an except block, or you can simply call pdb.post_mortem() with no arguments directly (in the except block).
But either way, you must catch the exception before you can debug it.

Deep-learning library Neon DataIterator / ArrayIterator init error

When trying to set up the DataIterator as explained on neon tutorial.
from neon.data import DataIterator
import numpy as np
X = np.random.rand(10000, 3072)
y = np.random.randint(1, 11, 10000)
train = DataIterator(X=X, y=y, nclass=10, lshape=(3, 32, 32))
I encountered a weird error:
ERROR:neon.data.dataiterator:DataIterator class has been deprecated and renamed"ArrayIterator" please use that name.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "neon/data/dataiterator.py", line 168, in __init__
super(DataIterator, self).__init__(*args, **kwargs)
File "neon/data/dataiterator.py", line 82, in __init__
self.Xdev = [self.be.array(x) for x in X]
AttributeError: 'NoneType' object has no attribute 'array'
I then tried with ArrayIterator, keeping X, y the same.
ArrayIterator(X=X, y=y, nclass=10, lshape=(3,32,32))
With the same NoneType error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "neon/data/dataiterator.py", line 82, in __init__
self.Xdev = [self.be.array(x) for x in X]
AttributeError: 'NoneType' object has no attribute 'array'
Why would this be the case? Is there an easy fix?
Fixed the problem by generating backend.
from neon.backends import gen_backend
be = gen_backend()
(...)

Categories

Resources