I had this problem when trying to solve a optimization problem using pulp.
The code:
import pulp
import numpy as np
import math
prob = pulp.LpProblem("example", pulp.LpMaximize)
# Variable represent number of times device i is used
d = pulp.LpVariable("d", cat=pulp.LpContinuous,lowBound=0,upBound=np.inf)
var = pulp.LpVariable("var", cat=pulp.LpContinuous,lowBound=0,upBound=np.inf)
# The objective function that we want to maximize
n = len(y_arfima)
prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)*math.gamma(t-2*d))/(math.gamma(t-d)**2)) for t in range(1,n+1)])/2 - 1/2
# Actually solve the problem, this calls GLPK so you need it installed
pulp.GLPK().solve(prob)
# Print out the results
for v in prob.variables():
print (v.name, "=", v.varValue)
The error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: 'LpVariable' object has no attribute 'log'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12336/3027528854.py in <module>
11 # The objective function that we want to maximize
12 n = len(y_arfima)
---> 13 prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)*math.gamma(t-2*d))/(math.gamma(t-d)**2)) for t in range(1,n+1)])/2 - 1/2
14
15 # Actually solve the problem, this calls GLPK so you need it installed
TypeError: loop of ufunc does not support argument 0 of type LpVariable which has no callable log method
AttributeError Traceback (most recent call last)
AttributeError: 'LpVariable' object has no attribute 'log'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12336/3027528854.py in
11 # The objective function that we want to maximize
12 n = len(y_arfima)
---> 13 prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)math.gamma(t-2d))/(math.gamma(t-d)**2)) for t in range(1,n+1)])/2 - 1/2
14
15 # Actually solve the problem, this calls GLPK so you need it installed
TypeError: loop of ufunc does not support argument 0 of type LpVariable which has no callable log method
Can you help me?
Thanks!
Related
I am trying to run this code:
from find_job_titles import FinderAcora
finder=FinderAcora()
finder.findall('IT Audit & Governance')
But it gives me this error everytime:
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/find_job_titles/__init__.py in longest_match(matches)
48 """
---> 49 longest = next(matches)
50
StopIteration:
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
1 frames
<ipython-input-31-5b965ac3d7be> in <module>
----> 1 finder.findall('IT Audit & Governance')
/usr/local/lib/python3.8/dist-packages/find_job_titles/__init__.py in findall(self, string, use_longest)
82 else return all overlapping matches
83 :returns: list of matches of type `Match`
---> 84 """
85 return list(self.finditer(string, use_longest=use_longest))
86
RuntimeError: generator raised StopIteration
I tried using the suggestions from this Stack Overflow post but it didn't work.
I have been able to use # to do matrix multiplication before, but for some reason, it is not working anymore. I'm running Python 3.5.4, and in either IPython 6.2.1 or notebook 5.0.0, this unexpected error comes up:
In [1]: from numpy import arange
In [2]: A = arange(5)
In [3]: B = arange(15).reshape((5,3))
In [4]: A # B
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-455d622f3b50> in <module>()
----> 1 A # B
TypeError: unsupported operand type(s) for #: 'numpy.ndarray' and 'numpy.ndarray'
Using the matrix type doesn't help:
In [5]: from numpy import matrix
In [6]: A = matrix(arange(5))
In [7]: B = matrix(arange(15).reshape((5,3)))
In [8]: A # B
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-455d622f3b50> in <module>()
----> 1 A # B
TypeError: unsupported operand type(s) for #: 'matrix' and 'matrix'
I'm at a total loss here. Is the # operator deprecated? Am I missing something obvious? This came up while I was revising a notebook that worked fine about a year ago. Is there some other information that would help diagnose whatever is going on?
As far as I can tell, it turns out to have been an issue with having the right version of numpy, per cxw's comment. I have updated numpy, and everything seems to be working fine now.
I imported the module correctly into pandas and called it correctly using import main and then main.main(data, 1,10,2.5) but I am getting an error:
TypeError Traceback (most recent call last)
<ipython-input-52-e9913b227737> in <module>()
----> 1 main.main(data, 1, 10, 2.5)
38 dat_sh = data.shape[0]
39 #Z = random.sample(range(0,U),k_max)
---> 40 Z = cf.centroid_finder(data,sp_atr,k_max)
41
42 prototypes = {i: data[j:j+1].values.tolist()[0] for i,j in enumerate(Z)}
11 for i in range(dat_sh):
12 for j in range(dat_sh):
---> 13 D[i][j] = ed(sub_atr[i],sub_atr[j])
14
15
TypeError: 'module' object is not callable
ed is euclidean:
def ed(X2, X1):
return sqrt(sum(np.subtract(X1,X2)**2))
There may be some confusion regarding the importing of modules vs functions.
It's possible to recreate your error with a simple example, assuming that module/function importing has gotten mixed up somewhere along the way. Consider a case where we pass initial values a and b through a centroid_finder() function, which lives in cf.py:
# import cf.py as cf
import cf
a, b = ([1,2,3], [2,3,4])
cf.centroid_finder(a, b)
But centroid_finder() calls ed(), which lives in ed.py:
## cf.py
# import ed.py as ed
import ed
def centroid_finder(a, b):
print(ed(a, b))
## ed.py
from numpy import sqrt, sum
import numpy as np
def ed(X2, X1):
return sqrt(sum(np.subtract(X1,X2)**2))
Here, calling centroid_finder() will give the error you observed:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-e76ec6a0496c> in <module>()
3 a, b = ([1,2,3], [2,3,4])
4
----> 5 cf.centroid_finder(a, b)
cf.py in centroid_finder(a, b)
2
3 def centroid_finder(a, b):
----> 4 print(ed(a, b))
TypeError: 'module' object is not callable
That's because you imported a module, ed.py as ed...but what you wanted was to call the ed() function that lives inside of ed.py. That's ed.ed()!
Changing centroid_finder() to call ed.ed() produces the desired result:
# cf.py
import ed
def centroid_finder(a, b):
print(ed.ed(a, b))
Now, from the main script:
cf.centroid_finder(a, b)
# 1.73205080757
There's at least one undisclosed import shorthand in your example code, where you call sqrt in ed(). There isn't a natural sqrt() in Python, it most likely is imported from either math or numpy, e.g. from numpy import sqrt. That's not a problem, per se, but given the error you're getting about modules being non-callable, you might benefit from explicitly calling functions in your code from the modules they live in.
For example, using import numpy as np, call np.sqrt() instead of just importing sqrt() directly. This is a defensive programming posture that will prevent similar confusion in the future. (You do this already with np.subtract() in ed(); it's unclear why sqrt() doesn't get the same treatment.)
I have written a code in python to generate a sequence of ARIMA model's and determine their AIC values to compare them.The code is as below,
p=0
q=0
d=0
for p in range(5):
for d in range(1):
for q in range(4):
arima_mod=sm.tsa.ARIMA(df,(p,d,q)).fit()
print(arima_mod.params)
print arima_mod.aic()
I am getting a error message as below,
TypeError Traceback (most recent call last)
<ipython-input-60-b662b0c42796> in <module>()
8 arima_mod=sm.tsa.ARIMA(df,(p,d,q)).fit()
9 print(arima_mod.params)
---> 10 print arima_mod.aic()
global arima_mod.aic = 1262.2449736558815
11
**TypeError: 'numpy.float64' object is not callable**
Remove the brackets after print arima_mod.aic(). As I read it, arima_mod.aic is 1262.2449736558815, and thus a float. The brackets make python think it is a function, and tries to call it. You do not want that (because it breaks), you just want that value. So remove the brackets, and you'll be fine.
i have this problem in python, python keeps giving me an keyerror: weight
g.add_edge(1,3,weight=2.5)
g[1][2]['weight'] = 1.5
for n1,n2,attr in g.edges(data=True):
print n1,n2,attr['weight']
The output.
KeyError Traceback (most recent call last)
<ipython-input-56-832c29e7e1db> in <module>()
2 g[1][2]['weight'] = 1.5
3 for n1,n2,attr in g.edges(data=True):
----> 4 print n1,n2,attr['weight']
KeyError: 'weight'
0 1
i dont know why weight gives me an error?
My guess is that you have some other edges in your graph, and haven't set the weight attribute for all of them. Try the following:
for n1,n2,attr in g.edges(data=True):
print n1,n2,attr
See if attr contains a value for weight in every case.