Keep getting a only size-1 arrays can be converted to Python scalars

361 Views Asked by At

Ive looked multiple times on google as well as on this website as well and I am still unable to solve my problem. I have taken feedback from a previous question and decided to use a numerical bisection method however, when run the code keeps giving me this error. The code is pretty straightforward but I am basically trying to find out the EC10 and EC90 values for three different functions.

My code is:

# Imports
import numpy as np
from numpy import log as ln
from matplotlib import pyplot as plt
from random import randint
from mpmath import *
import sympy as sym


# Params
u = 10
c1 = randint(1,u)
n1 = randint(1,u)
k1 = randint(1,u)

c2 = randint(1,u)
n2 = randint(1,u)
k2 = randint(1,u)

print(f"c1 = {c1}")
print(f"n1 = {n1}")
print(f"k1 = {k1}")

print('{}(x^{})/({}+x^{})'.format(c1,n1,k1,n1))

print(f"c2 = {c2}")
print(f"n2 = {n2}")
print(f"k2 = {k2}")

print('{}(x^{})/({}+x^{})'.format(c2,n2,k2,n2))

mp.dps = 15
mp.pretty = False

# figure layout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Hill Function for f(x) and g(x) and f(g(x))
def f(x):
    return c1*(x**n1)/(k1+x**n1)
def g(x):
    return c2*(x**n2)/(k2+x**n2)
def c(x):
    return ((c1*x**(n2*n1))*(c2**n1))/((k2*(k2+(x**n2))**n1)+((c2**n1)*(x**(n2*n1))))

# EC finder
def BisectionEC10(fa,a,b):
    c=1
    x = float(np.linspace(a,b,1000))
    ystar = 0.10*(fa(x).max()-fa(x).min())
    while abs(fa(c)-ystar)<0.000000001:
        c=(a+b)/2
        if fa(c)-ystar<0:
            a=c
        elif fa(c)-ystar>0:
            b=c
    #print('The EC10 of the function is: ',"{0:.15f}".format(c))
    #print('Output of the function when evaluated at the EC10: ',fa(c))
    return c


def BisectionEC90(fa,a,b):
    c=1
    x = float(np.linspace(a,b,1000))
    ystar= 0.10*(fa(x).max()-fa(x).min())
    while abs(fa(c)-ystar)<0.000000001:
        c=(a+b)/2
        if fa(c)-ystar<0:
            a=c
        elif fa(c)-ystar>0:
            b=c
    #print('The EC90 of the function is: ',"{0:.15f}".format(c))
    #print('Output of the function when evaluated at the EC90: ',fa(c))
    return c



# EC90 and EC10 for f(x), g(x) and f(g(x))
up = 20
lo = 0
#x = np.linspace[lo,up,1000]
x = 1
#x = sym.symbols('x')

EC90_1 = BisectionEC90(f, lo, up)
EC10_1 = BisectionEC10(f, lo, up)

EC90_2 = BisectionEC90(g, lo, up)
EC10_2 = BisectionEC10(g, lo, up)

EC90_3 = BisectionEC90(c, lo, up)
EC10_3 = BisectionEC10(c, lo, up)

# Hill Coefficient for f(x) and g(x)
H_1 = ln(81)/(ln(EC90_1/EC10_1))
H_2 = ln(81)/(ln(EC90_2/EC10_2))
H_3 = ln(81)/(ln(EC90_3/EC10_3))
print(f"Hill's Coefficient for f(x) = {H_1}")
print(f"Hill's Coefficient for g(x) = {H_2}")
print(f"Hill's Coefficient for f(g(x)) = {H_3}")

# Plotting
plt.plot(x, f(x), color = 'red')
plt.plot(x, g(x), color = 'blue')
plt.plot(x, c(x), color = 'green')
plt.show()

My errors are:

Traceback (most recent call last):
  File "/Users/*****/Documents/Python/NumericBisectionMethod/main.py", line 86, in <module>
    EC90_1 = BisectionEC90(f, lo, up)
  File "/Users/*****/Documents/Python/NumericBisectionMethod/main.py", line 65, in BisectionEC90
    x = float(np.linspace(a,b,1000))
TypeError: only size-1 arrays can be converted to Python scalars
1

There are 1 best solutions below

0
Frank Yellin On

The float function cannot be applied to numpy arrays.

As it is, the array returned by np.linspace is already an array of doubles, so you don't need to convert it. If you ever do need to convert an array from one type to another, use arr.astype(float) or whatever.