Exemplo n.º 1
0
def classify_example(x, weights):
    """Given a pattern x, return the predicted label."""

    number_events = len(x.events)
    length_feat= x.length_feat()
    L = []
    
    #weights :
    w = weights

    longueurs = []
    xTilde = x.featuresMatrix()
#SET UP LIKELIHOOD FUNCTION
    for k in range(number_events):
        Mf = np.transpose(xTilde[k])
      #  print Mf.shape #type :np.ndarray, taille: length_feat, length_hyp(k)
        
        if Mf.shape==(0,):
            longueurs.append(0)
            continue
        longueurs.append(Mf.shape[1])
        wC = w[length_feat*k:length_feat*(k+1)]
     #   print len(wC) #type : list longueur: length_feat
        L = np.hstack((L, np.dot(wC, Mf)))
    #print L.shape#normalement c'est length_hyp
    costFunc = L
    constraints = x.constraintsMatrix()
    #print constraints.shape#(t1+t2, length_hyp)
    
#FIND ARGMAX LIKELIHOOD FUNCTION
    output2, msg = cplexSolving.solve(costFunc, constraints, x.singletsSize)#c'est ybar qu'il faut retourner
    
    print "--",
    #print msg
    output = [int(j) for j in output2]
    ybar=[];last=0
    for k in range(number_events):
        ybar.append(list(output[last:last+longueurs[k]]))
        last +=longueurs[k]

#    print "ybar : "
#    for k in range(number_events):
#        print ybar[k]
#    print "loss :", loss(y, ybar, sparm)
    return ybar
Exemplo n.º 2
0
def find_most_violated_constraint_margin(x, y, sm, sparm):
    """Return ybar associated with x's most violated constraint.

    The find most violated constraint function for margin rescaling.
    The default behavior is that this returns the value from the
    general find_most_violated_constraint function."""
    #dans l'exemple donne il y a une iteration sur toutes les classifications possibles pour trouver la contrainte la plus violee
    #nous on ne peut pas faire ca donc on doit utiliser cplex
    number_events = len(y)
    length_feat= sm.size_psi
    sigma = 0
    Y = [] ; L = []
    
    #weights :
    w = sm.w
    
    longueurs = []
    for k in range(number_events):
        sigma+=np.sum(y[k])
        longueurs.append(len(y[k]))
        Y=np.hstack((Y, y[k]))
    
    Y=Y/sigma#type np.array, taille length_hyp,
    xTilde = x.featuresMatrix()
#SET UP COST FUNCTION
    for k in range(number_events):
        Mf = np.transpose(xTilde[k])
        print Mf.shape #type :np.ndarray, taille: length_feat, length_hyp(k)
        wC = w[length_feat*k:length_feat*(k+1)]
        print len(wC) #type : list longueur: length_feat
        L = np.hstack((L, np.dot(wC, Mf)))
    print L.shape#normalement c'est length_hyp
    costFunc = L-Y
    constraints = x.constraintsMatrix()
    print constraints.shape#(t1+t2, length_hyp)
    output = cplexSolving.solve(costFunc, constraints, x.singletsSize)#c'est ybar qu'il faut retourner
    ybar=[];last=0
    for k in range(number_events):
        ybar.append(list(output[last:last+longueurs[k]]))
        last +=longueurs[k]
    
    return ybar