コード例 #1
0
ファイル: spls.py プロジェクト: wes-brooks/beauty_contest
    def Threshold(self, specificity=0.92):
        self.specificity = specificity

        if not hasattr(self, 'actual'):
            self.GetActual()

        if not hasattr(self, 'fitted'):
            self.GetFitted()

        #Decision threshold is the [specificity] quantile of the fitted values for non-exceedances in the training set.
        try:
            non_exceedances = [
                self.array_fitted[i] for i in range(len(self.actual))
                if self.actual[i] <= self.regulatory_threshold
            ]
            self.threshold = utils.Quantile(non_exceedances, specificity)
            self.specificity = float(
                sum([
                    non_exceedances[i] for i in range(len(non_exceedances))
                    if non_exceedances[i] <= self.threshold
                ])) / len(non_exceedances)

        #This error should only happen if somehow there are no non-exceedances in the training data.
        except ZeroDivisionError:
            self.threshold = self.regulatory_threshold
            self.specificity = 1
コード例 #2
0
ファイル: adapt.py プロジェクト: wes-brooks/beauty_contest
    def Threshold(self, specificity=0.92):
        self.specificity = specificity

        if not hasattr(self, 'actual'):
            self.GetActual()

        if not hasattr(self, 'fitted'):
            self.GetFitted()

        #Decision threshold is the [specificity] quantile of the fitted values for non-exceedances in the training set.
        try:
            print "regulatory threshold: " + str(self.regulatory_threshold)
            print "fitted: " + str(self.fitted)
            print "actual: " + str(self.actual)
            print "condition: " + str([
                k for k in range(len(self.actual))
                if self.actual[k] <= self.regulatory_threshold
            ])
            non_exceedances = [
                self.fitted[k] for k in range(len(self.fitted))
                if self.actual[k] <= self.regulatory_threshold
            ]
            print "ne: " + str(non_exceedances)
            print "spec: " + str(specificity)
            self.threshold = utils.Quantile(non_exceedances, specificity)
            self.specificity = float(
                len([
                    k for k in range(len(non_exceedances))
                    if non_exceedances[k] <= self.threshold
                ])) / len(non_exceedances)

        #This error should only happen if somehow there are no non-exceedances in the training data.
        except ZeroDivisionError:
            self.threshold = self.regulatory_threshold
            self.specificity = 1
コード例 #3
0
ファイル: logistic.py プロジェクト: wes-brooks/beauty_contest
 def Threshold(self, specificity=0.9):
     #Find the optimal decision threshold
     #fitted = np.array(self.model[np.where(np.array(self.model.names)=='fitted.values')[0]])
     self.threshold = utils.Quantile(
         self.fitted[self.data_dictionary[self.target] == 0], specificity)
     try:
         self.specificity = len(
             np.where(
                 self.fitted[self.actual <= self.regulatory_threshold] <=
                 self.threshold)[0]) / len(
                     self.fitted[self.actual <= self.regulatory_threshold])
     except ZeroDivisionError:
         self.specificity = 1
コード例 #4
0
    def Threshold(self, specificity=0.9):
        #Find the optimal decision threshold
        labels = self.data_dictionary[self.target]
        actual = self.actual
        fitted = self.fitted

        indx = [k for k in range(len(labels)) if labels[k] == 0]
        self.threshold = utils.Quantile([fitted[k] for k in indx], specificity)
        try:
            self.specificity = float(
                len([i for i in indx if fitted[i] <= self.threshold
                     ])) / len(indx)
        except ZeroDivisionError:
            self.specificity = 1
コード例 #5
0
ファイル: lasso.py プロジェクト: wes-brooks/beauty_contest
    def Threshold(self, specificity=0.92):
        self.specificity = specificity

        if not hasattr(self, 'actual'):
            self.GetActual()

        if not hasattr(self, 'fitted'):
            self.GetFitted()

        #Decision threshold is the [specificity] quantile of the fitted values for non-exceedances in the training set.
        try:
            non_exceedances = self.array_fitted[np.where(
                self.array_actual <= self.regulatory_threshold)[0]]
            self.threshold = utils.Quantile(non_exceedances, specificity)
            self.specificity = float(sum(
                non_exceedances <= self.threshold)) / non_exceedances.shape[0]

        #This error should only happen if somehow there are no non-exceedances in the training data.
        except ZeroDivisionError:
            self.threshold = self.regulatory_threshold
            self.specificity = 1
