code: def test_invalid_n_bins_array():
      # Bad shape
      n_bins = np.full((2, 4), 2.0)
      est = KBinsDiscretizer(n_bins=n_bins)
      err_msg = (
          r"n_bins must be a scalar or array of shape \(n_features,\)."
      )
      with pytest.raises(ValueError, match=err_msg):
          est.fit_transform(X)
  
      # Incorrect number of features
      n_bins = [1, 2, 2]
      est = KBinsDiscretizer(n_bins=n_bins)
      err_msg = (
          r"n_bins must be a scalar or array of shape \(n_features,\)."
      )
      with pytest.raises(ValueError, match=err_msg):
          est.fit_transform(X)
  
      # Bad bin values
      n_bins = [1, 2, 2, 1]
      est = KBinsDiscretizer(n_bins=n_bins)
      err_msg = (
          "KBinsDiscretizer received an invalid number of bins "
          "at indices 0, 3. Number of bins must be at least 2, "
          "and must be an int."
      )
      with pytest.raises(ValueError, match=err_msg):
          est.fit_transform(X)
  
      # Float bin values
      n_bins = [2.1, 2, 2.1, 2]
      est = KBinsDiscretizer(n_bins=n_bins)
      err_msg = (
          "KBinsDiscretizer received an invalid number of bins "
          "at indices 0, 2. Number of bins must be at least 2, "
          "and must be an int."
      )
      with pytest.raises(ValueError, match=err_msg):
          est.fit_transform(X)
  
imports: import warnings
  import numpy
  import pytest
  import scipy.sparse
  from sklearn import clone
  from sklearn.preprocessing import KBinsDiscretizer, OneHotEncoder
  from sklearn.utils._testing import assert_allclose, assert_allclose_dense_sparse, assert_array_almost_equal, assert_array_equal
label_desc: Use image node_id 4 for calling a global function with example usage: test_invalid_n_bins_array() without return types