code: def test_woe(self):
      cols = [
          "unique_str",
          "underscore",
          "extra",
          "none",
          "invariant",
          "categorical",
          "na_categorical",
          "categorical_int",
      ]
  
      # balanced label with balanced features
      X_balanced = pd.DataFrame(
          data=["1", "1", "1", "2", "2", "2"], columns=["col1"]
      )
      y_balanced = [True, False, True, False, True, False]
      enc = encoders.WOEEncoder()
      enc.fit(X_balanced, y_balanced)
      X1 = enc.transform(X_balanced)
      self.assertTrue(
          all(X1.sum() < 0.001),
          "When the class label is balanced, WoE should sum to 0 in each transformed column",
      )
  
      enc = encoders.WOEEncoder(cols=cols)
      enc.fit(X, np_y)
      X1 = enc.transform(X_t)
      th.verify_numeric(X1[cols])
      self.assertTrue(
          np.isfinite(X1[cols].to_numpy()).all(),
          "There must not be any NaN, inf or -inf in the transformed columns",
      )
      self.assertEqual(
          len(list(X_t)),
          len(list(X1)),
          "The count of attributes must not change",
      )
      self.assertEqual(
          len(X_t), len(X1), "The count of rows must not change"
      )
      X2 = enc.transform(X_t, np_y_t)
      th.verify_numeric(X2)
      self.assertTrue(
          np.isfinite(X2[cols].to_numpy()).all(),
          "There must not be any NaN, inf or -inf in the transformed columns",
      )
      self.assertEqual(
          len(list(X_t)),
          len(list(X2)),
          "The count of attributes must not change",
      )
      self.assertEqual(
          len(X_t), len(X2), "The count of rows must not change"
      )
      X3 = enc.transform(X, np_y)
      th.verify_numeric(X3)
      self.assertTrue(
          np.isfinite(X3[cols].to_numpy()).all(),
          "There must not be any NaN, inf or -inf in the transformed columns",
      )
      self.assertEqual(
          len(list(X)),
          len(list(X3)),
          "The count of attributes must not change",
      )
      self.assertEqual(
          len(X), len(X3), "The count of rows must not change"
      )
      self.assertTrue(
          X3["unique_str"].var() < 0.001,
          "The unique string column must not be predictive of the label",
      )
      X4 = enc.fit_transform(X, np_y)
      th.verify_numeric(X4)
      self.assertTrue(
          np.isfinite(X4[cols].to_numpy()).all(),
          "There must not be any NaN, inf or -inf in the transformed columns",
      )
      self.assertEqual(
          len(list(X)),
          len(list(X4)),
          "The count of attributes must not change",
      )
      self.assertEqual(
          len(X), len(X4), "The count of rows must not change"
      )
      self.assertTrue(
          X4["unique_str"].var() < 0.001,
          "The unique string column must not be predictive of the label",
      )
  
      enc = encoders.WOEEncoder()
      enc.fit(X, np_y)
      X1 = enc.transform(X_t)
      self.assertEqual(
          len(list(X_t)),
          len(list(X1)),
          "The count of attributes must not change",
      )
      self.assertEqual(
          len(X_t), len(X1), "The count of rows must not change"
      )
      th.verify_numeric(X1)
      X2 = enc.transform(X_t, np_y_t)
      th.verify_numeric(X2)
      self.assertEqual(
          len(list(X_t)),
          len(list(X2)),
          "The count of attributes must not change",
      )
      self.assertEqual(
          len(X_t), len(X2), "The count of rows must not change"
      )
  
      # seed
      enc = encoders.WOEEncoder(
          cols=cols, random_state=2001, randomized=True
      )
      enc.fit(X, np_y)
      X1 = enc.transform(X_t, np_y_t)
      X2 = enc.transform(X_t, np_y_t)
      self.assertTrue(
          X1.equals(X2),
          "When the seed is given, the results must be identical",
      )
      th.verify_numeric(X1)
      th.verify_numeric(X2)
  
      # invariant target
      y_invariant = [True, True, True, True, True, True]
      enc = encoders.WOEEncoder()
      with self.assertRaises(ValueError):
          enc.fit(X_balanced, y_invariant)
  
      # branch coverage unit tests - no cols
      enc = encoders.WOEEncoder(cols=[])
      enc.fit(X, np_y)
      self.assertTrue(enc.transform(X_t).equals(X_t))
  
      # missing values in the target
      y_missing = [True, True, None, True, True, True]
      enc = encoders.WOEEncoder()
      with self.assertRaises(ValueError):
          enc.fit(X_balanced, y_missing)
  
      # impute missing
      enc = encoders.WOEEncoder(handle_missing="return_nan")
      enc.fit(X, np_y)
      X1 = enc.transform(X_t)
      th.verify_numeric(X1)
      self.assertTrue(X1.isna().any(axis=None))
      self.assertEqual(
          len(list(X_t)),
          len(list(X1)),
          "The count of attributes must not change",
      )
      self.assertEqual(
          len(X_t), len(X1), "The count of rows must not change"
      )
  
      X2 = enc.transform(X_t, np_y_t)
      th.verify_numeric(X2)
      self.assertTrue(X1.isna().any(axis=None))
      self.assertEqual(
          len(list(X_t)),
          len(list(X2)),
          "The count of attributes must not change",
      )
      self.assertEqual(
          len(X_t), len(X2), "The count of rows must not change"
      )
  
file_path: category_encoders/tests/test_woe.py
imports: import pandas
  from unittest import TestCase
  import tests.helpers
  import numpy
  import category_encoders
label_desc: Use image node_id 1 for calling the TestWeightOfEvidenceEncoder obj's underlying member method code with example usage: obj.test_woe() without return types