Skip to content

Instantly share code, notes, and snippets.

@amkatrutsa
Created April 16, 2015 08:20
Show Gist options
  • Save amkatrutsa/d4931f9b6b8cd5e2c0ae to your computer and use it in GitHub Desktop.
Save amkatrutsa/d4931f9b6b8cd5e2c0ae to your computer and use it in GitHub Desktop.
k = 256 #number of clusters
c = 4 #number of subspaces
R = np.eye(m, m)
opq_codes = np.zeros((n, c))
opq_centroids = np.zeros((k, m/c, c))
iterations = 30
for it in xrange(iterations):
print "Num iter", it
mod_X = np.dot(X, R)
opq_Y = np.zeros((n, m))
for i in xrange(c):
centroid, label = vq.kmeans2(mod_X[:, dim[i]:dim[i+1]], k, iter=1)
opq_codes[:, i] = label
opq_centroids[:, :, i] = centroid
opq_Y[:, dim[i]:dim[i+1]] = centroid[label, :]
mat_to_svd = np.dot(X.T, opq_Y)
U, S, V = scipy.linalg.svd(mat_to_svd)
R = np.dot(U, V)
def opq_retrieval(query):
q = X[query,:].reshape((1, X.shape[1])).dot(R)
distance = np.zeros((k, c))
dist = np.zeros((n, ))
for i in xrange(c):
part_q = q[:, dim[i]:dim[i+1]]
distance[:, i] = cdist(part_q, opq_centroids[:, :, i])
for j in xrange(n):
dist[j] = np.sum(distance[opq_codes[j, :].astype(int), :])
indices = np.argsort(dist)
return indices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment