1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
# cython: profile=True
# Profiling is enabled by default as the overhead does not seem to be measurable
# on this specific use case.
# Author: Peter Prettenhofer <peter.prettenhofer@gmail.com>
# Olivier Grisel <olivier.grisel@ensta.org>
#
# License: BSD Style.
import numpy as np
from ..utils.extmath import norm
cimport numpy as np
cimport cython
ctypedef np.float64_t DOUBLE
ctypedef np.int32_t INT
cdef extern from "math.h":
double sqrt(double f)
cdef extern from "cblas.h":
double ddot "cblas_ddot"(int N, double *X, int incX, double *Y, int incY)
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cpdef DOUBLE _assign_labels_array(np.ndarray[DOUBLE, ndim=2] X,
np.ndarray[DOUBLE, ndim=1] x_squared_norms,
np.ndarray[DOUBLE, ndim=2] centers,
np.ndarray[INT, ndim=1] labels,
np.ndarray[DOUBLE, ndim=1] distances):
"""Compute label assignement and inertia for a dense array
Return the inertia (sum of squared distances to the centers).
"""
cdef:
unsigned int n_clusters = centers.shape[0]
unsigned int n_features = centers.shape[1]
unsigned int n_samples = X.shape[0]
unsigned int sample_idx, center_idx, feature_idx
unsigned int store_distances = 0
unsigned int k
DOUBLE inertia = 0.0
DOUBLE min_dist
DOUBLE dist
np.ndarray[DOUBLE, ndim=1] center_squared_norms = np.zeros(
n_clusters, dtype=np.float64)
if n_samples == distances.shape[0]:
store_distances = 1
for center_idx in range(n_clusters):
center_squared_norms[center_idx] = ddot(
n_features, ¢ers[center_idx, 0], 1, ¢ers[center_idx, 0], 1)
for sample_idx in range(n_samples):
min_dist = -1
for center_idx in range(n_clusters):
dist = 0.0
# hardcoded: minimize euclidean distance to cluster center:
# ||a - b||^2 = ||a||^2 + ||b||^2 -2 <a, b>
dist += ddot(n_features, &X[sample_idx, 0], 1,
¢ers[center_idx, 0], 1)
dist *= -2
dist += center_squared_norms[center_idx]
dist += x_squared_norms[sample_idx]
if min_dist == -1 or dist < min_dist:
min_dist = dist
labels[sample_idx] = center_idx
if store_distances:
distances[sample_idx] = min_dist
inertia += min_dist
return inertia
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cpdef DOUBLE _assign_labels_csr(X, np.ndarray[DOUBLE, ndim=1] x_squared_norms,
np.ndarray[DOUBLE, ndim=2] centers,
np.ndarray[INT, ndim=1] labels,
np.ndarray[DOUBLE, ndim=1] distances):
"""Compute label assignement and inertia for a CSR input
Return the inertia (sum of squared distances to the centers).
"""
cdef:
np.ndarray[DOUBLE, ndim=1] X_data = X.data
np.ndarray[INT, ndim=1] X_indices = X.indices
np.ndarray[INT, ndim=1] X_indptr = X.indptr
unsigned int n_clusters = centers.shape[0]
unsigned int n_features = centers.shape[1]
unsigned int n_samples = X.shape[0]
unsigned int store_distances = 0
unsigned int sample_idx, center_idx, feature_idx
unsigned int k
DOUBLE inertia = 0.0
DOUBLE min_dist
DOUBLE dist
np.ndarray[DOUBLE, ndim=1] center_squared_norms = np.zeros(
n_clusters, dtype=np.float64)
if n_samples == distances.shape[0]:
store_distances = 1
for center_idx in range(n_clusters):
center_squared_norms[center_idx] = ddot(
n_features, ¢ers[center_idx, 0], 1, ¢ers[center_idx, 0], 1)
for sample_idx in range(n_samples):
min_dist = -1
for center_idx in range(n_clusters):
dist = 0.0
# hardcoded: minimize euclidean distance to cluster center:
# ||a - b||^2 = ||a||^2 + ||b||^2 -2 <a, b>
for k in range(X_indptr[sample_idx], X_indptr[sample_idx + 1]):
dist += centers[center_idx, X_indices[k]] * X_data[k]
dist *= -2
dist += center_squared_norms[center_idx]
dist += x_squared_norms[sample_idx]
if min_dist == -1 or dist < min_dist:
min_dist = dist
labels[sample_idx] = center_idx
if store_distances:
distances[sample_idx] = dist
inertia += min_dist
return inertia
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def _mini_batch_update_csr(X, np.ndarray[DOUBLE, ndim=1] x_squared_norms,
np.ndarray[DOUBLE, ndim=2] centers,
np.ndarray[INT, ndim=1] counts,
np.ndarray[INT, ndim=1] nearest_center,
np.ndarray[DOUBLE, ndim=1] old_center,
int compute_squared_diff):
"""Incremental update of the centers for sparse MiniBatchKMeans.
Parameters
----------
X: CSR matrix, dtype float64
The complete (pre allocated) training set as a CSR matrix.
centers: array, shape (n_clusters, n_features)
The cluster centers
counts: array, shape (n_clusters,)
The vector in which we keep track of the numbers of elements in a
cluster
Returns
-------
inertia: float
The inertia of the batch prior to centers update, i.e. the sum
distances to the closest center for each sample. This is the objective
function being minimized by the k-means algorithm.
squared_diff: float
The sum of squared update (squared norm of the centers position
change). If compute_squared_diff is 0, this computation is skipped and
0.0 is returned instead.
Both squared diff and inertia are commonly used to monitor the convergence
of the algorithm.
"""
cdef:
np.ndarray[DOUBLE, ndim=1] X_data = X.data
np.ndarray[int, ndim=1] X_indices = X.indices
np.ndarray[int, ndim=1] X_indptr = X.indptr
unsigned int n_samples = X.shape[0]
unsigned int n_clusters = centers.shape[0]
unsigned int n_features = centers.shape[1]
unsigned int sample_idx, center_idx, feature_idx
unsigned int k
int old_count, new_count
DOUBLE center_diff
DOUBLE squared_diff = 0.0
# move centers to the mean of both old and newly assigned samples
for center_idx in range(n_clusters):
old_count = counts[center_idx]
new_count = old_count
# count the number of samples assigned to this center
for sample_idx in range(n_samples):
if nearest_center[sample_idx] == center_idx:
new_count += 1
if new_count == old_count:
# no new sample: leave this center as it stands
continue
# rescale the old center to reflect it previous accumulated
# weight w.r.t. the new data that will be incrementally contributed
if compute_squared_diff:
old_center[:] = centers[center_idx]
centers[center_idx] *= old_count
# iterate of over samples assigned to this cluster to move the center
# location by inplace summation
for sample_idx in range(n_samples):
if nearest_center[sample_idx] != center_idx:
continue
# inplace sum with new samples that are members of this cluster
# and update of the incremental squared difference update of the
# center position
for k in range(X_indptr[sample_idx], X_indptr[sample_idx + 1]):
centers[center_idx, X_indices[k]] += X_data[k]
# inplace rescale center with updated count
if new_count > old_count:
# update the count statistics for this center
counts[center_idx] = new_count
# re-scale the updated center with the total new counts
centers[center_idx] /= new_count
# update the incremental computation of the squared total
# centers position change
if compute_squared_diff:
for feature_idx in range(n_features):
squared_diff += (old_center[feature_idx]
- centers[center_idx, feature_idx]) ** 2
return squared_diff
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def csr_row_norm_l2(X, squared=True):
"""Get L2 norm of each row in CSR matrix X.
TODO: refactor me in the sklearn.utils.sparsefuncs module once the CSR
sklearn.preprocessing.Scaler has been refactored as well.
"""
cdef:
unsigned int n_samples = X.shape[0]
unsigned int n_features = X.shape[1]
np.ndarray[DOUBLE, ndim=1] norms = np.zeros((n_samples,),
dtype=np.float64)
np.ndarray[DOUBLE, ndim=1] X_data = X.data
np.ndarray[int, ndim=1] X_indices = X.indices
np.ndarray[int, ndim=1] X_indptr = X.indptr
unsigned int i
unsigned int j
double sum_
int with_sqrt = not squared
for i in range(n_samples):
sum_ = 0.0
for j in range(X_indptr[i], X_indptr[i + 1]):
sum_ += X_data[j] * X_data[j]
if with_sqrt:
sum_ = sqrt(sum_)
norms[i] = sum_
return norms
|