def calculate_gap(data, clustering, km, B=<NUM_LIT:50>, **kwargs):
k = len(set(clustering))<EOL>Wk = km.inertia_<EOL>mins, maxes = calculate_bounding_box(data)<EOL>Wk_est = []<EOL>for i in range(B):<EOL><INDENT>data_sample = (maxes-mins)*np.random.random(data.shape) + mins<EOL>km_b = KMeans(k)<EOL>km_b.fit_predict(data_sample)<EOL>Wk_est.append(km_b.inertia_)<EOL><DEDENT>Wk_est = np.log(np.array(Wk_est))<EOL>Wk_mean = np.mean(Wk_est)<EOL>Wk_std = np.std(Wk_est)<EOL>gap = Wk_mean - np.log(Wk)<EOL>sk = np.sqrt(<NUM_LIT:1> + <NUM_LIT:1.0>/B)*Wk_std<EOL>return gap, sk<EOL>
See: https://datasciencelab.wordpress.com/2013/12/27/finding-the-k-in-k-means-clustering/

https://web.stanford.edu/~hastie/Papers/gap.pdf

Returns two results: the gap score, and s_k.