I have such Python code
import numpy as np
import matplotlib.pyplot as plt
import math
from scipy import optimize as opt
def func1(x):
f1 = math.exp(x-2)+x**3-x
return f1
solv1_bisect = opt.bisect(func1, -1.5, 1.5)
x1 = np.linspace(-1.5,1.5)
y1 = func1(x1)
plt.plot(x1,y1,'r-')
plt.grid()
print('solv1_bisect = ', solv1_bisect)
and I've got the error message such as
TypeError: only length-1 arrays can be converted to Python scalars
Please help me to fix it, Thanks!
The problem is that you are using math.exp that expects a Python scalar, for example:
>>> import numpy as np
>>> import math
>>> math.exp(np.arange(3))
Traceback (most recent call last):
File "path", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-10-3ac3b9478cd5>", line 1, in <module>
math.exp(np.arange(3))
TypeError: only size-1 arrays can be converted to Python scalars
Use np.exp instead:
def func1(x):
f1 = np.exp(x - 2) + x ** 3 - x
return f1
The difference between np.exp and math.exp is that math.exp works with Python's numbers (floats and integers) while np.exp can work with numpy arrays. In your code the argument x is a numpy array, hence the error.
Related
I am trying to find these 3 function's Wronskian determinant but the code has "TypeError: No loop matching the specified signature and casting was found for ufunc det ". How can I solve it?
import numpy as np
import numpy.linalg
import sympy as sp
x = sp.Symbol('x')
e=sp.exp(-3*x) #inputs
f=sp.cos(2*x)
g=sp.sin(2*x)
buneya=np.array([e,f,g],dtype=object)
a=sp.diff(buneya[0]) #first derivative
b=sp.diff(buneya[1])
c=sp.diff(buneya[2])
k=sp.diff(a)
l=sp.diff(b) #second derivative
m=sp.diff(c)
wronskian=np.array([[e,f,g],[a,b,c],[k,l,m]],dtype=object)
print (np.linalg.det(wronskian) ) #determinant
Traceback (most recent call last):
File "C:\Users\canat\.spyder-py3\temp.py", line 20, in <module>
print (np.linalg.det(wronskian) )
File "<__array_function__ internals>", line 5, in det
File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\linalg\linalg.py", line 2159, in det
r = _umath_linalg.det(a, signature=signature)
TypeError: No loop matching the specified signature and casting was found for ufunc det
It is that simple:
import sympy as sp
x = sp.Symbol('x')
e=sp.exp(-3*x) #inputs
f=sp.cos(2*x)
g=sp.sin(2*x)
buneya=[e,f,g]
a=sp.diff(buneya[0]) #first derivative
b=sp.diff(buneya[1])
c=sp.diff(buneya[2])
k=sp.diff(a)
l=sp.diff(b) #second derivative
m=sp.diff(c)
wronskian=sp.Matrix([[e,f,g],[a,b,c],[k,l,m]]).det()
print(wronskian)
returns
26*exp(-3*x)*sin(2*x)**2 + 26*exp(-3*x)*cos(2*x)**2
see sympy matrices docs for further info.
EDIT #1
And even simpler!
import sympy as sp
from sympy.matrices import dense
x = sp.Symbol('x')
e=sp.exp(-3*x) #inputs
f=sp.cos(2*x)
g=sp.sin(2*x)
wronskian = dense.wronskian([e,f,g],x)
print(wronskian)
returns the same
26*exp(-3*x)*sin(2*x)**2 + 26*exp(-3*x)*cos(2*x)**2
wronskian method is also documented in sympy docs.
While trying to create an example with scipy.optimize curve_fit I found that scipy seems to be incompatible with Python's math module. While function f1 works fine, f2 throws an error message.
from scipy.optimize import curve_fit
from math import sin, pi, log, exp, floor, fabs, pow
x_axis = np.asarray([pi * i / 6 for i in range(-6, 7)])
y_axis = np.asarray([sin(i) for i in x_axis])
def f1(x, m, n):
return m * x + n
coeff1, mat = curve_fit(f1, x_axis, y_axis)
print(coeff1)
def f2(x, m, n):
return m * sin(x) + n
coeff2, mat = curve_fit(f2, x_axis, y_axis)
print(coeff2)
The full traceback is
Traceback (most recent call last):
File "/Documents/Programming/Eclipse/PythonDevFiles/so_test.py", line 49, in <module>
coeff2, mat = curve_fit(f2, x_axis, y_axis)
File "/usr/local/lib/python3.5/dist-packages/scipy/optimize/minpack.py", line 742, in curve_fit
res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/scipy/optimize/minpack.py", line 377, in leastsq
shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
File "/usr/local/lib/python3.5/dist-packages/scipy/optimize/minpack.py", line 26, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
File "/usr/local/lib/python3.5/dist-packages/scipy/optimize/minpack.py", line 454, in func_wrapped
return func(xdata, *params) - ydata
File "/Documents/Programming/Eclipse/PythonDevFiles/so_test.py", line 47, in f2
return m * sin(x) + n
TypeError: only length-1 arrays can be converted to Python scalars
The error message appears with lists and numpy arrays as input alike. It affects all math functions, I tested (see functions in import) and must have something to do with, how the math module manipulates input data. This is most obvious with pow() function - if I don't import this function from math, curve_fit works properly with pow().
The obvious question - why does this happen and how can math functions be used with curve_fit?
P.S.: Please don't discuss, that one shouldn't fit the sample data with a linear fit. This was just chosen to illustrate the problem.
Be careful with numpy-arrays, operations working on arrays and operations working on scalars!
Scipy optimize assumes the input (initial-point) to be a 1d-array and often things go wrong in other cases (a list for example becomes an array and if you assumed to work on lists, things go havoc; those kind of problems are common here on StackOverflow and debugging is not that easy to do by the eye; code-interaction helps!).
import numpy as np
import math
x = np.ones(1)
np.sin(x)
> array([0.84147098])
math.sin(x)
> 0.8414709848078965 # this only works as numpy has dedicated support
# as indicated by the error-msg below!
x = np.ones(2)
np.sin(x)
> array([0.84147098, 0.84147098])
math.sin(x)
> TypeError: only size-1 arrays can be converted to Python scalars
To be honest: this is part of a very basic understanding of numpy and should be understood when using scipy's somewhat sensitive functions.
I’m having trouble using the bisect optimizer within scipy. Here are the relevant portions of my code:
How I’m importing things
import numpy as np
import scipy.optimize as sp
import matplotlib.pyplot as plt
Break in code, section causing errors below
#All variables are previously defined except for h
def BeamHeight(h):
x = 1000e3*M[i]*h/(fw*h^3-(fw-wt)(h-2*ft)^3) - Max_stress_steel
return x
for i in range(0,50):
h = np.zeros((50))
h[i] = sp.bisect(BeamHeight, hb, 5,xtol = 0.001)
Causing this error:
Traceback (most recent call last):
File "ShearMoment.py", line 63, in <module>
h[i] = sp.bisect(BeamHeight, hb, 5,xtol = 0.001)
File "/usr/lib/python2.7/dist-packages/scipy/optimize/zeros.py", line 248, in bisect
r = _zeros._bisect(f,a,b,xtol,rtol,maxiter,args,full_output,disp)
File "ShearMoment.py", line 58, in BeamHeight
x = 1000e3*M[i]*h/(fw*h^3-(fw-wt)(h-2*ft)^3) - Max_stress_steel
TypeError: 'float' object is not callable
I understand that scipy.optimize expects a function as one of its arguments. Am I doing this incorrectly?
In Python, concatenation is not implicitly multiplication, and ^ is not exponentiation. Multiplication must be made explicit with *, and exponentiation must be written as **. This part of BeamHeight:
fw*h^3-(fw-wt)(h-2*ft)^3
must be written as
fw*h**3-(fw-wt)*(h-2*ft)**3
I have following codes to plot a gassian-2d contour,but I get this Error:
Traceback (most recent call last):
File "question.py", line 15, in <module>
Z0=gaussian_2d(X0,Y0,2,3,cov)
File "question.py", line 4, in gaussian_2d
return exp(-0.5*mat([x-x0,y-y0])*sigmaMatrix.I*mat([x-x0,y-y0]).T)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/matrixlib/defmatrix.py", line 96, in asmatrix
return matrix(data, dtype=dtype, copy=False)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/matrixlib/defmatrix.py", line 272, in __new__
raise ValueError("matrix must be 2-dimensional")
ValueError: matrix must be 2-dimensional
This my code:
import matplotlib.pyplot as plt
from numpy import *
def gaussian_2d(x, y, x0, y0, sigmaMatrix):
return exp(-0.5*mat([x-x0,y-y0])*sigmaMatrix.I*mat([x-x0,y-y0]).T)
cov=mat([[1,0],[0,2]])
delta=0.025
xgrid=arange(-2, 6, delta)
ygrid=arange(-2, 6, delta)
X0, Y0 = meshgrid(xgrid, ygrid)
Z0=gaussian_2d(X0,Y0,2,3,cov)
Can anyone tell me what am i doing wrong here?
Error starts from the concatenation but also your matrix dimensions don't match.
You are performing a quadratic form multiplication x^T A x but your meshgrid variables are square matrices of 320x320. Your A matrix cov is 2x2 hence you get an error.
If the sizes matches you can column concatenate mat(c_[x-x0,y-y0]) or use any other stacking option.
I was trying to fit a specific function with scipy and I got weird results. I decided to test something I know the answer to so I created this:
from scipy.optimize import curve_fit as cf
import numpy as np
import random
def func(x,a):
return a+X
X =[]
for i in range (10):
V = random.random()
X.append(i+3 + V/10)
print cf(func, np.array(range(10)),np.array(X))
I expected to get something around 3, nevertheless, here the output:
(array([ -2.18158824e-12]), inf)
As a side note, I tried to see what I send something to func and I got this:
print func(np.array(range(10)),3)
Traceback (most recent call last):
File "/tmp/py1759O-P", line 16, in <module>
print func(np.array(range(10)),3)
File "/tmp/py1759O-P", line 6, in func
return a+X
TypeError: unsupported operand type(s) for +: 'int' and 'list
What am I doing wrong?
Don't use x and X as variable names when they carry such different meanings (or perhaps you didn't know Python is case sensitive?):
def func(x,a):
return a+X
X =[]
x is a numpy array, X is a list, and a is a scalar parameter value.
a+X results in an error since you can not add a scalar to a list.
In func, the argument is x, but X is used in the body of the function.
Here's a modified version of your code. It uses a few more features of numpy (e.g. np.random.random() instead of random.random()).
from scipy.optimize import curve_fit as cf
import numpy as np
def func(x, a):
return a + x
n = 10
xdata = np.arange(n)
ydata = func(xdata, 3) + np.random.random(n) / 10
print cf(func, xdata, ydata)
The output is
(array([ 3.04734293]), array([[ 8.19208558e-05]]))