ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()def works(x, m, b):
return m*x+bdef breaks(m, x, b):
return m*x+b
I was doing a fit for some of my data and kept getting the errorI finally figured out that it was because my independent variable wasn't the first variable in the defining function.ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Using slope-intercept as the example function, this works:And this doesn't:def works(x, m, b):
return m*x+bdef breaks(m, x, b):
return m*x+b
I've attached a file that demonstrates this.
There's nothing inherently wrong with requiring the independent variable come first, but it's extremely user-unfriendly to have this be completely undocumented.
Nothing in the error message gives a hint this is the reason for the error (at least for a novice like me),
and the documentation on this page actually says "The model function will normally take an independent variable (generally, the first argument)" which implies the variable order in the function doesn't matter.
It was blind luck that I changed the order of my variables when I was retyping my model function and then figured out that was the reason it worked.
Now that I know, it's not a problem for me. But hopefully something can be done to save someone else the same headache.
I'm using python 2.7 and the spyder IDE
Hm, I don't see how that implies that order does not matter. It says that a model normally takes and independent variable, and that this will normally be the first argument. That implies to me that a model may not need to have an independent variable and that the independent variable may not always be the first argument.Depending on how you count, within about 5 lines of that sentence in the doc and docstring, it also lists `independent_vars` as an argument to `Model`, describing this as "Arguments to func that are independent variables (default is None)".It may be that you were assuming that the argument name `x` implies that it should be an independent variable. Though a large majority of the examples do use `x` as the only independent variable, it is only position that sets the default independent variable, not name.
I didn't think about how Model determines the independent variable much, I guess I thought that it assumed whichever variable it was given that had multiple values was the independent i.e. m=3, b=2, x=[0,1,2,3] so x is the independent variable. I definitely didn't think it was taking x to be the independent variable (in my actual function angular frequency is the independent variable, so it's symbol is w).
I see the other places in the documentation, and that makes it much clearer now that I know what I'm looking for. I'm not great at parsing and understanding documentation because I'm not a programmer, just a chemist who likes using Python and LMFIT to analyze and graph his data :)
So for me the reading of
(generally, the first argument)Implied "usually the first argument is independent, but it doesn't need to be", and I didn't need to look at argument order as the problem with my function, which meant that the independent_vars parameter didn't instantly jump out to me as "oh, I need to specify the independent parameter if it's not first in the function" While something like(the first argument, unless otherwise specified)Instantly tells me the order matters and I messed up, while also telling me it doesn't have to be and I can look through the documentation for how to change it. As I said, I'm not a programmer but I appreciate the LMFIT module very much and wanted to do anything I could to help, even if that's just addressing a single slightly ambiguous line in the documentation.