示例#1
0
文件: PAV2.py 项目: motrom/mmcode
 def permute(self, x):
     prmt = range(len(self.Y))  # increasing order
     prmt = np.array(prmt)
     counter = 0
     for pocketList in margins(self.Y):
         pocketlen = len(pocketList)
         if pocketlen > 1:
             pocket = np.arange(counter, counter + pocketlen)
             sortedInds = np.argsort(x[pocket] * self.direction)
             prmt[pocket] = pocket[sortedInds]
         counter += pocketlen
     return prmt
示例#2
0
文件: PAV.py 项目: motrom/mmcode
 def __init__(self,Y,cost,order="partial",margin="soft"):
     if order != "partial" and order != "total":
         raise ValueError("order must be 'partial' or 'total'")
     if margin != "hard" and margin != "soft":
         raise ValueError("margin must be 'hard' or 'soft'")
     if np.all(np.diff(Y)>=0):
         self.direction = 1 # increasing order
     elif np.all(np.diff(Y)<=0):
         self.direction = -1 # decreasing order
     else:
         raise ValueError("Y must be fully sorted")
         
     self.Y = Y
     self.order = order
     self.margin = margin
     if order == "partial":
         Y = margins(Y)
         partialBins = list((len(y) for y in Y)) # size of each partially-ordered group
         costVector = []
         for k in range(len(partialBins)):
             if k == 0:
                 value = -partialBins[k+1]
             elif k == len(partialBins)-1:
                 value = partialBins[k-1]
             else:
                 value = partialBins[k-1] - partialBins[k+1]
             costVector += [value]*partialBins[k]
     else:
         # total order: [-1 0 0 0 0 0 1] or similar
         costVector = np.array([0]*(len(Y)))
         costVector[0] = -1
         costVector[-1] = 1
     self.costVector = np.array(costVector) * cost * self.direction
     if margin == 'soft':
         self.costVector = self.costVector / 2.
     
     if self.direction < 0:
         self.IR = sklearn.isotonic.IsotonicRegression(increasing=False)
     else:
         self.IR = sklearn.isotonic.IsotonicRegression()