"""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