Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def knn_initialize(X, missing_mask, verbose=False):
"""
Fill X with NaN values if necessary, construct the n_samples x n_samples
distance matrix and set the self-distance of each row to infinity.
"""
X_row_major = X.copy("C")
if missing_mask.sum() != np.isnan(X_row_major).sum():
# if the missing values have already been zero-filled need
# to put NaN's back in the data matrix for the distances function
X_row_major[missing_mask] = np.nan
D = all_pairs_normalized_distances(X_row_major, verbose=verbose)
# set diagonal of distance matrix to infinity since we don't want
# points considering themselves as neighbors
for i in range(X.shape[0]):
D[i, i] = np.inf
return X_row_major, D