I have some code that I need help vectorizing. I want to convert the following to vector form, how can I? I want to get rid of the inner loop - apparently, it's possible to do so. X is an NxD matrix. y is a 1xD vector.
def foo(X, y, mylambda, N, D, epsilon):
...
for j in xrange(D):
aj = 0
cj = 0
for i in xrange(N):
aj += 2 * (X[i,j] ** 2)
cj += 2 * (X[i,j] * (y[i] - w.transpose()*X[i].transpose() + w[j]*X[i,j]))
...
If I call numpy.vectorize() on the function, it throws an error at runtime.
Complete code:
import scipy
import scipy.io
import numpy
from numpy import linalg
from scipy import *
def data(N, d, k, sigma, seed=12231):
random.seed(seed)
X = randn(N, d)
wg = zeros(1 + d)
wg[1:k + 1] = 10 * sign(randn(k))
eps = randn(N) * sigma
y = X.dot(wg[1:]) + wg[0] + eps
return (y, X)
def foo(X, y, mylambda, n, D, epsilon):
identityMatrix = numpy.matrix(numpy.identity(D))
w = (X.transpose() * X + mylambda * identityMatrix).getI() * X.transpose() * y
newweight = (X.transpose() * X + mylambda * identityMatrix).getI() * X.transpose() * y
iterate = 1
iteration = 0
while iterate > 0 and iteration < 10000:
iteration += 1
iterate = 0
maxerror = 0
for j in xrange(D):
aj = 0
cj = 0
for i in xrange(n):
aj += 2 * (X[i,j] ** 2)
cj += 2 * (X[i,j] * (y[i] - w.transpose()*X[i].transpose() + w[j]*X[i,j]))
if cj < -mylambda:
newweight[j,0] = (cj + mylambda)/ aj
elif cj > mylambda:
newweight[j,0] = (cj - mylambda)/ aj
else:
newweight[j,0] = 0
if abs(newweight[j,0] - w[j,0]) > epsilon:
iterate += 1
if abs(newweight[j,0] - w[j,0]) > maxerror:
maxerror = abs(newweight[j,0] - w[j,0])
w[j,0] = newweight[j,0]
N, D, k = 50, 75, 5
(y, X) = data(N, D, k, 1, 123)
X = numpy.matrix(X)
y = numpy.matrix(y).transpose()
foo(X, y, 1, N, D, 0.1)
a[j] = 0anda[j] += 2 * (X[i,j] **2)(and a similar change ofbjtob[j]? Otherwise, in the outer loop you overwriteajwithout ever using it.w, what's the size?