Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def predict_coarse(self, x):
"""
Compute the coarse codes for a datapoint.
:param ndarray x:
the point to code
:returns tuple:
a tuple of coarse codes
"""
return tuple([predict_cluster(cx, self.Cs[split]) for cx, split in iterate_splits(x, self.num_coarse_splits)])
:returns tuple:
a tuple of fine codes
"""
if coarse_codes is None:
coarse_codes = self.predict_coarse(x)
px = self.project(x, coarse_codes)
fine_codes = []
for cx, split in iterate_splits(px, self.num_coarse_splits):
# Get product quantizer parameters for this split
_, _, _, subC = self.get_split_parameters(split)
# Compute subquantizer codes
fine_codes += [predict_cluster(fx, subC[sub_split]) for fx, sub_split in iterate_splits(cx, self.num_fine_splits)]
return tuple(fine_codes)
N = data.shape[0]
D = data.shape[1]
# Essential variables
A = np.zeros((V, D, D)) # accumulators for covariance estimator per cluster
mu = np.zeros((V, D)) # residual means
count = np.zeros(V, dtype=int) # count of points per cluster
assignments = np.zeros(N, dtype=int) # point cluster assignments
residuals = np.zeros((N, D)) # residual for data points given cluster assignment
# Iterate data points, accumulate estimators
for i in xrange(N):
d = data[i]
# Find cluster assignment and residual
cluster = predict_cluster(d, C)
centroid = C[cluster]
residual = d - centroid
assignments[i] = cluster
# Accumulate estimators for covariance matrix for the assigned cluster
mu[cluster] += residual
count[cluster] += 1
A[cluster] += np.outer(residual, residual)
residuals[i] = residual
return A, mu, count, assignments, residuals
def predict_coarse(self, x):
"""
Compute the coarse codes for a datapoint.
:param ndarray x:
the point to code
:returns tuple:
a tuple of coarse codes
"""
return tuple([predict_cluster(cx, self.Cs[split]) for cx, split in iterate_splits(x, self.num_coarse_splits)])
def compute_residuals(data, C):
assignments = np.apply_along_axis(predict_cluster, 1, data, C)
residuals = data - C[assignments]
return residuals, assignments