def fit_then_transform_dense(expected, input, categorical_features='all', minimum_fraction=None): ohe = OneHotEncoder(categorical_features=categorical_features, sparse=False, minimum_fraction=minimum_fraction) transformation = ohe.fit_transform(input.copy()) assert_array_almost_equal(expected, transformation) ohe2 = OneHotEncoder(categorical_features=categorical_features, sparse=False, minimum_fraction=minimum_fraction) ohe2.fit(input.copy()) transformation = ohe2.transform(input.copy()) assert_array_almost_equal(expected, transformation)
def fit_then_transform(expected, input, categorical_features='all', minimum_fraction=None): # Test fit_transform ohe = OneHotEncoder(categorical_features=categorical_features, minimum_fraction=minimum_fraction) transformation = ohe.fit_transform(input.copy()) assert_array_almost_equal(expected.astype(float), transformation.todense()) # Test fit, and afterwards transform ohe2 = OneHotEncoder(categorical_features=categorical_features, minimum_fraction=minimum_fraction) ohe2.fit(input.copy()) transformation = ohe2.transform(input.copy()) assert_array_almost_equal(expected, transformation.todense())
def test_transform(): """Test OneHotEncoder with both dense and sparse matrixes.""" input = np.array(((0, 1, 2, 3, 4, 5), (0, 1, 2, 3, 4, 5))).transpose() ohe = OneHotEncoder() ohe.fit(input) test_data = np.array(((0, 1, 2, 6), (0, 1, 6, 7))).transpose() output = ohe.transform(test_data).todense() assert np.sum(output) == 5 input = np.array(((0, 1, 2, 3, 4, 5), (0, 1, 2, 3, 4, 5))).transpose() ips = scipy.sparse.csr_matrix(input) ohe = OneHotEncoder() ohe.fit(ips) test_data = np.array(((0, 1, 2, 6), (0, 1, 6, 7))).transpose() tds = scipy.sparse.csr_matrix(test_data) output = ohe.transform(tds).todense() assert np.sum(output) == 3
def test_refit_on_new_data(): """Test that OneHotEncoder can refit on two data sets.""" ohe = OneHotEncoder() ohe.fit(dense1) ohe.fit(dense2)