Skip to main content

skranger

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.

Metadata

Release files for skranger 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for skranger 0.8.0
File Size Uploaded
skranger-0.8.0.tar.gz 116.5 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for skranger 0.8.0
File
skranger-0.8.0-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
skranger-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details
skranger-0.8.0-cp310-cp310-macosx_10_15_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.15+ x86-64 Details
skranger-0.8.0-cp39-cp39-win_amd64.whl CPython 3.9 CPython 3.9 Windows x86-64 Details
skranger-0.8.0-cp39-cp39-win32.whl CPython 3.9 CPython 3.9 Windows x86-32 Details
skranger-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.17+ x86-64 Details
skranger-0.8.0-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl CPython 3.9 CPython 3.9 Linux glibc 2.17+ x86-32 Details
skranger-0.8.0-cp39-cp39-macosx_10_15_x86_64.whl CPython 3.9 CPython 3.9 macOS 10.15+ x86-64 Details
skranger-0.8.0-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details
skranger-0.8.0-cp38-cp38-win32.whl CPython 3.8 CPython 3.8 Windows x86-32 Details
skranger-0.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.17+ x86-64 Details
skranger-0.8.0-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl CPython 3.8 CPython 3.8 Linux glibc 2.17+ x86-32 Details
skranger-0.8.0-cp38-cp38-macosx_10_15_x86_64.whl CPython 3.8 CPython 3.8 macOS 10.15+ x86-64 Details

Total release size: 14.4 MB

Release files / skranger-0.8.0.tar.gz

Download URL skranger-0.8.0.tar.gz
Size 116.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c97530f66a0f30b36d8e65b816932e79bda7ed09f7dd0db0694ba4c042e7724d
BLAKE2b-256 checksum
How to use checksums
6153a9b2c02e0c7aacd885c83bb07ada969b1ae55250ca5d3aad037ac30350a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.8.10

Release files / skranger-0.8.0-cp310-cp310-win_amd64.whl

Download URL skranger-0.8.0-cp310-cp310-win_amd64.whl
Size 328.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
640e64627885d7b6462faf69f30b342c5d70e4f05ee2306d89911d26479520cb
BLAKE2b-256 checksum
How to use checksums
c68270d461ebfae3e2488b38ed72e1cda1fe34eeb99f1854eab0343db1d267d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / skranger-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL skranger-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.3 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
11ca67b42d8f4b0d829e0eaed42cbcf630f82032ecbf275baf87169fc5b61478
BLAKE2b-256 checksum
How to use checksums
22913d1a52f9bfe38500ab72fe892659eb42e4c863fc9c982972f3084944ea68
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / skranger-0.8.0-cp310-cp310-macosx_10_15_x86_64.whl

Download URL skranger-0.8.0-cp310-cp310-macosx_10_15_x86_64.whl
Size 401.1 kB
Tags CPython 3.10 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
2aed8aa9170fa94f23c2590e37bfff8baacc05b5c6c92377e52edb27429840b5
BLAKE2b-256 checksum
How to use checksums
7bd782ead658f5725d8c965ddb6eaee04eafa517e40ec5255927108604652f3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / skranger-0.8.0-cp39-cp39-win_amd64.whl

Download URL skranger-0.8.0-cp39-cp39-win_amd64.whl
Size 327.9 kB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
3bf3d77569811ff26dedbfe14aaed7ad920d54293b630679ac491536771a49c7
BLAKE2b-256 checksum
How to use checksums
5e991d0cca736cff2a78d316697591cc028f8c1afc0c81fdf6da78dde432397d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / skranger-0.8.0-cp39-cp39-win32.whl

