argmax, argmin

In [2]:
from numpy import *
In [3]:
z = array([3,4,5,4,5,2])
z
Out[3]:
array([3, 4, 5, 4, 5, 2])
In [4]:
z.argmax()
Out[4]:
2

Mathematically, the argmax is a set.

In numpy argmax() returns just the first element of this set.

In [5]:
z.argmin()
Out[5]:
5

Let's generate some data to try our formula out on.

In [6]:
from numpy import *
%matplotlib inline
import matplotlib.pyplot as plt
In [7]:
n = 20
x = random.rand(n)
y = 3*x - 5
plt.scatter(x,y);

This data is perfectly linear. So our formula should give alpha=3, beta=5 exactly.

In [8]:
x.mean()
x
Out[8]:
array([ 0.7373587 ,  0.59939227,  0.04091788,  0.72995711,  0.62153767,
        0.2267084 ,  0.04206701,  0.34482642,  0.46026394,  0.39644541,
        0.66614185,  0.56589274,  0.78291356,  0.89741237,  0.10799515,
        0.77836509,  0.06089373,  0.40709861,  0.7233188 ,  0.37920816])
In [9]:
x*x
Out[9]:
array([ 0.54369786,  0.35927109,  0.00167427,  0.53283738,  0.38630908,
        0.0513967 ,  0.00176963,  0.11890526,  0.2118429 ,  0.15716897,
        0.44374497,  0.32023459,  0.61295364,  0.80534897,  0.01166295,
        0.60585222,  0.00370805,  0.16572928,  0.52319009,  0.14379883])
In [10]:
def best2(x,y):
    xbar = x.mean()
    ybar = y.mean()
    xybar = (x*y).mean()
    x2bar = (x*x).mean()
    alpha = (xybar-xbar*ybar)/(x2bar-xbar**2)
    beta = ybar - xbar*alpha
    return alpha,beta
In [11]:
best2(x,y)
Out[11]:
(2.9999999999999969, -4.9999999999999982)
In [12]:
a,b = best2(x,y)
print(a,b)
3.0 -5.0
In [13]:
n = 20
x = random.rand(n)
y = 3*x - 5
plt.scatter(x,y)
alpha,beta = best2(x,y)
xx = array([0,1])
plt.plot(xx,alpha*xx+beta,'r');

Now add some noise to y

In [14]:
n = 30
x = random.rand(n)
y = 3*x - 5 + 0.1*random.randn(n)
plt.scatter(x,y)
alpha,beta = best2(x,y)
xx = array([0,1])
plt.plot(xx,alpha*xx+beta,'r');

zip - a useful function for zipping together multiple lists, arrays, etc.

In [15]:
A = [1,2,3]
B = [10,100,1000]
for item in zip(A,B):
    print(item)
(1, 10)
(2, 100)
(3, 1000)
In [16]:
A = [1,2,3]
B = [10,100,1000]
for a,b in zip(A,B):
    print(a,b)
1 10
2 100
3 1000
In [ ]:
p = 15
A = random.rand(p)*6
B = random.rand(p)*-10
for alpha,beta in zip(A,B):
    print(alpha,beta)
    e = # array of pointwise errors
    # sum e to get overall E