import numpy as np

def vip(x, y, model):
    t = model.x_scores_
    w = model.x_weights_
    q = model.y_loadings_

    m, p = x.shape
    _, h = t.shape

    vips = np.zeros((p,))

    s = np.diag(t.T @ t @ q.T @ q).reshape(h, -1) # variabilité éxpliquée par chaque composante
    total_s = np.sum(s) #C'est la somme totale de la matrice diagonale s, représentant la variance totale expliquée par le modèle.

    for i in range(p):
        weight = np.array([ (w[i,j] / np.linalg.norm(w[:,j]))**2 for j in range(h) ])
        vips[i] = np.sqrt(p*(s.T @ weight)/total_s)

    return vips

def sel_ratio(x, y, model):
    pass