コード例 #6
0
    def Threshold(self, specificity=0.9):
        self.specificity = specificity

        if not hasattr(self, 'actual'):
            self.GetActual()

        if not hasattr(self, 'fitted'):
            self.GetFitted()

        #Decision threshold is the [specificity] quantile of the fitted values for non-exceedances in the training set.
        try:
            #non_exceedances = self.array_fitted[np.where(self.array_actual < self.regulatory_threshold)[0]]
            non_exceedances = [
                self.fitted[i] for i in range(len(self.fitted))
                if self.actual[i] < self.regulatory_threshold
            ]
            self.threshold = utils.Quantile(non_exceedances, specificity)
            self.specificity = float(
                len([x for x in non_exceedances if x < self.threshold
                     ])) / len(non_exceedances)

        #This error should only happen if somehow there are no non-exceedances in the training data.
        except IndexError:
            self.threshold = self.regulatory_threshold
コード例 #7
0
ファイル: CNet.py プロジェクト: lyuyanyii/Softatt
    def forward(self,
                x,
                stage=1,
                binary=False,
                single=True,
                noise=False,
                gauss=False,
                R=None,
                UR=None,
                noise_rate=None,
                hard_threshold=False,
                sharp=False,
                quantiled=False,
                CAM=False,
                **kwargs):
        x0, x1, x2, x3, x4, pred0 = self.cls(x)

        if stage == 0:
            return pred0, pred0, None

        if hasattr(self, 'CAM') and self.CAM:
            score0, pred = torch.max(pred0, 1)
            fc = list(self.cls.fc.parameters())[0]
            CAM_mask = torch.cat([
                self._get_mask(x4[i], int(pred[i]), fc, pred0[i])
                for i in range(x.size(0))
            ], 0)
            CAM_mask = F.upsample(CAM_mask, (x.size(2), x.size(3)),
                                  mode='bilinear')
            mask = self.reg(x0, x1, x2, x3, x4, CAM_mask)
        else:
            mask = self.reg(x0, x1, x2, x3, x4)
        mask = F.upsample(mask, (x.size(2), x.size(3)), mode='bilinear')

        y = None
        if noise and not gauss:
            y = x
            y = torch.transpose(y, 0, 1)
            y = torch.transpose(y, 1, 2)
            y = y.contiguous()
            y = y.view(x.size(2), x.size(0) * x.size(1), x.size(3))
            perm0 = torch.randperm(y.size(0)).cuda()
            perm1 = torch.randperm(y.size(1)).cuda()
            perm2 = torch.randperm(y.size(2)).cuda()
            y = y[perm0, :, :]
            y = y[:, perm1, :]
            y = y[:, :, perm2]
            y = y.view(x.size())

            y = torch.transpose(y, 0, 1)
            y = torch.transpose(y, 1, 2)
            y = y.contiguous()
            y = y.view(x.size(2), x.size(0) * x.size(1), x.size(3))
            perm0 = torch.randperm(y.size(0)).cuda()
            perm1 = torch.randperm(y.size(1)).cuda()
            perm2 = torch.randperm(y.size(2)).cuda()
            y = y[perm0, :, :]
            y = y[:, perm1, :]
            y = y[:, :, perm2]
            y = y.view(x.size())
        if gauss:
            y = R
            y *= torch.abs(x)
        if noise:
            y *= noise_rate
        if y is None:
            y = 0

        if quantiled:
            q = utils.Quantile()
            mask = q(mask)
        if binary:
            if not hard_threshold:
                bi = Binarized()
                mask = bi(mask, UR)
            else:
                bi = ThresholdBinarized()
                mask = bi(mask)

        if sharp:
            #mask_P = (mask > 0.1).type( torch.cuda.FloatTensor )
            #mask = mask * mask_P
            sp = sharp_t()
            mask = sp(mask)

        if not gauss:
            x = x * mask.expand(x.size())

        if noise:
            x = x + y * (1 - mask.expand(x.size()))

        if CAM:
            score0, pred = torch.max(pred0, 1)
            fc = list(self.cls.fc.parameters())[0]
            CAM_mask = torch.cat([
                self._get_mask(x4[i], int(pred[i]), fc, pred0[i])
                for i in range(x.size(0))
            ], 0)
            CAM_mask = F.upsample(CAM_mask, (x.size(2), x.size(3)),
                                  mode='bilinear')

        x0, x1, x2, x3, x4, pred1 = self.cls(x)

        if CAM:
            return pred0, pred1, mask, CAM_mask
        return pred0, pred1, mask