scipy.optimize.newton Not Accepting "full_output" Option - python

Long time listener, first time caller here.
I'm relatively new to Python, but not totally hopeless. The code below works as long as I omit the full_output option.
from scipy import optimize
res = optimize.newton(calcdelta, currentstartcost, full_output=True)
But when full_output is included (either True or False), it spits out the following error:
TypeError: newton() got an unexpected keyword argument 'full_output'
It's not the end of the world, but any help would be appreciated!
Thanks.

Looks like I just needed to update. I had to update statsmodels as well, as whatever version I was using would not work after updating scipy. I had assumed everything would be current since I just started using Python a few months ago, but I guess not.
Thanks to jakub for pointing me in the right direction

Related

How can I change the format of Python GTK3 SpinButton displayed text?

Basing my code on this allegedly Python-specific documentation example, I have:
def on_output(spin):
adj = spin.get_adjustment()
val = int(adj.get_value())
s = "%02d" % val
print "on_output: %s" % s
spin.set_text(s)
which I connect to my SpinButton's "output" signal. It seems to work when the control is first displayed (shows "00"), but when I click SpinButton's increment button, the formatted value from on_output is overwritten, so e.g. my "01" is shown as plain "1". Looks like another signal or event is causing the control to reformat itself after on_output, but I'm struggling a bit to diagnose. Any experts on GTK3 with Python, please help with debugging suggestions.
Platform is Xubuntu 18.10, Python 2.7, GTK3 3.22.
Wow! If ever there was a case for responding with 'RTFM!' this was it. As very politely pointed out by Alexander Dmitriev, the 'return' statement is missing. In my first attempt, I returned True, but it failed (for some presumably unrelated reason) so I tried False which made no difference. Somehow, the return value got lost after that, and when I re-read the doco 'carefully' I missed seeing it -- we see what we expect to see! I must be getting too old for this hacking game, maybe time to hang up my keyboard. :)

Gurobi Python Error (NoneType has no len())

I need to write an optimization file for Gurobi (Python) that is a modified version of a classic TSP. I tried to run the example file from their website:
examples.gurobi.com/traveling-salesman-problem/
I always get the following error:
TypeError: object of type 'NoneType' has no len()
What do I need to change?
Thx
Full code: https://www.dropbox.com/s/ewisx805b3o2wq5/beispiel_opt.py?dl=0
I can confirm the error with the example code from Gurobi's website. At the first look the problem seems to be inside the subtour function, that returns None if sum(lengths) == n and the missing check for if tour is None inside the subtourlim function.
Instead of providing a fix for the specific code, I first checked the examples that Gurobi installs inside the specific installation directory:
Mac: /Library/gurobi810/mac64/examples/python/
Linux: /opt/gurobi800/linux64/examples/python/
Windows: c:\gurobi800\win64\examples\python\
And surprisingly the tsp.py from there runs without any errors. Note also that the two mentioned functions are revised. So I guess the example from the website is just a old version of the code.

Weird error in sklearn.cluster.KMeans

