Skip to main content

Python bindings for C++ ranger random forests

Project description

actions travis rtd pypi pyversions

skranger provides scikit-learn compatible Python bindings to the C++ random forest implementation, ranger, using Cython.

The latest release of skranger uses version 0.12.1 of ranger.

Installation

skranger is available on pypi and can be installed via pip:

pip install skranger

Usage

There are two sklearn compatible classes, RangerForestClassifier and RangerForestRegressor. There is also the RangerForestSurvival class, which aims to be compatible with the scikit-survival API.

RangerForestClassifier

The RangerForestClassifier predictor uses ranger’s ForestProbability class to enable both predict and predict_proba methods.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from skranger.ensemble import RangerForestClassifier

X, y = load_iris(True)
X_train, X_test, y_train, y_test = train_test_split(X, y)

rfc = RangerForestClassifier()
rfc.fit(X_train, y_train)

predictions = rfc.predict(X_test)
print(predictions)
# [1 2 0 0 0 0 1 2 1 1 2 2 2 1 1 0 1 1 0 1 1 1 0 2 1 0 0 1 2 2 0 1 2 2 0 2 0 0]

probabilities = rfc.predict_proba(X_test)
print(probabilities)
# [[0.01333333 0.98666667 0.        ]
#  [0.         0.         1.        ]
#  ...
#  [0.98746032 0.01253968 0.        ]
#  [0.99       0.01       0.        ]]

RangerForestRegressor

The RangerForestRegressor predictor uses ranger’s ForestRegression class.

from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from skranger.ensemble import RangerForestRegressor

X, y = load_boston(True)
X_train, X_test, y_train, y_test = train_test_split(X, y)

rfr = RangerForestRegressor()
rfr.fit(X_train, y_train)

predictions = rfr.predict(X_test)
print(predictions)
# [20.01270808 24.65041667 11.97722067 20.10345    26.48676667 42.19045952
#  19.821      31.51163333  8.34169603 18.94511667 20.21901915 16.01440705
#  ...
#  18.37752952 19.34765    20.13355    21.19648333 18.91611667 15.58964837
#  31.4223    ]

RangerForestSurvival

The RangerForestSurvival predictor uses ranger’s ForestSurvival class, and has an interface similar to the RandomSurvivalForest found in the scikit-survival package.

from sksurv.datasets import load_veterans_lung_cancer
from sklearn.model_selection import train_test_split
from skranger.ensemble import RangerForestSurvival

X, y = load_veterans_lung_cancer()
# select the numeric columns as features
X = X[["Age_in_years", "Karnofsky_score", "Months_from_Diagnosis"]]
X_train, X_test, y_train, y_test = train_test_split(X, y)

rfs = RangerForestSurvival()
rfs.fit(X_train, y_train)

predictions = rfs.predict(X_test)
print(predictions)
# [107.99634921  47.41235714  88.39933333  91.23566667  61.82104762
#   61.15052381  90.29888492  47.88706349  21.25111508  85.5768254
#   ...
#   56.85498016  53.98227381  48.88464683  95.58649206  48.9142619
#   57.68516667  71.96549206 101.79123016  58.95402381  98.36299206]

chf = rfs.predict_cumulative_hazard_function(X_test)
print(chf)
# [[0.04233333 0.0605     0.24305556 ... 1.6216627  1.6216627  1.6216627 ]
#  [0.00583333 0.00583333 0.00583333 ... 1.55410714 1.56410714 1.58410714]
#  ...
#  [0.12933333 0.14766667 0.14766667 ... 1.64342857 1.64342857 1.65342857]
#  [0.00983333 0.0112619  0.04815079 ... 1.79304365 1.79304365 1.79304365]]

survival = rfs.predict_survival_function(X_test)
print(survival)
# [[0.95855021 0.94129377 0.78422794 ... 0.19756993 0.19756993 0.19756993]
#  [0.99418365 0.99418365 0.99418365 ... 0.21137803 0.20927478 0.20513086]
#  ...
#  [0.87868102 0.86271864 0.86271864 ... 0.19331611 0.19331611 0.19139258]
#  [0.99021486 0.98880127 0.95299007 ... 0.16645277 0.16645277 0.16645277]]

