MCPcopy Index your code
hub / github.com/scikit-learn/scikit-learn / test_normalize

Function test_normalize

sklearn/preprocessing/tests/test_data.py:2050–2092  ·  view source on GitHub ↗
(csr_container)

Source from the content-addressed store, hash-verified

2048
2049@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
2050def test_normalize(csr_container):
2051 # Test normalize function
2052 # Only tests functionality not used by the tests for Normalizer.
2053 X = np.random.RandomState(37).randn(3, 2)
2054 assert_array_equal(normalize(X, copy=False), normalize(X.T, axis=0, copy=False).T)
2055
2056 rs = np.random.RandomState(0)
2057 X_dense = rs.randn(10, 5)
2058 X_sparse = csr_container(X_dense)
2059 ones = np.ones((10))
2060 for X in (X_dense, X_sparse):
2061 for dtype in (np.float32, np.float64):
2062 for norm in ("l1", "l2"):
2063 X = X.astype(dtype)
2064 X_norm = normalize(X, norm=norm)
2065 assert X_norm.dtype == dtype
2066
2067 X_norm = toarray(X_norm)
2068 if norm == "l1":
2069 row_sums = np.abs(X_norm).sum(axis=1)
2070 else:
2071 X_norm_squared = X_norm**2
2072 row_sums = X_norm_squared.sum(axis=1)
2073
2074 assert_array_almost_equal(row_sums, ones)
2075
2076 # Test return_norm
2077 X_dense = np.array([[3.0, 0, 4.0], [1.0, 0.0, 0.0], [2.0, 3.0, 0.0]])
2078 for norm in ("l1", "l2", "max"):
2079 _, norms = normalize(X_dense, norm=norm, return_norm=True)
2080 if norm == "l1":
2081 assert_array_almost_equal(norms, np.array([7.0, 1.0, 5.0]))
2082 elif norm == "l2":
2083 assert_array_almost_equal(norms, np.array([5.0, 1.0, 3.60555127]))
2084 else:
2085 assert_array_almost_equal(norms, np.array([4.0, 1.0, 3.0]))
2086
2087 X_sparse = csr_container(X_dense)
2088 for norm in ("l1", "l2"):
2089 with pytest.raises(NotImplementedError):
2090 normalize(X_sparse, norm=norm, return_norm=True)
2091 _, norms = normalize(X_sparse, norm="max", return_norm=True)
2092 assert_array_almost_equal(norms, np.array([4.0, 1.0, 3.0]))
2093
2094
2095@pytest.mark.parametrize(

Callers

nothing calls this directly

Calls 2

normalizeFunction · 0.90
toarrayFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…