def post(self, request): api_logger.info('ComputingComputeApi', extra={'data': request.data}) serializer = serializers_request.ComputingComputeSerializer( data=request.data) serializer.is_valid(raise_exception=True) stat_logger.info('Running: %s @ %s' % (serializer.validated_data['actions'], serializer.validated_data['sources'])) task = ComputingTask().delay(input_data=serializer.validated_data, user_id=request.user.id) return Response({'computingHash': task.task_id})
def __init__(self, item=None): super(DataContainerSimple, self).__init__() if not item: stat_logger.info("DataContainerSimple: Using default constructor") else: self.id = item.itemId self.name = item.itemTitle try: self.hasPhotos = bool(item.photosInfo.item) except AttributeError: self.hasPhotos = True # If only main photo exists additional_info = str(bin(item.additionalInfo)[2:]).ljust(5, '0') self.allegroStandard = bool(int(additional_info[0])) self.shipmentFree = bool(int(additional_info[1])) self.sellerRating = item.sellerInfo.userRating for price in item.priceInfo.item: if price.priceType == 'buyNow': self.isBuyNow = True self.priceBuyNow = price.priceValue if price.priceType == 'bidding' or price.priceType == 'withDelivery': self.priceHighestBid = price.priceValue features = str(bin(item.promotionInfo)[2:]).ljust(5, '0') self.featurePromotion = bool(int(features[0])) self.featureHighlight = bool(int(features[1])) self.featureBold = bool(int(features[2])) self.amountStart = item.bidsCount + item.leftCount self.amountLeft = item.leftCount # priceBuyNow for auction type is the minimum price self.minPriceNotReached = True if (not self.isBuyNow and self.priceHighestBid < self.priceBuyNow) else False self.minPriceSet = True if (self.isBuyNow or (not self.isBuyNow and self.priceBuyNow > 0)) else False self.success = True if (self.amountStart > self.amountLeft or (not self.isBuyNow and item.biddersCount > 0)) else False self.used = False if item.conditionInfo == 'new' else True self.offerInfinite = True if item.timeToEnd == u'do wyczerpania przedmiotów' else False if not self.offerInfinite: self.endingDate = item.endingTime
def __init__(self, input_params, data): stat_logger.info('Action.__init__(), input_params: %s' % (input_params.__str__())) self.input_params = input_params self.ADVANCED_MODE = input_params['analysis']['advanced'] self.offers = data['offers'] self.offers_current = data['offers_current'] self.offers_ended = data['offers_ended'] self.user_data = data['user_data'] self.offers_price = [] self.offers_data = [] self.offers_data_keys = [] self.user_offer = [] self.user_offer_data = {} self.correlations = [] self.statistics = {} self.result = {}
def calculate(self): metrics = self.calculate_metrics() stat_logger.info('ShepardAlgorithm.calculateMetrics(): %s' % metrics) weights = self.calculate_weights(metrics) stat_logger.info('ShepardAlgorithm.calculateWeights(): %s' % weights) normalized_weights = self.normalize_weights(weights) stat_logger.info('ShepardAlgorithm.normalizeWeights(): %s' % normalized_weights) predicted_value = self.calculate_predicted_value(normalized_weights) stat_logger.info('ShepardAlgorithm.calculatePredictedValue(): %s' % predicted_value) return predicted_value
def get_data(cls, input_params): a = cls.get_api_class()(input_params['analysis']['advanced']) offers_current = a.search( name=input_params['item']['name'], quantity=cls.get_hint_input_current_quantity(), buy_now_only=True, category_id=input_params['item']['category_id'], used=not bool(input_params['item']['used'] ), # we ask user if it is new, so... negation guarantee=bool(input_params['item']['guarantee'])) offers_ended = a.search( name=input_params['item']['name'], quantity=cls.get_hint_input_ended_quantity(), finished=True, buy_now_only=True, category_id=input_params['item']['category_id'], used=not bool(input_params['item']['used']), guarantee=bool(input_params['item']['guarantee'])) offers = offers_ended + offers_current account_id = a.get_user_id(input_params['account'][cls.SOURCE]) user_data = a.get_user_data(account_id) stat_logger.info( "DataLoader.get_data(): len(offers_current): {}".format( len(offers_current))) stat_logger.info("DataLoader.get_data(): len(offers_ended): {}".format( len(offers_ended))) stat_logger.info("DataLoader.get_data(): user_data: {}".format( str(user_data))) if len(offers_current) == 0 or len( offers_ended) == 0 or user_data is None: raise AllecenaException('Error occurred: Getting data') elif len(offers_current) < cls.get_hint_input_ended_quantity() / 2: api_logger.error('Error occurred: Got not enough data') return { 'offers': offers, 'offers_current': offers_current, 'offers_ended': offers_ended, 'user_data': user_data }
def calculate(self): if settings.HINT_NORMALIZE: self.offers_data = self.data_processor_class.normalize(self.offers_data) stat_logger.info('Hint.calculate(): offers_data normalized: %s' % (str(self.offers_data))) if settings.HINT_CORRELATIONS: self.offers_data = self.data_processor_class.multiply_by_correlations( self.offers_data, self.correlations ) stat_logger.info('Hint.calculate(): offers_data after multiplication by correlations: %s' % (str(self.offers_data))) self.predicted_price = ShepardAlgorithm( self.offers_data, self.user_offer_data, self.offers_price ).calculate stat_logger.info('Hint.calculate(): predicted_price: %f' % self.predicted_price)
def prepare_data(self): self.offers_ended_classification = self.data_processor_class.get_classification( self.offers_ended) stat_logger.info( 'Prediction.prepare_data(): offers_ended_classification: %s' % [str(offer) for offer in self.offers_ended_classification]) for offer in self.offers: self.offers_data.append(offer.to_array(self.EXCLUSION_SET)) self.offers_price.append(offer.price) self.offers_data_keys = self.offers[0].get_param_names( settings.PREDICTION_EXCLUSION_SET) for offer in self.offers_current: self.offers_current_data.append(offer.to_array(self.EXCLUSION_SET)) self.offers_current_price.append(offer.price) for offer in self.offers_ended: self.offers_ended_data.append(offer.to_array(self.EXCLUSION_SET)) self.offers_ended_price.append(offer.price) os = Statistics(self.offers_data, self.offers_data_keys, self.offers_price) self.correlations = os.get_correlations() stat_logger.info('Prediction.prepare_data(): correlations: %s' % (str(self.correlations))) self.statistics = os.get_statistics( settings.PREDICTION_CORRELATIONS_RESULT_NUMBER) stat_logger.info('Prediction.prepare_data(): statistics: %s' % (str(self.statistics))) buyNowImportant = False for corr in self.statistics['correlations']: setattr(self.user_offer, corr[0], 1.0) if corr[0] == 'isBuyNow': buyNowImportant = True if buyNowImportant: self.user_offer.priceBuyNow = round(self.statistics['median'], 2) else: self.user_offer.priceHighestBid = round(self.statistics['median'], 2) self.user_offer_data = self.user_offer.to_array(self.EXCLUSION_SET) stat_logger.info('Prediction.prepare_data(): offers_data: %s' % (str(self.offers_data))) stat_logger.info('Prediction.prepare_data(): offers_data_keys: %s' % (str(self.offers_data_keys))) stat_logger.info('Prediction.prepare_data(): offers_current_data: %s' % (str(self.offers_current_data))) stat_logger.info('Prediction.prepare_data(): offers_ended_data: %s' % (str(self.offers_ended_data))) stat_logger.info('Prediction.prepare_data(): offers_price: %s' % (str(self.offers_price))) stat_logger.info( 'Prediction.prepare_data(): offers_current_price: %s' % (str(self.offers_current_price))) stat_logger.info('Prediction.prepare_data(): offers_ended_price: %s' % (str(self.offers_ended_price))) stat_logger.info('Prediction.prepare_data(): user_offer_data: %s' % (str(self.user_offer_data))) if settings.PREDICTION_CORRELATIONS_FILTERING: self.offers_data = self.data_processor_class.correlations_filter( self.offers_data, self.correlations, settings.PREDICTION_CORRELATIONS_FILTERING_MODE, settings.PREDICTION_CORRELATIONS_FILTERING_THRESHOLD if settings.PREDICTION_CORRELATIONS_FILTERING_MODE == 'threshold' else settings.PREDICTION_CORRELATIONS_FILTERING_NUMBER) self.offers_current_data = self.data_processor_class.correlations_filter( self.offers_current_data, self.correlations, settings.PREDICTION_CORRELATIONS_FILTERING_MODE, settings.PREDICTION_CORRELATIONS_FILTERING_THRESHOLD if settings.PREDICTION_CORRELATIONS_FILTERING_MODE == 'threshold' else settings.PREDICTION_CORRELATIONS_FILTERING_NUMBER) self.offers_ended_data = self.data_processor_class.correlations_filter( self.offers_ended_data, self.correlations, settings.PREDICTION_CORRELATIONS_FILTERING_MODE, settings.PREDICTION_CORRELATIONS_FILTERING_THRESHOLD if settings.PREDICTION_CORRELATIONS_FILTERING_MODE == 'threshold' else settings.PREDICTION_CORRELATIONS_FILTERING_NUMBER) user_offer_data_temp = self.data_processor_class.correlations_filter( [self.user_offer_data], self.correlations, settings.PREDICTION_CORRELATIONS_FILTERING_MODE, settings.PREDICTION_CORRELATIONS_FILTERING_THRESHOLD if settings.PREDICTION_CORRELATIONS_FILTERING_MODE == 'threshold' else settings.PREDICTION_CORRELATIONS_FILTERING_NUMBER) self.user_offer_data = user_offer_data_temp[0] self.correlations = self.data_processor_class.correlations_filter_update( self.correlations, settings.PREDICTION_CORRELATIONS_FILTERING_MODE, settings.PREDICTION_CORRELATIONS_FILTERING_THRESHOLD if settings.PREDICTION_CORRELATIONS_FILTERING_MODE == 'threshold' else settings.PREDICTION_CORRELATIONS_FILTERING_NUMBER) stat_logger.info( 'Prediction.prepare_data(): after correlations filtering: offers_data: %s' % str(self.offers_data)) stat_logger.info( 'Prediction.prepare_data(): after correlations filtering: offers_current_data: %s' % str(self.offers_current_data)) stat_logger.info( 'Prediction.prepare_data(): after correlations filtering: offers_ended_data: %s' % str(self.offers_ended_data)) stat_logger.info( 'Prediction.prepare_data(): after correlations filtering: user_offer_data: %s' % str(self.user_offer_data)) stat_logger.info( 'Prediction.prepare_data(): after correlations filtering: correlations: %s' % str(self.correlations))
def calculate(self): if settings.PREDICTION_NORMALIZE: self.offers_current_data = self.data_processor_class.normalize( self.offers_current_data) self.offers_ended_data = self.data_processor_class.normalize( self.offers_ended_data) self.user_offer_data = self.data_processor_class.normalize( [self.user_offer_data])[0] stat_logger.info( 'Prediction.calculate(): offers_current_data normalized: %s' % (str(self.offers_current_data))) stat_logger.info( 'Prediction.calculate(): offers_ended_data normalized: %s' % (str(self.offers_ended_data))) stat_logger.info( 'Prediction.calculate(): user_offer_data normalized: %s' % (str(self.user_offer_data))) if settings.PREDICTION_CORRELATIONS: self.offers_current_data = self.data_processor_class.multiply_by_correlations( self.offers_current_data, self.correlations) self.offers_ended_data = self.data_processor_class.multiply_by_correlations( self.offers_ended_data, self.correlations) self.user_offer_data = self.data_processor_class.multiply_by_correlations( [self.user_offer_data], self.correlations)[0] stat_logger.info( 'Prediction.calculate(): offers_current_data after multiplication by correlations: %s' % str(self.offers_current_data)) stat_logger.info( 'Prediction.calculate(): offers_ended_data after multiplication by correlations: %s' % str(self.offers_ended_data)) stat_logger.info( 'Prediction.calculate(): user_offer_data after multiplication by correlations: %s' % str(self.user_offer_data)) algorithm = SVMAlgorithm(self.offers_ended_data, self.offers_ended_classification, self.offers_current_data, self.user_offer_data) self.selling_probability = algorithm.calculate stat_logger.info('Prediction.calculate(): selling_probability: %f' % self.selling_probability)
def prepare_data(self): for offer in self.offers: if offer: self.offers_data.append(offer.to_array(self.EXCLUSION_SET)) self.offers_price.append(offer.price) self.offers_data_keys = self.offers[0].get_param_names(self.EXCLUSION_SET) os = Statistics( self.offers_data, self.offers_data_keys, self.offers_price ) self.correlations = os.get_correlations() self.user_offer = self.data_container_class() self.user_offer.build_from_user_data( guarantee=self.input_params['item']['guarantee'], used=self.input_params['item']['used'], sellerShop=self.user_data['shop'], sellerRating=self.user_data['rating'] ) self.user_offer_data = self.user_offer.to_array(self.EXCLUSION_SET) stat_logger.info('Hint.prepare_data(): offers_data: %s' % (self.offers_data.__str__())) stat_logger.info('Hint.prepare_data(): offers_data_keys: %s' % (self.offers_data_keys.__str__())) stat_logger.info('Hint.prepare_data(): offers_price: %s' % (self.offers_price.__str__())) stat_logger.info('Hint.prepare_data(): user_offer_data: %s' % (self.user_offer_data.__str__())) stat_logger.info('Hint.prepare_data(): correlations: %s' % (self.correlations.__str__())) self.statistics = os.get_statistics(settings.HINT_CORRELATIONS_RESULT_NUMBER) stat_logger.info('Hint.prepare_data(): statistics: %s' % (self.statistics.__str__())) if settings.HINT_CORRELATIONS_FILTERING: self.offers_data = self.data_processor_class.correlations_filter( self.offers_data, self.correlations, settings.HINT_CORRELATIONS_FILTERING_MODE, settings.HINT_CORRELATIONS_FILTERING_THRESHOLD if settings.HINT_CORRELATIONS_FILTERING_MODE == 'threshold' else settings.HINT_CORRELATIONS_FILTERING_NUMBER ) user_offer_data_temp = self.data_processor_class.correlations_filter( [self.user_offer_data], self.correlations, settings.HINT_CORRELATIONS_FILTERING_MODE, settings.HINT_CORRELATIONS_FILTERING_THRESHOLD if settings.HINT_CORRELATIONS_FILTERING_MODE == 'threshold' else settings.HINT_CORRELATIONS_FILTERING_NUMBER ) self.user_offer_data = user_offer_data_temp[0] self.correlations = self.data_processor_class.correlations_filter_update( self.correlations, settings.HINT_CORRELATIONS_FILTERING_MODE, settings.HINT_CORRELATIONS_FILTERING_THRESHOLD if settings.HINT_CORRELATIONS_FILTERING_MODE == 'threshold' else settings.HINT_CORRELATIONS_FILTERING_NUMBER ) stat_logger.info('Hint.prepare_data(): after correlations filtering: offers_data: %s' % (str(self.offers_data))) stat_logger.info('Hint.prepare_data(): after correlations filtering: user_offer_data: %s' % (str(self.user_offer_data))) stat_logger.info('Hint.prepare_data(): after correlations filtering: correlations: %s' % (str(self.correlations)))
def calculate(self): svm_engine = svm.SVC(C=settings.PREDICTION_SVM_C, kernel=settings.PREDICTION_SVM_KERNEL, degree=settings.PREDICTION_SVM_DEGREE, gamma=settings.PREDICTION_SVM_GAMMA, coef0=settings.PREDICTION_SVM_COEF0, shrinking=settings.PREDICTION_SVM_SHRINKING, probability=settings.PREDICTION_SVM_PROBABILITY, tol=settings.PREDICTION_SVM_TOL, cache_size=settings.PREDICTION_SVM_CACHE_SIZE, class_weight=settings.PREDICTION_SVM_CLASS_WEIGHT, verbose=settings.PREDICTION_SVM_VERBOSE, max_iter=settings.PREDICTION_SVM_MAX_ITER, random_state=settings.PREDICTION_SVM_RANDOM_STATE) svm_engine.fit(self.historical_data, self.historical_data_classification) current_data_classification = svm_engine.predict(self.current_data) stat_logger.info( 'SVMAlgorithm.calculate(): current_data_classification: %s' % current_data_classification.__str__()) probability_array = svm_engine.predict_proba([self.analyzed_item ]).tolist() stat_logger.info('SVMAlgorithm.calculate(): probability_array: %s' % probability_array.__str__()) probability = probability_array[0][1] stat_logger.info('SVMAlgorithm.calculate(): probability: %s' % probability.__str__()) if settings.PREDICTION_NEIGHBORS: neighbors = self.find_neighbors( current_data_classification, settings.PREDICTION_NEIGHBORS_THRESHOLD, settings.PREDICTION_NEIGHBORS_WIN_ONLY) neighbors_number = len(neighbors) try: probability_coefficient = ( 1.0 - float(neighbors_number) / float(len(self.current_data))) except ZeroDivisionError: probability_coefficient = 1.0 probability *= probability_coefficient stat_logger.info('SVMAlgorithm.calculate(): neighbors: %s' % str(neighbors)) stat_logger.info('SVMAlgorithm.calculate(): neighbors_number: %s' % str(neighbors_number)) stat_logger.info( 'SVMAlgorithm.calculate(): probability_coefficient: %s' % str(probability_coefficient)) stat_logger.info('SVMAlgorithm.calculate(): probability: %s' % str(probability)) return probability
def __init__(self, item=None): super(DataContainerDetailed, self).__init__() if not item: stat_logger.info("DataContainerDetailed: Using default constructor") else: item_info = item.itemInfo item_payment = item.itemPaymentOptions item_postage_options = item.itemPostageOptions.item self.id = item_info.itId self.name = item_info.itName try: self.hasPhotos = bool(item_info.itFotoCount) except AttributeError: self.hasPhotos = True self.shipmentFree = False self.minShipmentPrice = 0.0 for p in item_postage_options: if p.postageFreeShipping: self.shipment_free = True break # binary mask: http://allegro.pl/webapi/faq.php#faq_5 features = str(bin(item.itemInfo.itOptions)[2:]) self.featureBold = bool(int(features[0])) self.featureHighlight = bool(int(features[1])) self.featurePromotion = bool(int(features[2])) self.sellerRating = item_info.itSellerRating self.sellerShop = bool(int(features[3])) self.sellerSuper = False self.isBuyNow = bool(item_info.itBuyNowActive) self.priceBuyNow = float(item_info.itBuyNowPrice) self.priceHighestBid = float(item_info.itPrice) self.endingDate = item_info.itEndingTime self.allegroStandard = bool(item_info.itIsAllegroStandard) self.minShipmentTime = item_info.itOrderFulfillmentTime self.isProduct = bool(item_info.itHasProduct) self.isBrandZone = bool(item_info.itIsBrandZone) self.isForGuests = bool(item_info.itIsForGuests) self.amountStart = item_info.itStartingQuantity self.amountLeft = item_info.itQuantity self.views = item_info.itHitCount self.used = False if item_info.itIsNewUsed == 1 else True self.offerInfinite = True if item_info.itDurationInfo.durationType == 2 else False self.minPriceNotReached = True if float(item_info.itReservePrice) == -2.0 else False self.minPriceSet = True if float(item_info.itReservePrice) == 0.0 else False self.invoice = bool(item_info.itVatInvoice) self.paymentPayu = bool(item_payment.payOptionAllegroPay) self.paymentOnDelivery = bool(item_payment.payOptionOnDelivery) self.paymentTransfer = bool(item_payment.payOptionTransfer) if item_info.itEndingInfo in (1, 2) and ( self.amountLeft < self.amountStart or (not self.isBuyNow and item_info.itBidCount > 0)): self.success = True else: self.success = False try: for attr in item.itemAttribs.item: if attr.attribName == 'Gwarancja' and \ (attr.attribValues.item[0] == 'Serwisowa' or attr.attribValues.item[0] == 'Producenta'): self.guarantee = True except (TypeError, AttributeError): self.guarantee = False