<h3 dir="auto">Describe the bug</h3>
<p dir="auto">sklearn.inspection.PartialDependenceDisplay fails with nulls in the dimension of the partial dependence</p>
<h3 dir="auto">Steps/Code to Reproduce</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#This is a slimmed down version of the example in the sklear documentation
#https://scikit-learn.org/stable/auto_examples/inspection/plot_partial_dependence.html
#I only need to change one line 

from sklearn.datasets import fetch_openml
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OrdinalEncoder
from time import time
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import HistGradientBoostingRegressor
import numpy as np

bikes = fetch_openml(&quot;Bike_Sharing_Demand&quot;, version=2, as_frame=True, parser=&quot;pandas&quot;)
# Make an explicit copy to avoid &quot;SettingWithCopyWarning&quot; from pandas
X, y = bikes.data.copy(), bikes.target

#####LOOK HERE#######
#This line is changed to put the value to null
X[&quot;weather&quot;].replace(to_replace=&quot;heavy_rain&quot;, value=np.nan, inplace=True)

mask_training = X[&quot;year&quot;] == 0.0
X = X.drop(columns=[&quot;year&quot;])
X_train, y_train = X[mask_training], y[mask_training]
X_test, y_test = X[~mask_training], y[~mask_training]

numerical_features = [&quot;temp&quot;,&quot;feel_temp&quot;,&quot;humidity&quot;,&quot;windspeed&quot;,
                      ]
categorical_features = X_train.columns.drop(numerical_features)

hgbdt_preprocessor = ColumnTransformer(
    transformers=[
        (&quot;cat&quot;, OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=np.nan), categorical_features),
        (&quot;num&quot;, &quot;passthrough&quot;, numerical_features),
    ],
    sparse_threshold=1,
    verbose_feature_names_out=False
).set_output(transform=&quot;pandas&quot;)


hgbdt_model = make_pipeline(
    hgbdt_preprocessor,
    HistGradientBoostingRegressor(
        categorical_features=categorical_features, random_state=0
    ),
)
hgbdt_model.fit(X_train, y_train)

import matplotlib.pyplot as plt
from sklearn.inspection import PartialDependenceDisplay

features_info = {
    # features of interest
    &quot;features&quot;: [&quot;temp&quot;, &quot;humidity&quot;, &quot;windspeed&quot;, &quot;season&quot;, &quot;weather&quot;, &quot;hour&quot;],
    # information regarding categorical features
    &quot;categorical_features&quot;: categorical_features,
}
common_params = {
    &quot;subsample&quot;: 50,
    &quot;n_jobs&quot;: 2,
    &quot;grid_resolution&quot;: 20,
    &quot;random_state&quot;: 0,
}

print(&quot;Computing partial dependence plots...&quot;)
tic = time()
_, ax = plt.subplots(ncols=3, nrows=2, figsize=(9, 8), constrained_layout=True)
display = PartialDependenceDisplay.from_estimator(
    hgbdt_model,
    X_train,
    **features_info,
    ax=ax,
    **common_params,
)"><pre class="notranslate"><span class="pl-c">#This is a slimmed down version of the example in the sklear documentation</span>
<span class="pl-c">#https://scikit-learn.org/stable/auto_examples/inspection/plot_partial_dependence.html</span>
<span class="pl-c">#I only need to change one line </span>

