# Fit learner using CombineML

**URL:** https://discourse.julialang.org/t/fit-learner-using-combineml/17420
**Category:** New to Julia
**Tags:** package
**Created:** [November 12, 2018, 5:06am UTC](https://discourse.julialang.org/t/fit-learner-using-combineml/17420 "2018-11-12T05:06:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)
#### Post date: [November 12, 2018, 5:06am UTC](https://discourse.julialang.org/t/fit-learner-using-combineml/17420/1 "2018-11-12T05:06:38Z")

</div>

What is the issue with the following code? I am using Julia 1.0 on Win10.

```julia
using CombineML
using CombineML.Util
using CombineML.Transformers

# # Create a learner
# # Learner with default settings
# learner = PrunedTree()

# # Learner with some of the default settings overriden
# learner = PrunedTree(Dict(
# :impl_options => Dict(
# :purity_threshold => 0.5
# )
# ))

# All learners are called in the same way.
learner = StackEnsemble(Dict(
  :learners => [
    PrunedTree(), 
    RandomForest(),
    DecisionStumpAdaboost()
  ], 
  :stacker => RandomForest()
));

# Create pipeline
pipeline1 = Pipeline(Dict(
  :transformers => [
    OneHotEncoder(), # Encodes nominal features into numeric
    Imputer(), # Imputes NA values
    StandardScaler(), # Standardizes features 
    PCA(),
    learner # Predicts labels on features
  ]
));

# Train
fit!(pipeline1, X, y)

Output: 

UndefVarError: fit! not defined
Stacktrace:
 [1] top-level scope at In[29]:35

```

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [November 12, 2018, 6:46am UTC](https://discourse.julialang.org/t/fit-learner-using-combineml/17420/2 "2018-11-12T06:46:46Z")

</div>

FWIW, I get a different error (possibly using different data) than you. It seems to suggest that the types at the end of the pipeline before `learner` don’t match what `fit!` expects:

```julia
using CombineML.Util
using CombineML.Transformers
import RDatasets
iris = RDatasets.dataset("datasets", "iris")
X = convert(Array, iris[[:SepalLength, :SepalWidth, :PetalLength, :PetalWidth]])
y = convert(Array, iris[:Species]);
learner = StackEnsemble(Dict(
  :learners => [
    PrunedTree(), 
    RandomForest(),
    DecisionStumpAdaboost()
  ], 
  :stacker => RandomForest()
));

# Create pipeline
pipeline1 = Pipeline(Dict(
  :transformers => [
    OneHotEncoder(), # Encodes nominal features into numeric
    Imputer(), # Imputes NA values
    StandardScaler(), # Standardizes features 
    PCA(),
    learner # Predicts labels on features
  ]
));
fit!(pipeline1, X, y)

```

the error being

```julia
ERROR: MethodError: no method matching fit!(::StackEnsemble, ::LinearAlgebra.Adjoint{Float64,Array{Float64,2}}, ::Array{String,1})
Closest candidates are:
  fit!(::StackEnsemble, ::Array{T,2} where T, ::Array{T,1} where T) at /Users/tlienart/.julia/packages/CombineML/p9oKU/src/combineml/ensemble.jl:84
  fit!(::Transformer, ::Array{T,2} where T, ::Array{T,1} where T) at /Users/tlienart/.julia/packages/CombineML/p9oKU/src/types.jl:27
  fit!(::Baseline, ::Array{T,2} where T, ::Array{T,1} where T) at /Users/tlienart/.julia/packages/CombineML/p9oKU/src/combineml/baseline.jl:34

```

showing that the issue is that whatever comes out of your pipeline, once it gets to the learner, the dimensions and or types are not ok.

In fact just applying your `learner` directly on the data works

```julia
julia> fit!(learner, X, y)
Dict{Symbol,Any} with 4 entries:
  :learners => Learner[PrunedTree(Decision Tree…
  :keep_original_features => false
  :stacker => RandomForest(Ensemble of Decision Trees…
  :label_map => LabelMap (with 3 labels):…

```

So maybe a couple of pointers to try:

- apply the same `using` as in [the example](https://github.com/ppalmes/CombineML.jl/blob/master/MetaModeling.ipynb) so not loading `CombineML` by itself
- make sure that the pipeline doesn’t alter types and dimensions differently than what you’d expect

---

<div class="post-metadata">

### Author: ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)
#### Post date: [November 12, 2018, 7:29am UTC](https://discourse.julialang.org/t/fit-learner-using-combineml/17420/3 "2018-11-12T07:29:19Z")

</div>

@tlienart: Thanks for your quick response.

I have been able to pin-point the issue here. By commenting out the PCA() bit from the pipeline and prefixing fit with Transformers, the code works fine. I will separately investigate why PCA is not working. The fit error probably occured because I have also initialised StatsBase as one of package for dataprocessing and this led to a confusion about which fit method to use by the code.

However, the score() does not work now which I am not much worried. Do I need to prefix it as well?

```julia
# Create pipeline
pipeline1 = Pipeline(Dict(
  :transformers => [
    OneHotEncoder(), # Encodes nominal features into numeric
    Imputer(), # Imputes NA values
    StandardScaler(), # Standardizes features 
# PCA(),
    learner # Predicts labels on features
  ]
));

# Train
Transformers.fit!(pipeline1, X_train0, y_train0)

# Predict
predictions = transform!(pipeline1, X)
# Assess predictions
result = score(:accuracy, y, predictions)
Output:
UndefVarError: score not defined

Stacktrace:
 [1] top-level scope at In[30]:3

```

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [November 12, 2018, 7:32am UTC](https://discourse.julialang.org/t/fit-learner-using-combineml/17420/4 "2018-11-12T07:32:20Z")

</div>

nice

I think `CombineML` is not being actively maintained anymore unfortunately (I just tried to go through the example ipynb and a [number of things needed fixing](https://github.com/ppalmes/CombineML.jl/issues/18) maybe you could also mention your trouble with PCA?

For the `score` bit, did you do `using CombineML.Util` ? I seem to have the `score` function ok:

```julia
# fresh julia
julia> using CombineML.Util
help?> score
search: score isconcretetype searchsorted searchsortedlast searchsortedfirst StackOverflowError

  No documentation found.

  CombineML.Util.score is a Function.

  # 1 method for generic function "score":
  [1] score(metric::Symbol, actual, predicted) in CombineML.Util at /Users/tlienart/.julia/dev/CombineML/src/util.jl:53

```

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [November 12, 2018, 7:34am UTC](https://discourse.julialang.org/t/fit-learner-using-combineml/17420/5 "2018-11-12T07:34:48Z")

</div>

otherwise you can maybe consider using [MLMetrics](https://github.com/JuliaML/MLMetrics.jl)

---

<div class="post-metadata">

### Author: ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)
#### Post date: [November 12, 2018, 7:36am UTC](https://discourse.julialang.org/t/fit-learner-using-combineml/17420/6 "2018-11-12T07:36:00Z")

</div>

Yes it is the very first line of my code. I used confusion\_matrix from DecisionTree to get the accuracy score.
