def parakeet_update_centroids(X, assignments, k): d = X.shape[1] new_centroids = np.zeros((k,d), dtype=X.dtype) for i in xrange(k): mask = (assignments == i) count = np.sum(mask) assigned_data = X[mask] if count == 1: new_centroids[i, :] = assigned_data[0] elif count > 1: new_centroids[i,:] = parakeet.mean(assigned_data) return new_centroids
def test_mean(): x = np.random.randn(1000) true_mean = np.mean(x) parakeet_mean = par.mean(x) print "Numpy mean = %s, Parakeet mean = %s" % (true_mean, parakeet_mean) assert abs(true_mean - parakeet_mean) < 0.00001
def calc_centroid(X, a, i): return par.mean(X[a == i], axis=0)