mlr-org / mlr3pipelines

Dataflow Programming for Machine Learning in R
https://mlr3pipelines.mlr-org.com/
GNU Lesser General Public License v3.0
132 stars 25 forks source link

graphlearners doesn't work in conterfactual packages #773

Closed yzm886216 closed 3 days ago

yzm886216 commented 3 months ago

My code is showed below:

prc_rf_lrn=lrn("classif.ranger",predict_type="prob",importance="impurity") graph_rf=prc_rf_lrn %>>% po("threshold") graph_rf$plot() learners_rf=GraphLearner$new(graph_rf)

learners_rf 's predict_type is [response], prob

* Model: list * Parameters: classif.ranger.importance=impurity, classif.ranger.max.depth=4, classif.ranger.min.node.size=30, classif.ranger.mtry=4, classif.ranger.num.threads=1, classif.ranger.num.trees=400, threshold.thresholds=0.4444 * Packages: mlr3, mlr3pipelines, mlr3learners, ranger * Predict Types: [response], prob * Feature Types: logical, integer, numeric, character, factor, ordered, POSIXct * Properties: featureless, hotstart_backward, hotstart_forward, importance, loglik, missings, multiclass, oob_error, selected_features, twoclass, weights But when I use conterfacutal packages, it went wrong: predictor1=Predictor$new(learners_rf,data=dat1,y="Prostate_cancer",type="prob") Warning: Output seems to be class instead of probabilities. Automatically transformed to 0 and 1 probabilities. You might want to set predict.type = 'prob' for Learner! So my results are strange.How to solve it?
mb706 commented 2 months ago

The counterfactuals package needs a model that outputs probabilities. Although you set the classif.ranger's predict.type to prob, you are using the po("threshold") PipeOp. The latter turns probability predictions into response predictions by thresholding them. What is the reason why you are using thresholding? If you are using the counterfactuals package and want to limit yourself to counterfactuals that are beyond a certain probability, you probably need to use the desired_prob argument of the $find_counterfactuals() method.

larskotthoff commented 2 months ago

And please don't cross-post on stackoverflow and here.

mb706 commented 3 days ago

Closing this in favour of https://github.com/mlr-org/mlr3pipelines/issues/645