def test_fill_diagonal(): a = np.zeros((3, 3), int) fill_diagonal(a, 5) yield (assert_array_equal, a, np.array([[5, 0, 0], [0, 5, 0], [0, 0, 5]])) #Test tall matrix a = np.zeros((10, 3), int) fill_diagonal(a, 5) yield (assert_array_equal, a, np.array([[5, 0, 0], [0, 5, 0], [0, 0, 5], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])) #Test tall matrix wrap a = np.zeros((10, 3), int) fill_diagonal(a, 5, True) yield (assert_array_equal, a, np.array([[5, 0, 0], [0, 5, 0], [0, 0, 5], [0, 0, 0], [5, 0, 0], [0, 5, 0], [0, 0, 5], [0, 0, 0], [5, 0, 0], [0, 5, 0]])) #Test wide matrix a = np.zeros((3, 10), int) fill_diagonal(a, 5) yield (assert_array_equal, a, np.array([[5, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 5, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 5, 0, 0, 0, 0, 0, 0, 0]])) # The same function can operate on a 4-d array: a = np.zeros((3, 3, 3, 3), int) fill_diagonal(a, 4) i = np.array([0, 1, 2]) yield (assert_equal, np.where(a != 0), (i, i, i, i))
def test_tall_matrix_wrap(self): a = np.zeros((10, 3), int) fill_diagonal(a, 5, True) assert_array_equal( a, np.array([[5, 0, 0], [0, 5, 0], [0, 0, 5], [0, 0, 0], [5, 0, 0], [0, 5, 0], [0, 0, 5], [0, 0, 0], [5, 0, 0], [0, 5, 0]]))
def estimate_mmd(self, sample1, sample2, unbiased=False): """ Compute the MMD between two samples. Parameters ---------- sample1 : array-like matrix, shape=(n_samples1, n_features) sample2 : array-like matrix, shape=(n_samples2, n_features) unbiased : boolean, optional Default is False. Returns ------- estimated_mmd : float """ assert len(sample1.shape) == 2 assert len(sample2.shape) == 2 assert sample1.shape[1] == sample2.shape[1] K11 = self.kernel(sample1, sample1) K22 = self.kernel(sample2, sample2) K12 = self.kernel(sample1, sample2) if unbiased: fill_diagonal(K11, 0.0) fill_diagonal(K22, 0.0) n = float(shape(K11)[0]) m = float(shape(K22)[0]) return (np.sum(K11) / (n**2 - n) + np.sum(K22) / (m**2 - m) - 2 * np.mean(K12)) else: return np.mean(K11) + np.mean(K22) - 2 * np.mean(K12)
def test_basic(self): a = np.zeros((3, 3), int) fill_diagonal(a, 5) assert_array_equal( a, np.array([[5, 0, 0], [0, 5, 0], [0, 0, 5]]) )
def test_wide_matrix(self): a = np.zeros((3, 10), int) fill_diagonal(a, 5) assert_array_equal( a, np.array([[5, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 5, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 5, 0, 0, 0, 0, 0, 0, 0]]))
def test_wide_matrix(self): a = np.zeros((3, 10), int) fill_diagonal(a, 5) assert_array_equal( a, np.array([[5, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 5, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 5, 0, 0, 0, 0, 0, 0, 0]]) )
def unbiased_HSnorm_estimate_of_centred_operator(K): '''returns an unbiased estimate of \sum_r \lambda_r^2 where \lambda_r are the eigenvalues of the centred kernel operator''' m = shape(K)[0] fill_diagonal(K, 0.) first_term = sum(K**2) second_term = (sum(K)**2) / ((m - 1.) * (m - 2.)) third_term = -2. * sum(K.dot(K)) / (m - 2.) return (first_term + second_term + third_term) / (m * (m - 3.))
def test_tall_matrix_wrap(self): a = np.zeros((10, 3), int) fill_diagonal(a, 5, True) assert_array_equal( a, np.array([[5, 0, 0], [0, 5, 0], [0, 0, 5], [0, 0, 0], [5, 0, 0], [0, 5, 0], [0, 0, 5], [0, 0, 0], [5, 0, 0], [0, 5, 0]]) )
def estimateMMD(self,sample1,sample2,unbiased=False): """ Compute the MMD between two samples """ K11 = self.kernel(sample1,sample1) K22 = self.kernel(sample2,sample2) K12 = self.kernel(sample1,sample2) if unbiased: fill_diagonal(K11,0.0) fill_diagonal(K22,0.0) n=float(shape(K11)[0]) m=float(shape(K22)[0]) return sum(sum(K11))/(pow(n,2)-n) + sum(sum(K22))/(pow(m,2)-m) - 2*mean(K12[:]) else: return mean(K11[:])+mean(K22[:])-2*mean(K12[:])
def test_tall_matrix(self): a = np.zeros((10, 3), int) fill_diagonal(a, 5) assert_array_equal( a, np.array([ [5, 0, 0], [0, 5, 0], [0, 0, 5], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], ]), )
def test_hetero_shape_handling(self): # raise error with high dimensionality and # shape mismatch a = np.zeros((3, 3, 7, 3), int) with assert_raises_regex(ValueError, "equal length"): fill_diagonal(a, 2)
def test_low_dim_handling(self): # raise error with low dimensionality a = np.zeros(3, int) with assert_raises_regex(ValueError, "at least 2-d"): fill_diagonal(a, 5)
def test_operate_4d_array(self): a = np.zeros((3, 3, 3, 3), int) fill_diagonal(a, 4) i = np.array([0, 1, 2]) assert_equal(np.where(a != 0), (i, i, i, i))
def test_hetero_shape_handling(self): # raise error with high dimensionality and # shape mismatch a = np.zeros((3,3,7,3), int) with assert_raises_regex(ValueError, "equal length"): fill_diagonal(a, 2)