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
Download src/wisp_fast/funnel/shared_gram.py from Zipeng365/WISP: direct link, hf CLI and curl.
- Browser
- Download file 4.89 kB
-
https://huggingface.co/Zipeng365/WISP/resolve/main/src/wisp_fast/funnel/shared_gram.py
- Command line
-
hf download hf://Zipeng365/WISP/src/wisp_fast/funnel/shared_gram.py
-
curl -L -o shared_gram.py https://huggingface.co/Zipeng365/WISP/resolve/main/src/wisp_fast/funnel/shared_gram.py
4.89 kB
| """FP64 weighted Ridge screen and restricted verification using one sample Gram.""" | |
| import numpy as np | |
| from scipy.linalg import cho_factor, cho_solve, eigh | |
| from scipy.linalg.blas import dsymv, dsyrk | |
| from sklearn.preprocessing import LabelBinarizer | |
| from sklearn.utils.validation import check_X_y | |
| from .engine import FastFamily, _training_rows | |
| from .ridge_fast import ScreenedRidgeClassifierCV | |
| def _center_lower(G,w): | |
| """Turn the valid lower triangle of X X.T into weighted centered A A.T.""" | |
| mean=dsymv(1.,G,w/w.sum(),lower=1) | |
| root=np.sqrt(w) | |
| G-=mean[:,None] | |
| G+=((w@mean)/w.sum()-mean)[None,:] | |
| G*=root[:,None];G*=root[None,:] | |
| return G | |
| def _gram_screen(G,w,Y,alphas,*,solution=False): | |
| root=np.sqrt(w) | |
| B=(Y-np.average(Y,axis=0,weights=w))*root[:,None] | |
| _center_lower(G,w) | |
| # H=I-2uu.T maps the intercept direction to e0. The lower triangle of | |
| # (H G H)[1:,1:] suffices for every PRESS denominator and numerator. | |
| u=root.copy();u[0]+=np.linalg.norm(root);u/=np.linalg.norm(u) | |
| Gu=dsymv(1.,G,u,lower=1) | |
| v=2*(Gu-u*(u@Gu)) | |
| G-=u[:,None]*v;G-=v[:,None]*u | |
| B=B[1:]-(2*u[1:,None])*(u@B) | |
| eig,Q=eigh(G[1:,1:],lower=True,check_finite=False,driver='evd') | |
| if eig[0]<-1e-4*min(alphas):raise ArithmeticError('unstable Gram') | |
| inverse=1/(np.maximum(eig,0.)[:,None]+alphas) | |
| rhs=Q.T@B | |
| Z=-u[:,None]*((2*u[1:])@Q) | |
| Z[1:]+=Q | |
| diagonal=(Z*Z)@inverse | |
| if np.min(diagonal)<=32*np.finfo(float).eps:raise ArithmeticError('leverage') | |
| numerator=(Z@(inverse[:,:,None]*rhs[:,None,:]).reshape(len(eig),-1)).reshape(len(w),len(alphas),-1) | |
| losses=np.mean((numerator/diagonal[:,:,None])**2,axis=(0,2)) | |
| if not np.isfinite(losses).all():raise ArithmeticError('nonfinite') | |
| idx=int(losses.argmin()) | |
| if len(losses)>1 and np.partition(losses,1)[1]-losses[idx]<=1e-9*(1+losses[idx]): | |
| raise ArithmeticError('ambiguous alpha') | |
| if solution: | |
| return alphas[idx],losses,Z@(rhs*inverse[:,idx,None]) | |
| return alphas[idx],losses | |
| class GramScreen(ScreenedRidgeClassifierCV): | |
| """Wide full-row CIS designs only. Revert to the screened implementation on a tie.""" | |
| def fit(self,X,y,sample_weight=None): | |
| X,y=check_X_y(X,y,dtype=np.float64,ensure_min_samples=2) | |
| n,d=X.shape | |
| w=np.ones(n) if sample_weight is None else np.asarray(sample_weight,np.float64) | |
| encoder=LabelBinarizer(neg_label=-1,pos_label=1) | |
| Y=encoder.fit_transform(y).astype(np.float64) | |
| if (not self.screening_rows<n<=min(d,4096) or len(encoder.classes_)<2 or w.shape!=(n,) | |
| or not np.isfinite(w).all() or (w<=0).any()): | |
| return super().fit(X,y,sample_weight=sample_weight) | |
| ids=_training_rows(y,self.screening_rows,self.random_state) | |
| enc=np.searchsorted(encoder.classes_,y) | |
| weights=w[ids]*(np.bincount(enc,weights=w)/np.bincount(enc[ids],weights=w[ids]))[enc[ids]] | |
| G=dsyrk(1.,np.ascontiguousarray(X).T,trans=1,lower=1) | |
| try: | |
| alpha,loss=_gram_screen(np.asfortranarray(G[np.ix_(ids,ids)]),weights,Y[ids],np.asarray(self.alphas)) | |
| _center_lower(G,w);G.ravel(order='K')[::n+1]+=alpha | |
| ymean=(w@Y)/w.sum() | |
| root=np.sqrt(w) | |
| dual=cho_solve(cho_factor(G,lower=True,overwrite_a=True,check_finite=False), | |
| root[:,None]*(Y-ymean),check_finite=False) | |
| except (ArithmeticError,np.linalg.LinAlgError): | |
| return super().fit(X,y,sample_weight=sample_weight) | |
| mass=root[:,None]*dual | |
| xm=(w/w.sum())@X | |
| beta=X.T@mass-xm[:,None]*mass.sum(0) | |
| self.classes_=encoder.classes_;self.coef_=beta.T.copy();self.intercept_=ymean-xm@beta | |
| self.alpha_,self.best_score_,self.loo_scores_=alpha,-float(np.min(loss)),-loss | |
| self.solver_used_='shared_gram_screen';self.screening_rows_=len(ids);self.n_features_in_=d | |
| return self | |
| class VerificationFamily(FastFamily): | |
| def _fit_head(self,F,y): | |
| if self.ridge_solver!='screened' or self.template.classifier!='ridge': | |
| return super()._fit_head(F,y) | |
| from sklearn.pipeline import Pipeline | |
| from sklearn.preprocessing import StandardScaler | |
| from sklearn.utils.class_weight import compute_sample_weight | |
| kwargs={} | |
| if getattr(self.template,'class_balance','balanced')=='balanced': | |
| kwargs['clf__sample_weight']=compute_sample_weight('balanced',y) | |
| self.model_=Pipeline([('scale',StandardScaler()),('clf',GramScreen(alphas=np.logspace(-3,3,9)))]) | |
| self.model_.fit(F,y,**kwargs) | |
| def verify_candidate(spec,**options): | |
| from .search import make_candidate | |
| m=make_candidate(spec,**options) | |
| if m.mode=='exact' and m.method_id in ('wisp_rc','wisp_cis') and m.ridge_solver=='screened': | |
| return VerificationFamily(m.template,mode='exact',ridge_solver='screened') | |
| return m | |