I have coded a kriging algorithm but I find it quite slow. Especially, do you have an idea on how I could vectorise the piece of code in the cons function below:
import time
import numpy as np
B = np.zeros((200, 6))
P = np.zeros((len(B), len(B)))
def cons():
time1=time.time()
for i in range(len(B)):
for j in range(len(B)):
P[i,j] = corr(B[i], B[j])
time2=time.time()
return time2-time1
def corr(x,x_i):
return np.exp(-np.sum(np.abs(np.array(x) - np.array(x_i))))
time_av = 0.
for i in range(30):
time_av+=cons()
print "Average=", time_av/100.
Edit: Bonus questions
- What happens to the broadcasting solution if I want
corr(B[i], C[j])with C the same dimension than B What happens to the scipy solution if my p-norm orders are an array:
p=np.array([1.,2.,1.,2.,1.,2.]) def corr(x, x_i): return np.exp(-np.sum(np.abs(np.array(x) - np.array(x_i))**p))For 2., I tried
P = np.exp(-cdist(B, C,'minkowski', p))but scipy is expecting a scalar.