# How does the inertia cython implementation in scikit-learn for kmeans work? [closed]

Specifically, what do the & symbol stand for? and why is the column index always 0?

    cpdef floating _inertia_dense(
np.ndarray[floating, ndim=2, mode='c'] X,  # IN
floating[::1] sample_weight,               # IN
floating[:, ::1] centers,                  # IN
int[::1] labels):                          # IN
"""Compute inertia for dense input data
Sum of squared distance between each sample and its assigned center.
"""
cdef:
int n_samples = X.shape[0]
int n_features = X.shape[1]
int i, j

floating sq_dist = 0.0
floating inertia = 0.0

for i in range(n_samples):
j = labels[i]
sq_dist = _euclidean_dense_dense(&X[i, 0], &centers[j, 0],
n_features, True)
inertia += sq_dist * sample_weight[i]

return inertia



& is the "address-of" operator in c, and that appears to be how it's being used here. See these two SO posts.

Note the signature of _euclidean_dense_dense:

cdef floating _euclidean_dense_dense(
floating* a, # IN
floating* b, # IN
int n_features,
bint squared) nogil:


The first two inputs are pointers. So you need to pass the address, not a copy of the data.

Notice too that only the address of the element in the first column gets passed. If you look at the definition for _euclidean_dense_dense that becomes clearer: that function actually loops over the addresses of the rest of the columns in its computation.