def test_float_precision(): km = KMeansConstrained(n_init=1, random_state=30) inertia = {} X_new = {} centers = {} for dtype in [np.float64, np.float32]: X_test = X.astype(dtype) km.fit(X_test) # dtype of cluster centers has to be the dtype of the input # data assert_equal(km.cluster_centers_.dtype, dtype) inertia[dtype] = km.inertia_ X_new[dtype] = km.transform(X_test) centers[dtype] = km.cluster_centers_ # ensure the extracted row is a 2d array assert_equal(km.predict(X_test[:1]), km.labels_[0]) if hasattr(km, 'partial_fit'): km.partial_fit(X_test[0:3]) # dtype of cluster centers has to stay the same after # partial_fit assert_equal(km.cluster_centers_.dtype, dtype) # compare arrays with low precision since the difference between # 32 and 64 bit sometimes makes a difference up to the 4th decimal # place assert_array_almost_equal(inertia[np.float32], inertia[np.float64], decimal=4) assert_array_almost_equal(X_new[np.float32], X_new[np.float64], decimal=4) assert_array_almost_equal(centers[np.float32], centers[np.float64], decimal=4)
def test_transform(): km = KMeansConstrained(n_clusters=n_clusters) km.fit(X) X_new = km.transform(km.cluster_centers_) for c in range(n_clusters): assert_equal(X_new[c, c], 0) for c2 in range(n_clusters): if c != c2: assert_greater(X_new[c, c2], 0)