Adam Li
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    A proposal for improvements to Tree submodule ============================================= :Author: Adam Li (adam2392@gmail.com) :Status: Draft :Type: Process :Created: 2022-10-06 Abstract -------- The goal of this proposal is to suggest improvements to the `tree` module that will i) make the tree code more maintainable and simplified and ii) enable 3rd party packages to more easily leverage the optimized tree Python/Cython codebase. Detailed description -------------------- ### Motivation Currently, the tree submodule is not only difficult to maintain (according to dev team), but also very challenging to improve and extend. This hinders progress not only for tree development within scikit-learn, but also for 3rd party packages that seek to leverage the API of scikit-learn and the the highly optimized performances of the underlying trees. To improve the trees, we must start with a bottom-up approach by analyzing, improving and simplifying the underlying Cython code. In addition, we need to most likely do a top-down approach of simplifying and modularizing the Python tree API to fit the semantics that any possible decision tree might have. That is, the proposed API abstractions should make as little assumptions as possible, while not sacrificing the speed and optimizations that the tree code has achieved to date. Moreover, these improvements would enable scikit-learn to be able to support highly cited tree methods, such as quantile trees, survival trees, honest trees and oblique trees, all of which have been cited over 300 times and are highly used in other fields of research and industry. Some explicit examples that motivate the proposed changes are the enablement of: - honest trees (Athey 2016; 1000+ citations across multiple papers) - quantile trees (Meinshausen, 2006; 1465 citations) - oblique trees (Breiman 2001; 97697 citations) - unsupervised trees - canonical correlation tree - [name=jjerphan] Some scikit-survival models in [`sksurv.ensemble`](https://scikit-survival.readthedocs.io/en/stable/api/ensemble.html) relying on a [`LogrankCriterion`](https://github.com/sebp/scikit-survival/blob/488bf1a03524beb5728b6fb62373be23e350b396/sksurv/tree/_criterion.pyx#L92) In general, each example tree here differs from the original decision tree in how it i) uses the leaf nodes, ii) computes the split feature value, iii) criterion. These trees don't necessarily need to go into scikit-learn of course. Notes - show Thomas's PR fits nicely with oblique trees PR [x] - demonstrate quantile-trees can subclass the new API, or scikit-survival - Vincent Maladière to help regarding the above Implementation -------------- #### Python API Currently, the Python API for `BaseDecisionTree` has a few issues that seems to make extra assumptions that breaks the sklearn API contract, or makes extensions difficult. 1. `BaseDecisionTree` should not assume supervised learning: BaseDecisionTree is simply a BaseEstimator, yet it makes the assumption within its API that it is a supervised tree. This is unnecessary and the code that interfaces with the “y-labels” can be extracted to its downstream classifier trees, where it should be. If one looks at other classes that inherit from BaseEstimator, they do not make this assumption (e.g. `NeighborsBase`, which enables KNN and NearestNeighbors a supervised and unsupervised method). This most likely would introduce a non-compatible change due to changing the inner API of a `BaseDecisionTree` to assume `y=None` and then migrating the error checking code relevant for ylabels downstream. However, this is actually a sensible change that would make BaseDecisionTree actually make more sense semantically. This would enable unsupervised decision trees in 3rd party extensions and the ability for sklearn to implement such on our end if an algorithm is deemed for inclusion [1,2,3]. [1] https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.128.8686&rep=rep1&type=pdf [2] https://www.cs.jhu.edu/~jason/papers/karakos+khudanpur+eisner+priebe.icassp05.pdf [3] https://arxiv.org/pdf/1907.02844.pdf 2. `BaseDecisionTree` should modularize the setting of Cython `Tree`, Cython `Splitter` and Cython `Criterion`: This would enable 3rd party extensions to easily leverage BaseDecisionTree as their platform for any kind of decision tree, and implement their own Cython API. See [Oblique Trees PR](https://github.com/scikit-learn/scikit-learn/pull/22754). #### Cython API 1. Criterion should not assume supervised learning: I have outlined a proposal for this in #[24577](https://github.com/scikit-learn/scikit-learn/issues/24577). Please see there for more details. 2. Criterion init should handle data and meta-parameter initializations separately: Right now, `criterion.init()` and `splitter.init()` both initialize X and y assuming supervised learning. Moreover, they pass in the view to the X and y arrays while passing in other metadata, such as sample weight, and index pointers. Firstly y is not changing, but criterion.init(y) is being called repeatedly, whereas that function is meant to be called to reset mainly the start and end index pointers. 3. Splitter should not assume `y` variable: Splitter should only require X array. y is simply passed in so that way it can be passed to Criterion. That is, it is only used in literally one place. Instead, Splitter should follow the semantics of a transformer. In essence, it is fed a 2D array of (n_samples, n_features) and it returns another 2D array of (n_samples, m_try_features). Y labels are not involved. Note this change will also require changing the calls via Tree/TreeBuilder. 4. Tree and TreeBuilder should separate adding split nodes vs leaf nodes: Split nodes inherently are different from leaf nodes. Although in-code they are mixed up and it works, semantically they should be separate. This would enable honest trees and quantile trees out of the box because the main difference between those trees and the current Tree code is that the leaves are set using other information. - in honest trees, the leaves are set using a out-of-bag dataset - in quantile trees, the leaves need to store all y-labels 5. Splitter should separate computing the candidate splitting feature value: I.e. see [oblique trees PR](https://github.com/scikit-learn/scikit-learn/pull/22754) and item 1 in this #[24000](https://github.com/scikit-learn/scikit-learn/issues/24000) 6. Tree and TreeBuilder should separate how it sets node values and how it computes feature values: See [oblique trees pr](https://github.com/scikit-learn/scikit-learn/pull/22754). This would also enable generalizations for how one stores node values. 7. Enable Tree/TreeBuilder to work with different `Splitter` and `SplitRecord` and `Nodes` This is hard, but can be done with pointer casting. ### Proposed implementation sequence 1. Modularize `Criterion` without breaking backward compatibility. 2. ** Simplify `Criterion` and `Splitter` inits and working with X/y data. 3. ** Modularize `Splitter` and `Tree` and `TreeBuilder` together to allow arbitrary split values, which introduces incompatible changes in the Cython code 4. Python API for `BaseDecisionTree` without breaking backward compatibility. 5. ** Introduce separation of "leaf" and "split" nodes. Backward compatibility ---------------------- In the previous section, the implementation sequence marked with "(**)" introduce backwards-incompatible changes. Alternatives ------------ Although scikit-learn does not "support" a public Cython API, many packages either i) leverage the API directly by `cimport` from their `.pxd` files included in distro, or ii) copy/paste and modify. The copy/paste and modify approach is really just a short-term fix that eventually makes a downstream package unmaintainable and also diverge from the API of scikit-learn. By modularizing code in Cython and introducing more simple semantics at the `Criterion`, `Splitter`, `Tree` and `TreeBuilder`, scikit-learn can easily introduce more functionality through Python API entrypoints, while 3rd party packages would benefit from the highly optimized Cython codebase without creating a fork. Discussion ---------- - [Oblique tree PR](https://github.com/scikit-learn/scikit-learn/pull/22754) - [Criterion issue](https://github.com/scikit-learn/scikit-learn/issues/24577) - [Modularization of Tree/TreeBuilder/Splitter issue](https://github.com/scikit-learn/scikit-learn/issues/24000) Design as of December 9th 2022 ------------------------------ >[name=Julien Jerphanion] Still WIP ```plantuml package sklearn.base { abstract class BaseEstimator interface MultiOutputMixin interface ClassifierMixin interface RegressorMixin } package sklearn.tree { abstract class BaseDecisionTree { criterion: None splitter: None max_depth: None min_samples_split: None min_samples_leaf: None min_weight_fraction_leaf: None max_features: None max_leaf_nodes: None random_state: None min_impurity_decrease: None class_weight: None ccp_alpha: None } BaseDecisionTree <|-left- DecisionTreeClassifier BaseDecisionTree <|-right- DecisionTreeRegressor package cython { abstract class Splitter {} class Tree {} class TreeBuilder {} class Criterion {} Splitter <|-- BestFirstDenseSplitter Splitter <|-- DepthFirstDenseSplitter Splitter *-- Criterion } BaseDecisionTree *-- Tree } note top of sklearn.tree: ExtraTrees not represented\nas their logic is identical. ClassifierMixin -[hidden]left- BaseEstimator BaseEstimator <|-- BaseDecisionTree MultiOutputMixin <|-- BaseDecisionTree ClassifierMixin <|-- DecisionTreeClassifier RegressorMixin <|-- DecisionTreeRegressor ``` Copyright --------- This document has been placed in the public domain [1]_. References and Footnotes ------------------------ .. [1] Open Publication License: https://www.opencontent.org/openpub/

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully