Beispiel #1
0
    def trainModel(self,
                   allTrainData,
                   searchRange=[1, 300],
                   budget=25000 * 1000):
        """
        Train the constant model.

        A best bid price will be returned

        Budget should be in chinese fen * 1000

        :param allTrainData: Training data in matrix
        :param searchRange: Search Grid in array for best bid price. [lowerbound, upperbound]
        :param budget: The budget to use. chinese fen * 1000
        :return: The best constant bid price that obtained the highest CTR

        """
        # goldlabel = np.copy(allTrainData)
        # goldlabel = np.delete(goldlabel, [0, 21, 22], axis=1)# remove 'click','bidprice','payprice'

        bestBid = 0
        bestCTR = 0
        bestClick = 0
        # print(goldlabel.shape)
        for bid in range(searchRange[0], searchRange[1]):
            # for bid in range(1000, 1001):  # To test cutting back budget
            self.defaultBid = bid
            # start_time = time.time()
            bids = self.getBidPrice(allTrainData.bidid)
            # print('Metrics np.apply_along_axis time: {} seconds'.format(round(time.time() - start_time, 2)))
            # myEvaluator = Evaluator(budget, bids, allTrainData)
            myEvaluator = Evaluator()
            resultDict = myEvaluator.computePerformanceMetricsDF(
                budget, bids, allTrainData)

            # if resultDict['won'] != 0:
            #     print("Constant bid: {} CTR: {}".format(self.defaultBid, resultDict['click'] / resultDict['won']))
            # else:
            #     print("Constant bid: {} CTR: not computed as no. of won is 0".format(self.defaultBid))

            # if resultDict['won'] != 0:
            #     currentCTR = resultDict['click'] / resultDict['won']
            # else:
            #     continue

            # if currentCTR > bestCTR:
            #     bestCTR = currentCTR
            #     bestBid = bid

            if resultDict['won'] != 0:
                print("Constant bid: {} Clicks: {}".format(
                    self.defaultBid, resultDict['click']))
                print("bestBid: ", bestBid)
                print("bestClick: ", bestClick)
            else:
                print("Constant bid: {} CTR: not computed as no. of won is 0".
                      format(self.defaultBid))

            if resultDict['won'] != 0:
                currentClick = resultDict['click']
            else:
                continue

            if currentClick > bestClick:
                bestClick = currentClick
                bestBid = bid

        # print("bestBid: ", bestBid)
        # print("bestCTR: ", bestCTR)
        print("bestBid: ", bestBid)
        print("bestClick: ", bestClick)

        self.defaultBid = bestBid

        return self.defaultBid