<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">datasets</span> <span class="pl-k">import</span> <span class="pl-s1">fetch_openml</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">compose</span> <span class="pl-k">import</span> <span class="pl-v">ColumnTransformer</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">preprocessing</span> <span class="pl-k">import</span> <span class="pl-v">OrdinalEncoder</span>
<span class="pl-k">from</span> <span class="pl-s1">time</span> <span class="pl-k">import</span> <span class="pl-s1">time</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">pipeline</span> <span class="pl-k">import</span> <span class="pl-s1">make_pipeline</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">ensemble</span> <span class="pl-k">import</span> <span class="pl-v">HistGradientBoostingRegressor</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-s1">bikes</span> <span class="pl-c1">=</span> <span class="pl-en">fetch_openml</span>(<span class="pl-s">"Bike_Sharing_Demand"</span>, <span class="pl-s1">version</span><span class="pl-c1">=</span><span class="pl-c1">2</span>, <span class="pl-s1">as_frame</span><span class="pl-c1">=</span><span class="pl-c1">True</span>, <span class="pl-s1">parser</span><span class="pl-c1">=</span><span class="pl-s">"pandas"</span>)
<span class="pl-c"># Make an explicit copy to avoid "SettingWithCopyWarning" from pandas</span>
<span class="pl-v">X</span>, <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">bikes</span>.<span class="pl-s1">data</span>.<span class="pl-en">copy</span>(), <span class="pl-s1">bikes</span>.<span class="pl-s1">target</span>

<span class="pl-c">#####LOOK HERE#######</span>
<span class="pl-c">#This line is changed to put the value to null</span>
<span class="pl-v">X</span>[<span class="pl-s">"weather"</span>].<span class="pl-en">replace</span>(<span class="pl-s1">to_replace</span><span class="pl-c1">=</span><span class="pl-s">"heavy_rain"</span>, <span class="pl-s1">value</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">nan</span>, <span class="pl-s1">inplace</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)

<span class="pl-s1">mask_training</span> <span class="pl-c1">=</span> <span class="pl-v">X</span>[<span class="pl-s">"year"</span>] <span class="pl-c1">==</span> <span class="pl-c1">0.0</span>
<span class="pl-v">X</span> <span class="pl-c1">=</span> <span class="pl-v">X</span>.<span class="pl-en">drop</span>(<span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">"year"</span>])
<span class="pl-v">X_train</span>, <span class="pl-s1">y_train</span> <span class="pl-c1">=</span> <span class="pl-v">X</span>[<span class="pl-s1">mask_training</span>], <span class="pl-s1">y</span>[<span class="pl-s1">mask_training</span>]
<span class="pl-v">X_test</span>, <span class="pl-s1">y_test</span> <span class="pl-c1">=</span> <span class="pl-v">X</span>[<span class="pl-c1">~</span><span class="pl-s1">mask_training</span>], <span class="pl-s1">y</span>[<span class="pl-c1">~</span><span class="pl-s1">mask_training</span>]

<span class="pl-s1">numerical_features</span> <span class="pl-c1">=</span> [<span class="pl-s">"temp"</span>,<span class="pl-s">"feel_temp"</span>,<span class="pl-s">"humidity"</span>,<span class="pl-s">"windspeed"</span>,
                      ]
<span class="pl-s1">categorical_features</span> <span class="pl-c1">=</span> <span class="pl-v">X_train</span>.<span class="pl-s1">columns</span>.<span class="pl-en">drop</span>(<span class="pl-s1">numerical_features</span>)

<span class="pl-s1">hgbdt_preprocessor</span> <span class="pl-c1">=</span> <span class="pl-v">ColumnTransformer</span>(
    <span class="pl-s1">transformers</span><span class="pl-c1">=</span>[
        (<span class="pl-s">"cat"</span>, <span class="pl-v">OrdinalEncoder</span>(<span class="pl-s1">handle_unknown</span><span class="pl-c1">=</span><span class="pl-s">'use_encoded_value'</span>, <span class="pl-s1">unknown_value</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">nan</span>), <span class="pl-s1">categorical_features</span>),
        (<span class="pl-s">"num"</span>, <span class="pl-s">"passthrough"</span>, <span class="pl-s1">numerical_features</span>),
    ],
    <span class="pl-s1">sparse_threshold</span><span class="pl-c1">=</span><span class="pl-c1">1</span>,
    <span class="pl-s1">verbose_feature_names_out</span><span class="pl-c1">=</span><span class="pl-c1">False</span>
).<span class="pl-en">set_output</span>(<span class="pl-s1">transform</span><span class="pl-c1">=</span><span class="pl-s">"pandas"</span>)


<span class="pl-s1">hgbdt_model</span> <span class="pl-c1">=</span> <span class="pl-en">make_pipeline</span>(
    <span class="pl-s1">hgbdt_preprocessor</span>,
    <span class="pl-v">HistGradientBoostingRegressor</span>(
        <span class="pl-s1">categorical_features</span><span class="pl-c1">=</span><span class="pl-s1">categorical_features</span>, <span class="pl-s1">random_state</span><span class="pl-c1">=</span><span class="pl-c1">0</span>
    ),
)
<span class="pl-s1">hgbdt_model</span>.<span class="pl-en">fit</span>(<span class="pl-v">X_train</span>, <span class="pl-s1">y_train</span>)

<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">inspection</span> <span class="pl-k">import</span> <span class="pl-v">PartialDependenceDisplay</span>

<span class="pl-s1">features_info</span> <span class="pl-c1">=</span> {
    <span class="pl-c"># features of interest</span>
    <span class="pl-s">"features"</span>: [<span class="pl-s">"temp"</span>, <span class="pl-s">"humidity"</span>, <span class="pl-s">"windspeed"</span>, <span class="pl-s">"season"</span>, <span class="pl-s">"weather"</span>, <span class="pl-s">"hour"</span>],
    <span class="pl-c"># information regarding categorical features</span>
    <span class="pl-s">"categorical_features"</span>: <span class="pl-s1">categorical_features</span>,
}
<span class="pl-s1">common_params</span> <span class="pl-c1">=</span> {
    <span class="pl-s">"subsample"</span>: <span class="pl-c1">50</span>,
    <span class="pl-s">"n_jobs"</span>: <span class="pl-c1">2</span>,
    <span class="pl-s">"grid_resolution"</span>: <span class="pl-c1">20</span>,
    <span class="pl-s">"random_state"</span>: <span class="pl-c1">0</span>,
}

<span class="pl-en">print</span>(<span class="pl-s">"Computing partial dependence plots..."</span>)
<span class="pl-s1">tic</span> <span class="pl-c1">=</span> <span class="pl-en">time</span>()
<span class="pl-s1">_</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>(<span class="pl-s1">ncols</span><span class="pl-c1">=</span><span class="pl-c1">3</span>, <span class="pl-s1">nrows</span><span class="pl-c1">=</span><span class="pl-c1">2</span>, <span class="pl-s1">figsize</span><span class="pl-c1">=</span>(<span class="pl-c1">9</span>, <span class="pl-c1">8</span>), <span class="pl-s1">constrained_layout</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-s1">display</span> <span class="pl-c1">=</span> <span class="pl-v">PartialDependenceDisplay</span>.<span class="pl-en">from_estimator</span>(
    <span class="pl-s1">hgbdt_model</span>,
    <span class="pl-v">X_train</span>,
    <span class="pl-c1">**</span><span class="pl-s1">features_info</span>,
    <span class="pl-s1">ax</span><span class="pl-c1">=</span><span class="pl-s1">ax</span>,
    <span class="pl-c1">**</span><span class="pl-s1">common_params</span>,
)</pre></div>
<h3 dir="auto">Expected Results</h3>
<p dir="auto">I would find three possibilities acceptable</p>
<ol dir="auto">
<li>
<p dir="auto">Error with a message saying that "NULLs are not allowed". The actual error is not clear what the issue is and it took a while for me to debug it.</p>
</li>
<li>
<p dir="auto">Apply the pd.dropna() function on only the relevant feature while looping. My current work around is to do this in sklearn.inspection.partial_dependence. It cannot be done in this function when you are getting the PDP across many feature because it would distort the results.</p>
</li>
<li>
<p dir="auto">Add a bin or something where the PDP value for the null is given. This seems simpler for categoricals.</p>
</li>
</ol>
<h3 dir="auto">Actual Results</h3>
<div class="highlight highlight-text-python-traceback notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):

  File &quot;/opt/anaconda3/envs/sklearn12/lib/python3.10/site-packages/spyder_kernels/py3compat.py&quot;, line 356, in compat_exec
    exec(code, globals, locals)

  File &quot;/Users/ Part_dep.py&quot;, line 73, in &lt;module&gt;
    display = PartialDependenceDisplay.from_estimator(

  File &quot;/opt/anaconda3/envs/sklearn12/lib/python3.10/site-packages/sklearn/inspection/_plot/partial_dependence.py&quot;, line 655, in from_estimator
    [

  File &quot;/opt/anaconda3/envs/sklearn12/lib/python3.10/site-packages/sklearn/inspection/_plot/partial_dependence.py&quot;, line 656, in &lt;listcomp&gt;
    len(_unique(_safe_indexing(X, idx, axis=1)))

  File &quot;/opt/anaconda3/envs/sklearn12/lib/python3.10/site-packages/sklearn/utils/_encode.py&quot;, line 45, in _unique
    return _unique_np(

  File &quot;/opt/anaconda3/envs/sklearn12/lib/python3.10/site-packages/sklearn/utils/_encode.py&quot;, line 53, in _unique_np
    uniques = np.unique(

  File &quot;&lt;__array_function__ internals&gt;&quot;, line 180, in unique

  File &quot;/opt/anaconda3/envs/sklearn12/lib/python3.10/site-packages/numpy/lib/arraysetops.py&quot;, line 274, in unique
    ret = _unique1d(ar, return_index, return_inverse, return_counts,

  File &quot;/opt/anaconda3/envs/sklearn12/lib/python3.10/site-packages/numpy/lib/arraysetops.py&quot;, line 336, in _unique1d
    ar.sort()

TypeError: '&lt;' not supported between instances of 'float' and 'str'"><pre class="notranslate">Traceback (most recent call last):

  File <span class="pl-s">"/opt/anaconda3/envs/sklearn12/lib/python3.10/site-packages/spyder_kernels/py3compat.py"</span>, line <span class="pl-c1">356</span>, in <span class="pl-en">compat_exec</span>
    <span class="pl-c1">exec</span>(code, <span class="pl-c1">globals</span>, <span class="pl-c1">locals</span>)

  File <span class="pl-s">"/Users/ Part_dep.py"</span>, line <span class="pl-c1">73</span>, in <span class="pl-en">&lt;module&gt;</span>
    display <span class="pl-k">=</span> PartialDependenceDisplay.from_estimator(

  File <span class="pl-s">"/opt/anaconda3/envs/sklearn12/lib/python3.10/site-packages/sklearn/inspection/_plot/partial_dependence.py"</span>, line <span class="pl-c1">655</span>, in <span class="pl-en">from_estimator</span>
    [

  File <span class="pl-s">"/opt/anaconda3/envs/sklearn12/lib/python3.10/site-packages/sklearn/inspection/_plot/partial_dependence.py"</span>, line <span class="pl-c1">656</span>, in <span class="pl-en">&lt;listcomp&gt;</span>
    <span class="pl-c1">len</span>(_unique(_safe_indexing(X, idx, <span class="pl-v">axis</span><span class="pl-k">=</span><span class="pl-c1">1</span>)))

  File <span class="pl-s">"/opt/anaconda3/envs/sklearn12/lib/python3.10/site-packages/sklearn/utils/_encode.py"</span>, line <span class="pl-c1">45</span>, in <span class="pl-en">_unique</span>
    <span class="pl-k">return</span> _unique_np(

  File <span class="pl-s">"/opt/anaconda3/envs/sklearn12/lib/python3.10/site-packages/sklearn/utils/_encode.py"</span>, line <span class="pl-c1">53</span>, in <span class="pl-en">_unique_np</span>
    uniques <span class="pl-k">=</span> np.unique(

  File <span class="pl-s">"&lt;__array_function__ internals&gt;"</span>, line <span class="pl-c1">180</span>, in <span class="pl-en">unique</span>

  File <span class="pl-s">"/opt/anaconda3/envs/sklearn12/lib/python3.10/site-packages/numpy/lib/arraysetops.py"</span>, line <span class="pl-c1">274</span>, in <span class="pl-en">unique</span>
    ret <span class="pl-k">=</span> _unique1d(ar, return_index, return_inverse, return_counts,

  File <span class="pl-s">"/opt/anaconda3/envs/sklearn12/lib/python3.10/site-packages/numpy/lib/arraysetops.py"</span>, line <span class="pl-c1">336</span>, in <span class="pl-en">_unique1d</span>
    ar.sort()

<span class="pl-en">TypeError</span>: <span class="pl-s">'&lt;' not supported between instances of 'float' and 'str'</span></pre></div>
<h3 dir="auto">Versions</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="System:
    python: 3.10.8 (main, Nov  4 2022, 08:45:18) [Clang 12.0.0 ]
executable: /opt/anaconda3/envs/sklearn12/bin/python
   machine: macOS-10.16-x86_64-i386-64bit

Python dependencies:
      sklearn: 1.2.0
          pip: 22.3.1
   setuptools: 65.6.3
        numpy: 1.23.5
        scipy: 1.9.3
       Cython: None
       pandas: 1.5.3
   matplotlib: 3.6.2
       joblib: 1.1.1
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       filepath: /opt/anaconda3/envs/sklearn12/lib/libmkl_rt.1.dylib
         prefix: libmkl_rt
       user_api: blas
   internal_api: mkl
        version: 2021.4-Product
    num_threads: 10
threading_layer: intel

       filepath: /opt/anaconda3/envs/sklearn12/lib/libomp.dylib
         prefix: libomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 10"><pre class="notranslate">System:
    python: 3.10.8 (main, Nov  4 2022, 08:45:18) [Clang 12.0.0 ]
executable: /opt/anaconda3/envs/sklearn12/bin/python
   machine: macOS-10.16-x86_64-i386-64bit

Python dependencies:
      sklearn: 1.2.0
          pip: 22.3.1
   setuptools: 65.6.3
        numpy: 1.23.5
        scipy: 1.9.3
       Cython: None
       pandas: 1.5.3
   matplotlib: 3.6.2
       joblib: 1.1.1
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       filepath: /opt/anaconda3/envs/sklearn12/lib/libmkl_rt.1.dylib
         prefix: libmkl_rt
       user_api: blas
   internal_api: mkl
        version: 2021.4-Product
    num_threads: 10
threading_layer: intel

       filepath: /opt/anaconda3/envs/sklearn12/lib/libomp.dylib
         prefix: libomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 10</pre></div>
<h3 dir="auto">Describe the bug</h3>
<p dir="auto">Since version 1.2, the partial dependence plot can deal with categorical features - fantastic! We encountered a small but annoying issue when there are missing values in the categoricals:</p>
<ul dir="auto">
<li>Missing values in numeric features are handled, but not shown (okay)</li>
<li>Missing values in categoricals raise an error</li>
</ul>
<h3 dir="auto">Steps/Code to Reproduce</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="# Make data

from sklearn import datasets
import pandas as pd

iris, Species = datasets.load_iris(return_X_y=True)
iris = pd.DataFrame(
    iris, 
    columns=[&quot;sepal_length&quot;, &quot;sepal_width&quot;, &quot;petal_length&quot;, &quot;petal_width&quot;]
)
iris[&quot;species&quot;] = pd.Series(Species).map({0: &quot;A&quot;, 1: &quot;B&quot;, 2: &quot;C&quot;})
iris.head()


# Add some missing values

import numpy as np

iris.loc[0:5, &quot;sepal_width&quot;] = np.nan
iris.loc[0:5, &quot;species&quot;] = np.nan


# Make model

from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
from sklearn.linear_model import LinearRegression
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import make_pipeline

species_encoder = make_pipeline(
    SimpleImputer(strategy=&quot;constant&quot;, fill_value=&quot;A&quot;),
    OneHotEncoder(drop=[&quot;A&quot;], sparse_output=False)
)

preprocessor = ColumnTransformer(
    transformers=[
        (&quot;species_encoder&quot;, species_encoder, [&quot;species&quot;]),
        (&quot;other&quot;, SimpleImputer(), [&quot;sepal_width&quot;, &quot;petal_width&quot;, &quot;petal_length&quot;])
    ],
    verbose_feature_names_out=False
).set_output(transform=&quot;pandas&quot;)

model = make_pipeline(preprocessor, LinearRegression())

model.fit(iris, iris.sepal_length)


# Inspect

from sklearn.inspection import PartialDependenceDisplay

_ = PartialDependenceDisplay.from_estimator(
    model,
    iris,
    features=[&quot;sepal_width&quot;, &quot;species&quot;], 
    categorical_features=[&quot;species&quot;]
)"><pre class="notranslate"><span class="pl-c"># Make data</span>

<span class="pl-k">from</span> <span class="pl-s1">sklearn</span> <span class="pl-k">import</span> <span class="pl-s1">datasets</span>
<span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>

<span class="pl-s1">iris</span>, <span class="pl-v">Species</span> <span class="pl-c1">=</span> <span class="pl-s1">datasets</span>.<span class="pl-en">load_iris</span>(<span class="pl-s1">return_X_y</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-s1">iris</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(
    <span class="pl-s1">iris</span>, 
    <span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">"sepal_length"</span>, <span class="pl-s">"sepal_width"</span>, <span class="pl-s">"petal_length"</span>, <span class="pl-s">"petal_width"</span>]
)
<span class="pl-s1">iris</span>[<span class="pl-s">"species"</span>] <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">Series</span>(<span class="pl-v">Species</span>).<span class="pl-en">map</span>({<span class="pl-c1">0</span>: <span class="pl-s">"A"</span>, <span class="pl-c1">1</span>: <span class="pl-s">"B"</span>, <span class="pl-c1">2</span>: <span class="pl-s">"C"</span>})
<span class="pl-s1">iris</span>.<span class="pl-en">head</span>()


<span class="pl-c"># Add some missing values</span>

<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-s1">iris</span>.<span class="pl-s1">loc</span>[<span class="pl-c1">0</span>:<span class="pl-c1">5</span>, <span class="pl-s">"sepal_width"</span>] <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">nan</span>
<span class="pl-s1">iris</span>.<span class="pl-s1">loc</span>[<span class="pl-c1">0</span>:<span class="pl-c1">5</span>, <span class="pl-s">"species"</span>] <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">nan</span>


<span class="pl-c"># Make model</span>

<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">impute</span> <span class="pl-k">import</span> <span class="pl-v">SimpleImputer</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">preprocessing</span> <span class="pl-k">import</span> <span class="pl-v">OneHotEncoder</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">linear_model</span> <span class="pl-k">import</span> <span class="pl-v">LinearRegression</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">compose</span> <span class="pl-k">import</span> <span class="pl-v">ColumnTransformer</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">pipeline</span> <span class="pl-k">import</span> <span class="pl-s1">make_pipeline</span>

<span class="pl-s1">species_encoder</span> <span class="pl-c1">=</span> <span class="pl-en">make_pipeline</span>(
    <span class="pl-v">SimpleImputer</span>(<span class="pl-s1">strategy</span><span class="pl-c1">=</span><span class="pl-s">"constant"</span>, <span class="pl-s1">fill_value</span><span class="pl-c1">=</span><span class="pl-s">"A"</span>),
    <span class="pl-v">OneHotEncoder</span>(<span class="pl-s1">drop</span><span class="pl-c1">=</span>[<span class="pl-s">"A"</span>], <span class="pl-s1">sparse_output</span><span class="pl-c1">=</span><span class="pl-c1">False</span>)
)

<span class="pl-s1">preprocessor</span> <span class="pl-c1">=</span> <span class="pl-v">ColumnTransformer</span>(
    <span class="pl-s1">transformers</span><span class="pl-c1">=</span>[
        (<span class="pl-s">"species_encoder"</span>, <span class="pl-s1">species_encoder</span>, [<span class="pl-s">"species"</span>]),
        (<span class="pl-s">"other"</span>, <span class="pl-v">SimpleImputer</span>(), [<span class="pl-s">"sepal_width"</span>, <span class="pl-s">"petal_width"</span>, <span class="pl-s">"petal_length"</span>])
    ],
    <span class="pl-s1">verbose_feature_names_out</span><span class="pl-c1">=</span><span class="pl-c1">False</span>
).<span class="pl-en">set_output</span>(<span class="pl-s1">transform</span><span class="pl-c1">=</span><span class="pl-s">"pandas"</span>)

<span class="pl-s1">model</span> <span class="pl-c1">=</span> <span class="pl-en">make_pipeline</span>(<span class="pl-s1">preprocessor</span>, <span class="pl-v">LinearRegression</span>())

<span class="pl-s1">model</span>.<span class="pl-en">fit</span>(<span class="pl-s1">iris</span>, <span class="pl-s1">iris</span>.<span class="pl-s1">sepal_length</span>)


<span class="pl-c"># Inspect</span>

<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">inspection</span> <span class="pl-k">import</span> <span class="pl-v">PartialDependenceDisplay</span>

<span class="pl-s1">_</span> <span class="pl-c1">=</span> <span class="pl-v">PartialDependenceDisplay</span>.<span class="pl-en">from_estimator</span>(
    <span class="pl-s1">model</span>,
    <span class="pl-s1">iris</span>,
    <span class="pl-s1">features</span><span class="pl-c1">=</span>[<span class="pl-s">"sepal_width"</span>, <span class="pl-s">"species"</span>], 
    <span class="pl-s1">categorical_features</span><span class="pl-c1">=</span>[<span class="pl-s">"species"</span>]
)</pre></div>
<ul dir="auto">
<li>A missing value in the numeric feature seems no problem (even if the nan level is not shown in the plot)</li>
<li>The code works when not adding missing values</li>
</ul>
<h3 dir="auto">Expected Results</h3>
<p dir="auto">Either a plot without nan level or a plot with separate nan level (might be an option in some fix)</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/16206095/212533978-a32a3d9e-ea16-4c14-b3cc-2c2abc0998e3.png"><img src="https://user-images.githubusercontent.com/16206095/212533978-a32a3d9e-ea16-4c14-b3cc-2c2abc0998e3.png" alt="output" style="max-width: 100%;"></a></p>
<h3 dir="auto">Actual Results</h3>
<div class="highlight highlight-text-python-traceback notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="TypeError                                 Traceback (most recent call last)
Cell In [73], line 3
      1 from sklearn.inspection import PartialDependenceDisplay
----&gt; 3 _ = PartialDependenceDisplay.from_estimator(
      4     model,
      5     iris,
      6     features=[&quot;sepal_width&quot;, &quot;species&quot;], 
      7     categorical_features=[&quot;species&quot;]
      8 )

File c:\Users\Michael\anaconda3\envs\ml_lecture\lib\site-packages\sklearn\inspection\_plot\partial_dependence.py:705, in PartialDependenceDisplay.from_estimator(cls, estimator, X, features, categorical_features, feature_names, target, response_method, n_cols, grid_resolution, percentiles, method, n_jobs, verbose, line_kw, ice_lines_kw, pd_line_kw, contour_kw, ax, kind, centered, subsample, random_state)
    699         raise ValueError(
    700             f&quot;When a floating-point, subsample={subsample} should be in &quot;
    701             &quot;the (0, 1) range.&quot;
    702         )
    704 # compute predictions and/or averaged predictions
--&gt; 705 pd_results = Parallel(n_jobs=n_jobs, verbose=verbose)(
    706     delayed(partial_dependence)(
    707         estimator,
    708         X,
    709         fxs,
    710         feature_names=feature_names,
    711         categorical_features=categorical_features,
    712         response_method=response_method,
...
--&gt; 336     ar.sort()
    337     aux = ar
    338 mask = np.empty(aux.shape, dtype=np.bool_)

TypeError: '&lt;' not supported between instances of 'str' and 'float'"><pre class="notranslate">TypeError                                 Traceback (most recent call last)
Cell In [73], line 3
      <span class="pl-c1">1</span> <span class="pl-k">from</span> sklearn.inspection <span class="pl-k">import</span> PartialDependenceDisplay
----&gt; 3 _ = PartialDependenceDisplay.from_estimator(
      <span class="pl-c1">4</span>     model,
      <span class="pl-c1">5</span>     iris,
      <span class="pl-c1">6</span>     features<span class="pl-k">=</span>[<span class="pl-s"><span class="pl-pds">"</span>sepal_width<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>species<span class="pl-pds">"</span></span>], 
      <span class="pl-c1">7</span>     categorical_features<span class="pl-k">=</span>[<span class="pl-s"><span class="pl-pds">"</span>species<span class="pl-pds">"</span></span>]
      <span class="pl-c1">8</span> )

File c:\Users\Michael\anaconda3\envs\ml_lecture\lib\site-packages\sklearn\inspection\_plot\partial_dependence.py:705, in PartialDependenceDisplay.from_estimator(cls, estimator, X, features, categorical_features, feature_names, target, response_method, n_cols, grid_resolution, percentiles, method, n_jobs, verbose, line_kw, ice_lines_kw, pd_line_kw, contour_kw, ax, kind, centered, subsample, random_state)
    <span class="pl-c1">699</span>         <span class="pl-k">raise</span> <span class="pl-c1">ValueError</span>(
    <span class="pl-c1">700</span>             <span class="pl-s">f</span><span class="pl-pds">"</span><span class="pl-s">When a floating-point, subsample=</span><span class="pl-c1">{</span>subsample<span class="pl-c1">}</span><span class="pl-s"> should be in </span><span class="pl-pds">"</span>
    <span class="pl-c1">701</span>             <span class="pl-s"><span class="pl-pds">"</span>the (0, 1) range.<span class="pl-pds">"</span></span>
    <span class="pl-c1">702</span>         )
    <span class="pl-c1">704</span> <span class="pl-c"><span class="pl-c">#</span> compute predictions and/or averaged predictions</span>
--&gt; 705 pd_results = Parallel(n_jobs=n_jobs, verbose=verbose)(
    <span class="pl-c1">706</span>     delayed(partial_dependence)(
    <span class="pl-c1">707</span>         estimator,
    <span class="pl-c1">708</span>         X,
    <span class="pl-c1">709</span>         fxs,
    <span class="pl-c1">710</span>         feature_names<span class="pl-k">=</span>feature_names,
    <span class="pl-c1">711</span>         categorical_features<span class="pl-k">=</span>categorical_features,
    <span class="pl-c1">712</span>         response_method<span class="pl-k">=</span>response_method,
...
--&gt; 336     ar.sort()
    <span class="pl-c1">337</span>     aux <span class="pl-k">=</span> ar
    <span class="pl-c1">338</span> mask <span class="pl-k">=</span> np.empty(aux.shape, <span class="pl-v">dtype</span><span class="pl-k">=</span>np.bool_)

<span class="pl-en">TypeError</span>: <span class="pl-s">'&lt;' not supported between instances of 'str' and 'float'</span></pre></div>
<h3 dir="auto">Versions</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="sklearn: 1.2.0"><pre class="notranslate">sklearn: 1.2.0</pre></div>