Python 的 PyALE 函数对于 2D ALE 绘图的值错误

问题描述 投票:0回答:1

我正在使用 Python 的 PyALE 函数创建累积局部效应图。我正在使用 RandomForestRegression 函数来构建模型。

我可以创建一维 ALE 图。但是,当我尝试使用相同的模型和训练数据创建 2D ALE 图时,出现值错误。

这是我的代码。

ale(training_data, model=model1, feature=["feature1", "feature2"])

我可以使用以下代码绘制特征 1 和特征 2 的一维 ALE 图。

ale(training_data, model=model1, feature=["feature1"], feature_type="continuous")

ale(training_data, model=model1, feature=["feature2"], feature_type="continuous")

数据框中的任何列都没有缺失值或无限值。

我使用 2D ALE 绘图命令收到以下错误。

ValueError: Input contains NaN, infinity or a value too large for dtype('float32').

这是该函数的链接https://pypi.org/project/PyALE/#description

我不确定为什么会出现此错误。我希望能得到一些帮助。

python random-forest iml pyale
1个回答
2
投票

问题已在 PyALE 包 v1.1.2 版本中得到解决。对于那些使用早期版本的用户,github 中的问题线程中提到的解决方法是重置馈送到函数

ale
的数据集的索引。为了完整起见,这里有一个重现错误和解决方法的代码:

from PyALE import ale
import pandas as pd
import matplotlib.pyplot as plt
import random
from sklearn.ensemble import RandomForestRegressor

# get the raw diamond data (from R's ggplot2)
dat_diamonds = pd.read_csv(
    "https://raw.githubusercontent.com/tidyverse/ggplot2/master/data-raw/diamonds.csv"
)
X = dat_diamonds.loc[:, ~dat_diamonds.columns.str.contains("price")].copy()
y = dat_diamonds.loc[:, "price"].copy()

features = ["carat","depth", "table", "x", "y", "z"]

# fit the model
model = RandomForestRegressor(random_state=1345)
model.fit(X[features], y)

# sample the data
random.seed(1234)
indices = random.sample(range(X.shape[0]), 10000)
sampleData = X.loc[indices, :]

# get the effects.....
# This throws the error
ale_eff = ale(X=sampleData[features], model=model, feature=["z", "table"], grid_size=100)

# This will work, just reset the index with drop=True
ale_eff = ale(X=sampleData[features].reset_index(drop=True), model=model, feature=["z", "table"], grid_size=100)
© www.soinside.com 2019 - 2024. All rights reserved.