[Numpy-discussion] Combining Sigmoid Curves

Christopher Barker Chris.Barker@noaa....
Fri May 2 15:24:01 CDT 2008

```Rich,

this could use some serious vectorization/numpyification! Poke around
the scipy Wiki and whatever other tutorials you can find -- you'll be

When you are writing a loop like:
>  	  for i in xL:
>  	    x.append(xL[i])

You should be doing array operations!

Specifics:
xL = nx.arange(ll,mid,0.1)

# you've now created an array of your X values
for i in xL:
x.append(xL[i])
#this dumps them into a list - why not keep them an array?
# if you really need an array (you don't here), then you can use:
xL.tolist()

That's why you get this error:
TypeError: unsupported operand type(s) for -: 'list' and 'float'

you're trying to do array operations on a list.

Also, you probably want np.linspace, rather than arange, it's a better
option for floats.

Here is my version:
import numpy as np

ll, lr, mid = (-10, 10, 0)

flexL = (mid - ll)/2.0
flexR = (lr - mid)/2.0
tau = (mid - ll)/10.0

xL = np.linspace(ll, mid, 5)
yL = 1.0 / (1.0 + np.exp(-(xL-flexL)/tau))

print xL
print yL

xR = np.linspace(mid, lr, 5)
yR = 1 - (1.0 / (1.0 + np.exp(-(xR-flexR)/tau)))

print xR
print yR

# now put them together:
x = np.hstack((xL, xR[1:])) # don't want to duplicate the midpoint
y = np.hstack((yL, yR[1:]))

print x
print y

Though it doesn't look like the numbers are right.

Also, you don't need to create the separate left and right arraysand put
them together, slicing gives a view, so you could do:

numpoints = 11 # should be an odd number to get the midpoint
x = linspace(ll,lr, numpoints)
y = zeros_like(x)

xL = x[:numpoints/2]
yL = y[:numpoints/2]

yL[:] = 1.0 / (1.0 + np.exp(-(xL-flexL)/tau))

you could also use something like:

np.where(x<0, .....)

left as an exercise for the reader...

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov
```