Skip to content

Instantly share code, notes, and snippets.

@ychennay
Created June 18, 2018 05:20
Show Gist options
  • Save ychennay/85d458999e3c81c82f5c043bfe408c87 to your computer and use it in GitHub Desktop.
Save ychennay/85d458999e3c81c82f5c043bfe408c87 to your computer and use it in GitHub Desktop.
import numpy as np
import time
from sklearn.metrics.pairwise import rbf_kernel
x = np.random.rand(10000)
x = x.reshape(100,100)
distances = []
γ = -.5
start = time.time()
for i in x:
for j in x:
distances.append(np.exp(γ * np.linalg.norm(i - j) ** 2))
np.array(distances).reshape(len(x),len(x))
end = time.time()
time1 = end - start
print(f"Runtime using for loops: {time1}")
start2 = time.time()
rbf_kernel(x, gamma= .5)
end2 = time.time()
time2 = end2 - start2
print(f"Runtime using vectorized matrix operations: {time2}")
# Runtime using for loops: 0.120
# Runtime using vectorized matrix operations: 0.009
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment