def test_sample_toy_fit_nonsparse_transform_sparse(): # Fit with non-sparse, test with sparse n = SubsampledNeighborsTransformer(0.3, random_state=3) expected_result = csr_matrix( ([3.464102, 1.732051, 1.732051], ([0, 1, 2], [2, 0, 1])), shape=(4, 4)) assert_array_almost_equal( n.fit(X).transform(X_csr).toarray(), expected_result.toarray()) expected_result = csr_matrix(([2.0, 1.0, 1.0], ([0, 1, 2], [2, 0, 1])), shape=(4, 4)) assert_array_equal( n.fit(X2).transform(X2_csr).toarray(), expected_result.toarray())
def test_iris_euclidean(): n = SubsampledNeighborsTransformer(0.4, metric='euclidean', random_state=42) assert_almost_equal(np.mean(n.fit(iris.data).transform(iris.data)), 0.836, decimal=2) n = SubsampledNeighborsTransformer(0.4, eps=2.0, metric='euclidean', random_state=42) assert_almost_equal(np.mean(n.fit(iris.data).transform(iris.data)), 0.147, decimal=2)
def test_sample_toy_fit_sparse_transform_nonsparse(): # Fit with sparse, test with non-sparse n = SubsampledNeighborsTransformer(0.9, random_state=2) expected_result = csr_matrix(([ 1.732051, 8.660254, 1.732051, 1.732051, 6.928203, 3.464102, 5.196152, 5.196152, 6.928203 ], ([0, 0, 1, 1, 1, 2, 2, 3, 3], [1, 3, 0, 2, 3, 0, 3, 2, 1])), shape=(4, 4)) assert_array_almost_equal( n.fit(X_csr).transform(X).toarray(), expected_result.toarray()) expected_result = csr_matrix( ([1.0, 3.0, 1.0, 1.0, 2.0, 1.0, 2.0, 1.0, 2.0], ([0, 0, 1, 1, 1, 2, 2, 3, 3], [1, 3, 0, 2, 3, 3, 0, 2, 1])), shape=(4, 4)) assert_array_equal( n.fit(X2_csr).transform(X2).toarray(), expected_result.toarray())
def test_iris_cosine(): n = SubsampledNeighborsTransformer(0.6, eps=0.1, metric='cosine', random_state=42) assert_almost_equal(np.mean(n.fit(iris.data).transform(iris.data)), 0.009, decimal=2)
def test_sample_toy_different(): # Fit and transform with different matrices n = SubsampledNeighborsTransformer(0.5, random_state=5) expected_result = csr_matrix(([5.055689, 8.833459], ([0, 1], [2, 1])), shape=(2, 3)) assert_array_almost_equal( n.fit(X_fit).transform(X_transform).toarray(), expected_result.toarray())
def test_sample_toy_noncsr(): # Fit and transform with non-CSR sparse matrices n = SubsampledNeighborsTransformer(0.8, random_state=4) expected_result = csr_matrix(([ 3.464102, 8.660254, 1.732051, 3.464102, 5.196152, 5.196152, 6.928203, 8.660254 ], ([0, 0, 1, 2, 2, 3, 3, 3], [2, 3, 0, 0, 3, 2, 1, 0])), shape=(4, 4)) assert_array_almost_equal( n.fit(X_csr.tocoo()).transform(X_csr.tolil()).toarray(), expected_result.toarray()) expected_result = csr_matrix( ([2.0, 3.0, 1.0, 1.0, 2.0, 1.0, 2.0, 3.0], ([0, 0, 1, 2, 2, 3, 3, 3], [2, 3, 0, 3, 0, 2, 1, 0])), shape=(4, 4)) assert_array_equal( n.fit(X2_csr.todok()).transform(X2_csr.tocsc()).toarray(), expected_result.toarray())