Skip to main content

Python bindings for C++ ranger random forests

Project description

build wheels 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(return_X_y=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. It also supports quantile regression using the predict_quantiles method.

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

X, y = load_boston(return_X_y=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)
# [26.27401667  8.96549989 24.82981667 27.92506667 28.04606667 45.4693
#  21.89681787 40.30345    11.53959613 19.13675    15.88567273 16.69713567
#  ...
#  20.29025364 26.21245833 23.79643333 14.03546362 21.24893333 34.8825
#  21.22463333]

# enable quantile regression on instantiation
rfr = RangerForestRegressor(quantiles=True)
rfr.fit(X_train, y_train)

quantile_lower = rfr.predict_quantiles(X_test, quantiles=[0.1])
print(quantile_lower)
# [22.    5.   21.88 23.08 23.1  35.89 10.85 31.5   7.04 14.5  11.7  10.9
#   8.1  28.38  7.2  19.6  29.1  13.1  24.94 21.09 15.6  11.7  10.41 14.5
#  ...
#  18.9  21.4   9.43  8.7  26.46 18.99  7.2  19.27 18.5  21.19 18.99 18.88
#  14.07 21.87 22.18  9.43 17.28 29.6  18.2 ]
quantile_upper = rfr.predict_quantiles(X_test, quantiles=[0.9])
print(quantile_upper)
# [30.83 12.85 29.01 33.1  33.1  50.   29.75 50.   15.   23.   19.96 21.4
#  20.53 50.   13.35 25.   48.5  19.6  46.   26.6  23.7  20.1  17.8  21.4
#  ...
#  26.78 28.1  17.86 27.5  46.25 24.4  16.74 24.4  28.7  29.1  24.4  25.
#  25.   31.51 28.   20.8  26.7  42.13 24.24]

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.8.0.tar.gz (116.5 kB view details)

Uploaded Source

Built Distributions

skranger-0.8.0-cp310-cp310-win_amd64.whl (328.2 kB view details)

Uploaded CPython 3.10 Windows x86-64

skranger-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

skranger-0.8.0-cp310-cp310-macosx_10_15_x86_64.whl (401.1 kB view details)

Uploaded CPython 3.10 macOS 10.15+ x86-64

skranger-0.8.0-cp39-cp39-win_amd64.whl (327.9 kB view details)

Uploaded CPython 3.9 Windows x86-64

skranger-0.8.0-cp39-cp39-win32.whl (302.0 kB view details)

Uploaded CPython 3.9 Windows x86

skranger-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

skranger-0.8.0-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl (2.3 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686

skranger-0.8.0-cp39-cp39-macosx_10_15_x86_64.whl (400.4 kB view details)

Uploaded CPython 3.9 macOS 10.15+ x86-64

skranger-0.8.0-cp38-cp38-win_amd64.whl (328.0 kB view details)

Uploaded CPython 3.8 Windows x86-64

skranger-0.8.0-cp38-cp38-win32.whl (302.5 kB view details)

Uploaded CPython 3.8 Windows x86

skranger-0.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

skranger-0.8.0-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl (2.3 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686

skranger-0.8.0-cp38-cp38-macosx_10_15_x86_64.whl (401.3 kB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

File details

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

File metadata

  • Download URL: skranger-0.8.0.tar.gz
  • Upload date:
  • Size: 116.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for skranger-0.8.0.tar.gz
Algorithm Hash digest
SHA256 c97530f66a0f30b36d8e65b816932e79bda7ed09f7dd0db0694ba4c042e7724d
MD5 60aac6dd3cc84e2194c3e6318f1fa5db
BLAKE2b-256 6153a9b2c02e0c7aacd885c83bb07ada969b1ae55250ca5d3aad037ac30350a4

See more details on using hashes here.

File details

Details for the file skranger-0.8.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: skranger-0.8.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 328.2 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for skranger-0.8.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 640e64627885d7b6462faf69f30b342c5d70e4f05ee2306d89911d26479520cb
MD5 160940fb15384cf4f534558eb77ee466
BLAKE2b-256 c68270d461ebfae3e2488b38ed72e1cda1fe34eeb99f1854eab0343db1d267d3

See more details on using hashes here.

File details

Details for the file skranger-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for skranger-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 11ca67b42d8f4b0d829e0eaed42cbcf630f82032ecbf275baf87169fc5b61478
MD5 6507a24c22241499bf24855c952f0642
BLAKE2b-256 22913d1a52f9bfe38500ab72fe892659eb42e4c863fc9c982972f3084944ea68

See more details on using hashes here.

File details

Details for the file skranger-0.8.0-cp310-cp310-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for skranger-0.8.0-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 2aed8aa9170fa94f23c2590e37bfff8baacc05b5c6c92377e52edb27429840b5
MD5 52fc9e828d7948551c8de538534c85ef
BLAKE2b-256 7bd782ead658f5725d8c965ddb6eaee04eafa517e40ec5255927108604652f3a

See more details on using hashes here.

File details

Details for the file skranger-0.8.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: skranger-0.8.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 327.9 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for skranger-0.8.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 3bf3d77569811ff26dedbfe14aaed7ad920d54293b630679ac491536771a49c7
MD5 cf35fa71810baff846f461857ddf7e37
BLAKE2b-256 5e991d0cca736cff2a78d316697591cc028f8c1afc0c81fdf6da78dde432397d

See more details on using hashes here.

File details

Details for the file skranger-0.8.0-cp39-cp39-win32.whl.

File metadata

  • Download URL: skranger-0.8.0-cp39-cp39-win32.whl
  • Upload date:
  • Size: 302.0 kB
  • Tags: CPython 3.9, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for skranger-0.8.0-cp39-cp39-win32.whl
Algorithm Hash digest
SHA256 b140c3bd5775fb6aa676329b3030591fb4b8c32590e36abed2349010fafa2a89
MD5 519a5cef837893816b87da0b06180195
BLAKE2b-256 dba45dea2a5e2c9feb2b4131f8b893a3cbac906c3a6952591ac3efe9d4fa9d61

See more details on using hashes here.

File details

Details for the file skranger-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for skranger-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a2fe8671d45c7b9a3e2bc4ec1d022a73d2e81705a1ec7ea7972a1aac50113b42
MD5 a314f99687b65cdf21192dfcb0ad8e53
BLAKE2b-256 1b8741281ee495f90522b07c102b0aab10164b12dc9ae3ee19b26dff4027d1cf

See more details on using hashes here.

File details

Details for the file skranger-0.8.0-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for skranger-0.8.0-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 03a824c4d7cbd598a92bd9eb886db3c855af4f4c0a05d541e1cd712b75e82f26
MD5 276af23d2228ea4954afa674585f5b29
BLAKE2b-256 a2e984364365c281af4ab12fe3011750faa7945f89af76995e73389eca164bc9

See more details on using hashes here.

File details

Details for the file skranger-0.8.0-cp39-cp39-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for skranger-0.8.0-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 583286fbafb60a37f34888b42c3baa35c8fdd334f751d4c67da00ba56b849c6d
MD5 6ef1c7d5829a2f51740df48b32962085
BLAKE2b-256 ee376eb301f902e46fb30d7db33c9fb487f30a084ad38dcf7892d22376dc07bf

See more details on using hashes here.

File details

Details for the file skranger-0.8.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: skranger-0.8.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 328.0 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for skranger-0.8.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 78af6ae059b45cd601eb03d72d0e7fa947d75a7a3ab0f7918b45744cfe8c29a1
MD5 0778e2dfa4a291080a2ee242dba1a38b
BLAKE2b-256 3c63bd45cc24b19ce38149f3ff82a5aaf2a291c4c22f1407bf096e2909327604

See more details on using hashes here.

File details

Details for the file skranger-0.8.0-cp38-cp38-win32.whl.

File metadata

  • Download URL: skranger-0.8.0-cp38-cp38-win32.whl
  • Upload date:
  • Size: 302.5 kB
  • Tags: CPython 3.8, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for skranger-0.8.0-cp38-cp38-win32.whl
Algorithm Hash digest
SHA256 09e30630f4c6fa0b3a8434de5961873910fef52ec912e42c75e142b17ea86ad6
MD5 4253ca175b2767a3567eaad7f3e821ab
BLAKE2b-256 a2378618ff4717ed9ca8cb921f336410d405bc2aeaec10122f853bf34970ebae

See more details on using hashes here.

File details

Details for the file skranger-0.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for skranger-0.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7753534b192a129755c6731b036b7f0575983b2601d5ba85e95d4c0ae3d13d2f
MD5 8b47abc9db64186181efc7ee4e470302
BLAKE2b-256 985583f54250658e4d4784cf89d408f9dd0fd03e94545844e3c404236a537824

See more details on using hashes here.

File details

Details for the file skranger-0.8.0-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for skranger-0.8.0-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 533613fda56ac43ea9118c15f8678f140442592501c27cc6ef20a30e23cb8328
MD5 eeffbfde345accc09d8614b076e9379d
BLAKE2b-256 449990a1441d98885831762ab938b08d47616b43ac4245ef6cd6f7ac75bd0c77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for skranger-0.8.0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 d487cb36336aed1c802ceb85531f061676b41904f44cfeb0536416e2f06a8786
MD5 04e5aa0e0a18fe467fc86b54366e70bb
BLAKE2b-256 7cafc5afbca6fc03294178a08ed74109273aedc4d62821737a5575f019a5ed28

See more details on using hashes here.

Supported by

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