Download URL skranger-0.8.0-cp39-cp39-win32.whl
Size 302.0 kB
Tags CPython 3.9 Windows x86-32
SHA-256 checksum
How to use checksums
b140c3bd5775fb6aa676329b3030591fb4b8c32590e36abed2349010fafa2a89
BLAKE2b-256 checksum
How to use checksums
dba45dea2a5e2c9feb2b4131f8b893a3cbac906c3a6952591ac3efe9d4fa9d61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / skranger-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL skranger-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.3 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
a2fe8671d45c7b9a3e2bc4ec1d022a73d2e81705a1ec7ea7972a1aac50113b42
BLAKE2b-256 checksum
How to use checksums
1b8741281ee495f90522b07c102b0aab10164b12dc9ae3ee19b26dff4027d1cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / skranger-0.8.0-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL skranger-0.8.0-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl
Size 2.3 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-32
SHA-256 checksum
How to use checksums
03a824c4d7cbd598a92bd9eb886db3c855af4f4c0a05d541e1cd712b75e82f26
BLAKE2b-256 checksum
How to use checksums
a2e984364365c281af4ab12fe3011750faa7945f89af76995e73389eca164bc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / skranger-0.8.0-cp39-cp39-macosx_10_15_x86_64.whl

Download URL skranger-0.8.0-cp39-cp39-macosx_10_15_x86_64.whl
Size 400.4 kB
Tags CPython 3.9 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
583286fbafb60a37f34888b42c3baa35c8fdd334f751d4c67da00ba56b849c6d
BLAKE2b-256 checksum
How to use checksums
ee376eb301f902e46fb30d7db33c9fb487f30a084ad38dcf7892d22376dc07bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / skranger-0.8.0-cp38-cp38-win_amd64.whl

Download URL skranger-0.8.0-cp38-cp38-win_amd64.whl
Size 328.0 kB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
78af6ae059b45cd601eb03d72d0e7fa947d75a7a3ab0f7918b45744cfe8c29a1
BLAKE2b-256 checksum
How to use checksums
3c63bd45cc24b19ce38149f3ff82a5aaf2a291c4c22f1407bf096e2909327604
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / skranger-0.8.0-cp38-cp38-win32.whl

Download URL skranger-0.8.0-cp38-cp38-win32.whl
Size 302.5 kB
Tags CPython 3.8 Windows x86-32
SHA-256 checksum
How to use checksums
09e30630f4c6fa0b3a8434de5961873910fef52ec912e42c75e142b17ea86ad6
BLAKE2b-256 checksum
How to use checksums
a2378618ff4717ed9ca8cb921f336410d405bc2aeaec10122f853bf34970ebae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / skranger-0.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL skranger-0.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.3 MB
Tags CPython 3.8 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
7753534b192a129755c6731b036b7f0575983b2601d5ba85e95d4c0ae3d13d2f
BLAKE2b-256 checksum
How to use checksums
985583f54250658e4d4784cf89d408f9dd0fd03e94545844e3c404236a537824
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / skranger-0.8.0-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL skranger-0.8.0-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl
Size 2.3 MB
Tags CPython 3.8 Linux glibc 2.17+ x86-32
SHA-256 checksum
How to use checksums
533613fda56ac43ea9118c15f8678f140442592501c27cc6ef20a30e23cb8328
BLAKE2b-256 checksum
How to use checksums
449990a1441d98885831762ab938b08d47616b43ac4245ef6cd6f7ac75bd0c77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / skranger-0.8.0-cp38-cp38-macosx_10_15_x86_64.whl

Download URL skranger-0.8.0-cp38-cp38-macosx_10_15_x86_64.whl
Size 401.3 kB
Tags CPython 3.8 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
d487cb36336aed1c802ceb85531f061676b41904f44cfeb0536416e2f06a8786
BLAKE2b-256 checksum
How to use checksums
7cafc5afbca6fc03294178a08ed74109273aedc4d62821737a5575f019a5ed28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release history Release notifications | RSS feed

This release

0.8.0 This release

14 release files

0.6.0

13 release files

0.5.0

13 release files

0.4.0

13 release files

0.3.2

13 release files

0.3.0

10 release files

0.2.0

10 release files

0.1.1

10 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page