I want to use mr. yasaichi's implementation of x-means written in Python for my master's thesis (yasaichi's x-means: https://gist.github.com/yasaichi/254a060eff56a3b3b858) . For the last few weeks there have been no problem and I have been running the algorithm several times on various data sets. Today, however, a weird error popped up:
AttributeError: 'KMeans' object has no attribute 'get_params'.
The error comes from line 75 in the yasaichi's implementation:
labels = range(0, k_means.get_params()["n_clusters"])
Originally I thought it was me who had done some weird changes to the code, but when I re-downloaded the original again it came up with the same error.
Any ideas?
It sounds like the KMeans object you are trying to use doesn't have the method get_params.
I just tested the code at https://gist.github.com/yasaichi/254a060eff56a3b3b858 and it worked for me. So, my best guess is that you are somehow overwriting the KMeans object or that your code is using a cached version of the code that defines the KMeans object.
To verify this, try adding print dir(k_means) before line 75 of yasaichi's implementation. You should also see that print k_means.__module__ should show sklearn.cluster.k_means_. If this is the case, the final thing I would recommend would be deleting the compiled Python file implementing the k_means_ module. This can be found by running the following:
import sklearn.cluster.k_means_
print sklearn.cluster.__file__

How to debug Python memory fault?

Edit: Really appreciate help in finding bug - but since it might prove hard to find/reproduce, any general debug help would be greatly appreciated too! Help me help myself! =)
Edit 2: Narrowing it down, commenting out code.
Edit 3: Seems lxml might not be the culprit, thanks! The full script is here. I need to go over it looking for references. What do they look like?
Edit 4: Actually, the scripts stops (goes 100%) in this, the parse_og part of it. So edit 3 is false - it must be lxml somehow.
Edit 5 MAJOR EDIT: As suggested by David Robinson and TankorSmash below, I've found a type of data content that will send lxml.etree.HTML( data ) in a wild loop. (I carelessly disregarded it, but find my sins redeemed as I've paid a price to the tune of an extra two days of debug! ;) A working crashing script is here. (Also opened a new question.)
Edit 6: Turns out this is a bug with lxml version 2.7.8 and below (at
least). Updated to lxml 2.9.0, and bug is gone. Thanks also to the fine folks over at this follow-up question.
I don't know how to debug this weird problem I'm having.
The below code runs fine for about five minutes, when the RAM is suddenly completely filled up (from 200MB to 1700MB during the 100% period - then when memory is full, it goes into blue wait state).
It's due to the code below, specifically the first two lines. That's for sure. But what is going on? What could possibly explain this behaviour?
def parse_og(self, data):
""" lxml parsing to the bone! """
try:
tree = etree.HTML( data ) # << break occurs on this line >>
m = tree.xpath("//meta[#property]")
#for i in m:
# y = i.attrib['property']
# x = i.attrib['content']
# # self.rj[y] = x # commented out in this example because code fails anyway
tree = ''
m = ''
x = ''
y = ''
i = ''
del tree
del m
del x
del y
del i
except Exception:
print 'lxml error: ', sys.exc_info()[1:3]
print len(data)
pass
You can try Low-level Python debugging with GDB. Probably there is a bug in Python interpreter or in lxml library and it is hard to find it without extra tools.
You can interrupt your script running under gdb when CPU usage goes to 100% and look at stack trace. It will probably help to understand what's going on inside script.
it must be due to some references which keep the documents alive. one must always be careful with string results from xpath evaluation. I see you have assigned None to tree and m but not to y,x and i .
Can you also assign None to y,x and i .
Tools are also helpful when trying to track down memory problems. I've found guppy to be a very useful Python memory profiling and exploration tool.
It is not the easiest to get started with due to a lack of good tutorials / documentation, but once you get to grips with it you will find it very useful. Features I make use of:
Remote memory profiling (via sockets)
Basic GUI for charting usage, optionally showing live data
Powerful, and consistent, interfaces for exploring data usage in a Python shell

Python error, 'bool' type not callable

I have a very long code in Python so I can't write it all here. Anyhow, the problem is that I'm plotting a function in the code with the semilogx command and everything works fine. However, if I switch to the plot command I got this error:
TypeError: 'bool' object is not callable
What do you think might cause the problem?
It seems that anywhere I use the plot command in the code I get the same error.
I tried plotting the first variable that I use in my code:
f=loadtxt(folder_out+"stars/stars"+str(output)+".txt",skiprows=2)
ids=f[:,0]
mass_star=f[:,1] # mass in Msun
x=f[:,2]
y=f[:,3]
z=f[:,4]
age=f[:,5] # age in Myr
plot(x,y,'.')
And the last line gives me the error. I'm sure I read the variable from the file, they have the same dimension.
Your error indicates that plot is not a function, as you think it is, but a bool (ie. True or False, the result of a boolean expression). This could be for a couple of reasons:
You explicitly assign a bool to a variable named plot (perhaps in a loop — remember that loops and if statements in Python do not create a new scope)
You have a bunch of from whatever import * statements, one of which imports a name plot which is clobbering the one from pylab (it might even be a from whatever import plot that you haven't noticed)
You could try to narrow it down by a simple text search for plot to see if you're doing it explicitly. You could also remove imports and strip down your script until it works as expected, and try to identify the problematic line.

Categories