def _test_input_format_f_contiguous_numpy(dtype): rng = np.random.RandomState(0) x_default = np.array(5 * rng.random_sample((10, 4)), dtype=dtype) x_numpy = np.asanyarray(x_default, dtype=dtype, order='F') assert not x_numpy.flags.c_contiguous assert x_numpy.flags.f_contiguous assert x_numpy.flags.fnc expected = linear_kernel(x_default) result = linear_kernel(x_numpy) assert_allclose(expected, result)
def _test_input_format_c_contiguous_pandas(dtype): pd = pytest.importorskip('pandas') rng = np.random.RandomState(0) x_default = np.array(5 * rng.random_sample((10, 4)), dtype=dtype) x_numpy = np.asanyarray(x_default, dtype=dtype, order='C') assert x_numpy.flags.c_contiguous assert not x_numpy.flags.f_contiguous assert not x_numpy.flags.fnc x_df = pd.DataFrame(x_numpy) expected = linear_kernel(x_df) result = linear_kernel(x_numpy) assert_allclose(expected, result)
def test_dense_self_linear_kernel(): rng = np.random.RandomState(0) X = np.array(5 * rng.random_sample((10, 4))) result = linear_kernel(X) expected = np.dot(X, np.array(X).T) assert_allclose(result, expected, rtol=1e-15)
def _test_input_format_c_not_contiguous_numpy(dtype): rng = np.random.RandomState(0) x_default = np.array(5 * rng.random_sample((10, 4)), dtype=dtype) dummy_data = np.insert(x_default, range(1, x_default.shape[1]), 8, axis=1) x_numpy = dummy_data[:, ::2] assert_allclose(x_numpy, x_default) assert not x_numpy.flags.c_contiguous assert not x_numpy.flags.f_contiguous assert not x_numpy.flags.fnc expected = linear_kernel(x_default) result = linear_kernel(x_numpy) assert_allclose(expected, result)
def _test_dense_small_linear_kernel(scale, shift, dtype): rng = np.random.RandomState(0) X = np.array(5 * rng.random_sample((10, 4)), dtype=dtype) Y = np.array(5 * rng.random_sample((15, 4)), dtype=dtype) result = linear_kernel(X, Y, scale=scale, shift=shift) expected = np.dot(X, np.array(Y).T) * scale + shift tol = 1e-14 if dtype == np.float64 else 1e-6 assert_allclose(result, expected, rtol=tol)