def test_tensor_input(): X = np.random.normal(size=(100, 5, 5)) mds = ClassicalMDS(n_components=3, dissimilarity="euclidean") mds.fit(X) assert_equal(mds.dissimilarity_matrix_.shape, (100, 100)) X_transformed = mds.fit_transform(X) assert_equal(X_transformed.shape, (100, 3))
def use_fit(): A = np.ones((4, 4)) - np.identity(4) mds = ClassicalMDS(n_components=3, dissimilarity="precomputed") mds.fit(A) B = np.dot(mds.components_, np.diag(mds.singular_values_)) Ahat = _compute_dissimilarity(B) # Checks up to 7 decimal points assert_almost_equal(A, Ahat)
def test_input(): X = np.random.normal(0, 1, size=(10, 3)) # X cannot be tensor when precomputed dissimilarity with pytest.raises(ValueError): tensor = np.random.normal(0, 1, size=(10, 3, 3)) mds = ClassicalMDS(n_components=3, dissimilarity="precomputed") mds.fit(tensor) with pytest.raises(ValueError): one_dimensional = np.random.normal(size=10) mds = ClassicalMDS(n_components=2, dissimilarity="euclidean") mds.fit(one_dimensional) # n_components > n_samples with pytest.raises(ValueError): mds = ClassicalMDS(n_components=100) mds.fit(X) # Invalid n_components with pytest.raises(ValueError): mds = ClassicalMDS(n_components=-2) with pytest.raises(TypeError): mds = ClassicalMDS(n_components="1") # Invalid dissimilarity with pytest.raises(ValueError): mds = ClassicalMDS(dissimilarity="abc") # Invalid input for fit function with pytest.raises(ValueError): mds = ClassicalMDS(n_components=3, dissimilarity="precomputed") mds.fit(X="bad_input") # Must be square and symmetric matrix if precomputed dissimilarity with pytest.raises(ValueError): mds = ClassicalMDS(n_components=3, dissimilarity="precomputed") mds.fit(X)