code: def test_multi_hot_encoder():
      """Tests basic MultiHotEncoder functionality."""
      col_a = ["red", "green", "blue", "red"]
      col_b = ["warm", "cold", "hot", "cold"]
      col_c = [1, 10, 5, 10]
      col_d = [["warm"], [], ["hot", "warm", "cold"], ["cold", "cold"]]
      in_df = pd.DataFrame.from_dict(
          {"A": col_a, "B": col_b, "C": col_c, "D": col_d}
      )
      ds = ray.data.from_pandas(in_df)
  
      encoder = MultiHotEncoder(["B", "C", "D"])
  
      # Transform with unfitted preprocessor.
      with pytest.raises(PreprocessorNotFittedException):
          encoder.transform(ds)
  
      # Fit data.
      encoder.fit(ds)
  
      assert encoder.stats_ == {
          "unique_values(B)": {"cold": 0, "hot": 1, "warm": 2},
          "unique_values(C)": {1: 0, 5: 1, 10: 2},
          "unique_values(D)": {"cold": 0, "hot": 1, "warm": 2},
      }
  
      # Transform data.
      transformed = encoder.transform(ds)
      out_df = transformed.to_pandas()
  
      processed_col_a = col_a
      processed_col_b = [[0, 0, 1], [1, 0, 0], [0, 1, 0], [1, 0, 0]]
      processed_col_c = [[1, 0, 0], [0, 0, 1], [0, 1, 0], [0, 0, 1]]
      processed_col_d = [[0, 0, 1], [0, 0, 0], [1, 1, 1], [2, 0, 0]]
      expected_df = pd.DataFrame.from_dict(
          {
              "A": processed_col_a,
              "B": processed_col_b,
              "C": processed_col_c,
              "D": processed_col_d,
          }
      )
  
      assert out_df.equals(expected_df)
  
      # Transform batch.
      pred_col_a = ["blue", "yellow", None]
      pred_col_b = ["cold", "warm", "other"]
      pred_col_c = [10, 1, 20]
      pred_col_d = [["cold", "warm"], [], ["other", "cold"]]
      pred_in_df = pd.DataFrame.from_dict(
          {
              "A": pred_col_a,
              "B": pred_col_b,
              "C": pred_col_c,
              "D": pred_col_d,
          }
      )
  
      pred_out_df = encoder.transform_batch(pred_in_df)
      print(pred_out_df.to_string())
  
      pred_processed_col_a = ["blue", "yellow", None]
      pred_processed_col_b = [[1, 0, 0], [0, 0, 1], [0, 0, 0]]
      pred_processed_col_c = [[0, 0, 1], [1, 0, 0], [0, 0, 0]]
      pred_processed_col_d = [[1, 0, 1], [0, 0, 0], [1, 0, 0]]
      pred_expected_df = pd.DataFrame.from_dict(
          {
              "A": pred_processed_col_a,
              "B": pred_processed_col_b,
              "C": pred_processed_col_c,
              "D": pred_processed_col_d,
          }
      )
  
      assert pred_out_df.equals(pred_expected_df)
  
      # Test null behavior.
      null_col = [1, None]
      nonnull_col = [1, 1]
      null_df = pd.DataFrame.from_dict({"A": null_col})
      null_ds = ray.data.from_pandas(null_df)
      nonnull_df = pd.DataFrame.from_dict({"A": nonnull_col})
      nonnull_ds = ray.data.from_pandas(nonnull_df)
      null_encoder = OneHotEncoder(["A"])
  
      # Verify fit fails for null values.
      with pytest.raises(ValueError):
          null_encoder.fit(null_ds)
      null_encoder.fit(nonnull_ds)
  
      # Verify transform fails for null values.
      with pytest.raises(ValueError):
          null_encoder.transform(null_ds).materialize()
      null_encoder.transform(nonnull_ds)
  
      # Verify transform_batch fails for null values.
      with pytest.raises(ValueError):
          null_encoder.transform_batch(null_df)
      null_encoder.transform_batch(nonnull_df)
  
      # Verify that `fit` and `transform` work with ndarrays.
      df = pd.DataFrame(
          {"column": [np.array(["A"]), np.array(["A", "B"])]}
      )
      ds = ray.data.from_pandas(df)
      encoder = MultiHotEncoder(["column"])
      transformed = encoder.fit_transform(ds)
      encodings = [
          record["column"] for record in transformed.take_all()
      ]
      assert encodings == [[1, 0], [1, 1]]
  
file_path: ray/python/ray/data/tests/preprocessors/test_encoder.py
imports: import numpy
  import pandas
  import pytest
  import ray
  from ray.data.preprocessor import PreprocessorNotFittedException
  from ray.data.preprocessors import Categorizer, LabelEncoder, MultiHotEncoder, OneHotEncoder, OrdinalEncoder
label_desc: Use image node_id 5 for calling a global function with example usage: test_multi_hot_encoder() without return types