License

skranger is licensed under GPLv3.

Development

To develop locally, it is recommended to have asdf, make and a C++ compiler already installed. After cloning, run make setup. This will setup the ranger submodule, install python and poetry from .tool-versions, install dependencies using poetry, copy the ranger source code into skranger, and then build and install skranger in the local virtualenv.

To format code, run make fmt. This will run isort and black against the .py files.

To run tests and inspect coverage, run make test.

To rebuild in place after making changes, run make build.

To create python package artifacts, run make dist.

To build and view documentation, run make docs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

skranger-0.2.0.tar.gz (115.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

skranger-0.2.0-cp38-cp38-manylinux1_x86_64.whl (432.8 kB view details)

Uploaded CPython 3.8

skranger-0.2.0-cp38-cp38-manylinux1_i686.whl (424.9 kB view details)

Uploaded CPython 3.8

skranger-0.2.0-cp38-cp38-macosx_10_15_x86_64.whl (460.3 kB view details)

Uploaded CPython 3.8macOS 10.15+ x86-64

skranger-0.2.0-cp37-cp37m-manylinux1_x86_64.whl (433.6 kB view details)

Uploaded CPython 3.7m

skranger-0.2.0-cp37-cp37m-manylinux1_i686.whl (425.5 kB view details)

Uploaded CPython 3.7m

skranger-0.2.0-cp37-cp37m-macosx_10_15_x86_64.whl (460.6 kB view details)

Uploaded CPython 3.7mmacOS 10.15+ x86-64

skranger-0.2.0-cp36-cp36m-manylinux1_x86_64.whl (433.5 kB view details)

Uploaded CPython 3.6m

skranger-0.2.0-cp36-cp36m-manylinux1_i686.whl (425.4 kB view details)

Uploaded CPython 3.6m

skranger-0.2.0-cp36-cp36m-macosx_10_15_x86_64.whl (460.6 kB view details)

Uploaded CPython 3.6mmacOS 10.15+ x86-64

File details

Details for the file skranger-0.2.0.tar.gz.

File metadata

  • Download URL: skranger-0.2.0.tar.gz
  • Upload date:
  • Size: 115.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.2 Darwin/17.7.0

File hashes

Hashes for skranger-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3ce1eaf197ef8dece93031b63b6838c6abfb8a1481bddaf580a1727ab4164a4f
MD5 01f19a848cc168c4b7463d936b72b961
BLAKE2b-256 11e5999f4cc6d76983a7696091deddf94e91888b58f811f26322210a6c9ff3a7

See more details on using hashes here.

File details

Details for the file skranger-0.2.0-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: skranger-0.2.0-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 432.8 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.0 requests/2.24.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.17

File hashes

Hashes for skranger-0.2.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 220f00658cf909a05e40d4f672799d47f5345379a1319b08469142f431fdcdb9
MD5 9bf41df03761a25f544446df9b47b674
BLAKE2b-256 a894c0ac269f7f942da79cdcfae3ee27676b93d9217341c2b69771cfc614e0fb

See more details on using hashes here.

File details

Details for the file skranger-0.2.0-cp38-cp38-manylinux1_i686.whl.

File metadata

  • Download URL: skranger-0.2.0-cp38-cp38-manylinux1_i686.whl
  • Upload date:
  • Size: 424.9 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.0 requests/2.24.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.17

File hashes

Hashes for skranger-0.2.0-cp38-cp38-manylinux1_i686.whl
Algorithm Hash digest
SHA256 28ef1f6d3f79717316f670f0c970baab9abaa6308ecf8f0acdec37c81e9adace
MD5 7695a8fd8a04c9aaee2279cfb0374c3f
BLAKE2b-256 06af585873f9c0bb6ba64a78d9092067961bb87aa3e688a51b48281d18c90b90

See more details on using hashes here.

File details

Details for the file skranger-0.2.0-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: skranger-0.2.0-cp38-cp38-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 460.3 kB
  • Tags: CPython 3.8, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.6

File hashes

Hashes for skranger-0.2.0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 abb28fbda1badceee0fb9fb7f7ffae66b93ef4e43cc35972eb1822551857f0f7
MD5 604a9a83c12eb861a5a504069bda3d1a
BLAKE2b-256 33b3648fffe2087f6aebb7e80f0a4cd0d10dcd70ad8e526ea980e826a3dfe6f1

See more details on using hashes here.

File details

Details for the file skranger-0.2.0-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: skranger-0.2.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 433.6 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.0 requests/2.24.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.17

File hashes

Hashes for skranger-0.2.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 68fae445d809966ad9a65f14265e63c173c5dfa6c40031112a0b1ad29098ee8c
MD5 cc787e27a815b4a4ead2de5d5231fa54
BLAKE2b-256 601cdc5d78124b1bd52e1d3df22474c0f56578e15621c38aa4e829ea403798d0

See more details on using hashes here.

File details

Details for the file skranger-0.2.0-cp37-cp37m-manylinux1_i686.whl.

File metadata

  • Download URL: skranger-0.2.0-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 425.5 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.0 requests/2.24.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.17

File hashes

Hashes for skranger-0.2.0-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 8950ea5c862fe8b7678fbdee3b9a315d27cead6fbe76f3e963e32c6c15baa791
MD5 9970e06f128f58cf43de6beeac5cedca
BLAKE2b-256 be0b35db0c45f48917a4269b5b180ff9205bb7620f6df8044f45b70835ca2f31

See more details on using hashes here.

File details

Details for the file skranger-0.2.0-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: skranger-0.2.0-cp37-cp37m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 460.6 kB
  • Tags: CPython 3.7m, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.9

File hashes

Hashes for skranger-0.2.0-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 2d259c72a083fd1fd0dfde63ef41b03ce7f49c4ee98daf39cb8e04ada229de99
MD5 21d71c593c7e36b674d27568ade320cb
BLAKE2b-256 545cf960bce5ffb0b271febee3115087f77c0e46da9fe8767b67888638fb8ff7

See more details on using hashes here.

File details

Details for the file skranger-0.2.0-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: skranger-0.2.0-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 433.5 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.0 requests/2.24.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.17

File hashes

Hashes for skranger-0.2.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0600e9051b23e033ff7e09fcf25ce483cf396ee3233625fbb69aad55ab5b56dd
MD5 61d24bd9f05e8d965ee3eef7d3313872
BLAKE2b-256 5b9e7354c8c0aed23a59e7fb71692ece9e03166c18f7d6a3d0880a918848dc02

See more details on using hashes here.

File details

Details for the file skranger-0.2.0-cp36-cp36m-manylinux1_i686.whl.

File metadata

  • Download URL: skranger-0.2.0-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 425.4 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.0 requests/2.24.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.17

File hashes

Hashes for skranger-0.2.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 6f736680a955b24748a93146b171d34df85bd47eba1f5c9ceec65092820837c2
MD5 d2cc1846aa040bbb7c04944467c081d9
BLAKE2b-256 1ea2b01c5cc44cc5f93c8b130a3ddc1add18a9588242e9c74bee5d950f146a19

See more details on using hashes here.

File details

Details for the file skranger-0.2.0-cp36-cp36m-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: skranger-0.2.0-cp36-cp36m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 460.6 kB
  • Tags: CPython 3.6m, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.6.8

File hashes

Hashes for skranger-0.2.0-cp36-cp36m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 62aa7fe8a6d312eb033866f1a9d82dbbb0f1c46453d51f2a9db01fb1eb5dc2cc
MD5 73e98dc347a3292406052b0eadedd8b6
BLAKE2b-256 d90ca33c6c44cfe86f2931213ffd793729e0db670336857739ea3cae64ec3a85

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page