Instructions to use Zipeng365/WISP with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Scikit-learn
How to use Zipeng365/WISP with Scikit-learn:
from huggingface_hub import hf_hub_download import joblib model = joblib.load( hf_hub_download("Zipeng365/WISP", "sklearn_model.joblib") ) # only load pickle files from sources you trust # read more about it here https://skops.readthedocs.io/en/stable/persistence.html - Notebooks
- Google Colab
- Kaggle
File size: 2,587 Bytes
ac7f7cb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | """Optional zero-copy C traversal over sklearn's fitted single-output forests."""
import ctypes
from functools import lru_cache
from .native import lookup
import numpy as np
@lru_cache(maxsize=1)
def _native():
fn = lookup("wisp_forest")
if fn is None:
return None
arr = np.ctypeslib.ndpointer
size = ctypes.c_ssize_t
fn.argtypes = [arr(dtype=np.float32, ndim=2, flags="C_CONTIGUOUS"), size, size,
arr(dtype=np.uintp, ndim=2, flags="C_CONTIGUOUS"), size, size,
arr(dtype=np.float64, ndim=2, flags="C_CONTIGUOUS")]
fn.restype = None
return fn
class ForestPlan:
"""Transient table of eight machine words per tree. The tree objects own the buffers."""
def __init__(self, head):
self.trees = [e.tree_ for e in head.estimators_]
self.table = None
self._views = []
if _native() is None or head.n_outputs_ != 1 or len(self.trees) > 32768:
return
width = len(head.classes_)
rows = []
for t in self.trees:
fields = [t.children_left, t.children_right, t.feature, t.threshold,
t.value, getattr(t, "missing_go_to_left", None)]
left, right, feat, thresh, value, missing = fields
if (missing is None or missing.dtype != np.uint8 or thresh.dtype != np.float64
or any(a.dtype != np.intp for a in [left, right, feat])
or value.dtype != np.float64 or value.shape != (t.node_count, 1, width)
or value.strides[0] < width * 8 or value.strides[-1] != 8
or not value.flags.aligned or left.strides[0] <= 0
or any(a.shape != (t.node_count,) or a.strides != left.strides
for a in (right, feat, thresh, missing))):
return
self._views.append(fields) # keep every buffer alive for the plan's lifetime
rows.append([a.ctypes.data for a in fields] + [left.strides[0], value.strides[0]])
self.table = np.asarray(rows, dtype=np.uintp)
def local_proba(head, features, plan):
# sklearn performs its normal feature-count/dtype and finite input checks.
X = head._validate_X_predict(features)
k = len(head.classes_)
out = np.zeros((len(X), k), dtype=np.float64)
if plan.table is not None:
_native()(np.ascontiguousarray(X), len(X), X.shape[1], plan.table, len(plan.trees), k, out)
else:
for tree in plan.trees:
out += tree.predict(X)[:, :k]
out /= len(plan.trees)
return out
|