Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added alignment for zero-based index for interaction_constraints #167

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/MLJInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ MLJModelInterface.@mlj_model mutable struct LGBMRegressor <: MLJModelInterface.D
cegb_penalty_feature_lazy::Vector{Float64} = Vector{Float64}()
cegb_penalty_feature_coupled::Vector{Float64} = Vector{Float64}()
path_smooth::Float64 = 0.0::(_ >= 0.0)
interaction_constraints::String = ""
interaction_constraints::Vector{Vector{Int}} = Vector{Vector{Int}}()
verbosity::Int = 1

# Dataset parameters
Expand Down Expand Up @@ -209,7 +209,7 @@ MLJModelInterface.@mlj_model mutable struct LGBMClassifier <: MLJModelInterface.
cegb_penalty_feature_lazy::Vector{Float64} = Vector{Float64}()
cegb_penalty_feature_coupled::Vector{Float64} = Vector{Float64}()
path_smooth::Float64 = 0.0::(_ >= 0.0)
interaction_constraints::String = ""
interaction_constraints::Vector{Vector{Int}} = Vector{Vector{Int}}()
verbosity::Int = 1

# Dateset parameters
Expand Down
18 changes: 9 additions & 9 deletions src/estimators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ mutable struct LGBMRegression <: LGBMEstimator
cegb_penalty_feature_lazy::Vector{Float64}
cegb_penalty_feature_coupled::Vector{Float64}
path_smooth::Float64
interaction_constraints::String
interaction_constraints::Vector{Vector{Int}}
verbosity::Int

# Dataset parameters
Expand Down Expand Up @@ -176,7 +176,7 @@ end
cegb_penalty_feature_lazy = Float64[],
cegb_penalty_feature_coupled = Float64[],
path_smooth = 0.,
interaction_constraints = "",
interaction_constraints = Vector{Int}[],
verbosity = 1,
linear_tree = false,
max_bin = 255,
Expand Down Expand Up @@ -277,7 +277,7 @@ function LGBMRegression(;
cegb_penalty_feature_lazy = Float64[],
cegb_penalty_feature_coupled = Float64[],
path_smooth = 0.,
interaction_constraints = "",
interaction_constraints = Vector{Int}[],
verbosity = 1,
linear_tree = false,
max_bin = 255,
Expand Down Expand Up @@ -403,7 +403,7 @@ mutable struct LGBMClassification <: LGBMEstimator
cegb_penalty_feature_lazy::Vector{Float64}
cegb_penalty_feature_coupled::Vector{Float64}
path_smooth::Float64
interaction_constraints::String
interaction_constraints::Vector{Vector{Int}}
verbosity::Int

# Dataset parameters
Expand Down Expand Up @@ -520,7 +520,7 @@ end
cegb_penalty_feature_lazy = Float64[],
cegb_penalty_feature_coupled = Float64[],
path_smooth = 0.
interaction_constraints = "",
interaction_constraints = Vector{Int}[],
verbosity = 1,
linear_tree = false,
max_bin = 255,
Expand Down Expand Up @@ -626,7 +626,7 @@ function LGBMClassification(;
cegb_penalty_feature_lazy = Float64[],
cegb_penalty_feature_coupled = Float64[],
path_smooth = 0.,
interaction_constraints = "",
interaction_constraints = Vector{Int}[],
verbosity = 1,
linear_tree = false,
max_bin = 255,
Expand Down Expand Up @@ -755,7 +755,7 @@ mutable struct LGBMRanking <: LGBMEstimator
cegb_penalty_feature_lazy::Vector{Float64}
cegb_penalty_feature_coupled::Vector{Float64}
path_smooth::Float64
interaction_constraints::String
interaction_constraints::Vector{Vector{Int}}
verbosity::Int

# Dataset parameters
Expand Down Expand Up @@ -875,7 +875,7 @@ end
cegb_penalty_feature_lazy = Float64[],
cegb_penalty_feature_coupled = Float64[],
path_smooth = 0.,
interaction_constraints = "",
interaction_constraints = Vector{Int}[],
verbosity = 1,
linear_tree = false,
max_bin = 255,
Expand Down Expand Up @@ -984,7 +984,7 @@ function LGBMRanking(;
cegb_penalty_feature_lazy = Float64[],
cegb_penalty_feature_coupled = Float64[],
path_smooth = 0.,
interaction_constraints = "",
interaction_constraints = Vector{Int}[],
verbosity = 1,
linear_tree = false,
max_bin = 255,
Expand Down
6 changes: 4 additions & 2 deletions src/fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,10 @@ function stringifyparams(estimator::LGBMEstimator)

if !isempty(param_value)
# Convert parameters that contain indices to C's zero-based indices.
if in(param_name, INDEXPARAMS) && typeof(param_value) <: AbstractArray
param_value = param_value .- one(eltype(param_value))
if param_name == :interaction_constraints && typeof(param_value) <: Vector{Vector{Int}}
param_value = "[" * join(map(x -> join(x .- 1, ","), param_value), "],[") * "]"
elseif in(param_name, INDEXPARAMS) && typeof(param_value) <: AbstractArray
param_value = param_value .- one(eltype(param_value))
end

if typeof(param_value) <: Array
Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

const INDEXPARAMS = [:categorical_feature]
const INDEXPARAMS = [:categorical_feature, :interaction_constraints]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extended this to interaction_constraints param which requires similar zero-based indexing handling to categorical_feature.
There are some dataset params such as label_column, weight_column, or group_column can take a single index indicating the column, or the column name and these are added in https://github.com/IQVIA-ML/LightGBM.jl/pull/164/files.


const MAXIMIZE_METRICS = ["auc", "ndcg", "average_precision"]

Expand Down
8 changes: 7 additions & 1 deletion test/basic/test_fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,17 @@ end

@testset "stringifyparams -- convert to zero-based" begin
indices = [1, 3, 5, 7, 9]
classifier = LightGBM.LGBMClassification(categorical_feature = indices, verbosity = -1)
interaction_constraints = [[1, 3], [5, 7], [9]]
classifier = LightGBM.LGBMClassification(
categorical_feature = indices,
interaction_constraints = interaction_constraints,
verbosity = -1)
ds_parameters = LightGBM.stringifyparams(classifier)

expected = "categorical_feature=0,2,4,6,8"
expected_constraints = "interaction_constraints=[0,2],[4,6],[8]"
@test occursin(expected, ds_parameters)
@test occursin(expected_constraints, ds_parameters)
end

@testset "stringifyparams -- multiple calls won't mutate fields" begin
Expand Down
4 changes: 2 additions & 2 deletions test/basic/test_parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ end
monotone_penalty = 0.1
# The below interaction constraints are defined so that features in each vector are allowed to interact with each other
# The model can consider interactions between those grouped features when buiding trees.
# Features that are no included in any interaction constraing group are treated as independent.
interaction_constraints = "[1,2],[3,4,5],[6,7,8,9,10]"
# Features that are not included in any interaction constraint group are treated as independent.
interaction_constraints = [[1,2],[3,4,5],[6,7,8,9,10]]

# Create and fit the estimator with monotone constraints
estimator_with_monotone_constraints = LightGBM.LGBMClassification(
Expand Down
Loading