示例#1
0
    def __init__(self, batch_size):
        """

        :param batch_size: Integer value, defined by the competition and available at competition page
        :param server_port: Connection string ('IP:port')
        :param user_email: String, e-mail used for registering to competition
        :param token: String, received after subscription to a competition
        :param competition_code: String, received after subscription to a competition
        :param first_prediction: Prediction, class generated from .proto file. Used to initiate communication with the
        server. Not influencing the results. Should contain appropriate fields from .proto file.
        """

        # mondrian
        self.mfr = MondrianForestRegressor(random_state=1,
                                           n_estimators=100,
                                           bootstrap=True)
        self.previous_target_3 = pd.Series()
        self.features_for_rowID = Queue()
        self.previous_train_batch = np.array([-1, -1, -1, -1, -1])
        # rrcf
        self.num_trees = 40
        self.tree_size = 256
        self.forest = []
        self.avg_codisp = {}
        self.curr_sum = 0
        self.curr_num = 0
        self.idx = 0

        self._init_modeling()

        a = 1
        while a == 1:
            print("wait")
            now = datetime.datetime.now()
            starttime = now.replace(hour=21, minute=0, second=0, microsecond=0)
            if now >= starttime:
                print(now)
                print("시작!")
                break

        self.batch_size = batch_size
        self.stop_thread = False
        self.predictions_to_send = Queue()
        self.channel = grpc.insecure_channel(
            'app.streaming-challenge.com:50051')
        self.stub = file_pb2_grpc.DataStreamerStub(self.channel)
        self.user_email = '*****@*****.**'
        self.competition_code = 'jR'  #oj
        self.token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoieXU5OTA1MjRAZ21haWwuY29tIiwiY29tcGV0aXRpb25faWQiOiIxIn0.B7CAjAsEbTjp4l1K4GR1Y0IJZj6_mKEbKBXsXXJmGBg'
        self.predictions_to_send.put(
            file_pb2.Prediction(rowID=1000, target=333))
        self.metadata = self.create_metadata(user_id=self.user_email,
                                             code=self.competition_code,
                                             token=self.token)
示例#2
0
    def __init__(self, batch_size):
        """

        :param batch_size: Integer value, defined by the competition and available at competition page
        :param server_port: Connection string ('IP:port')
        :param user_email: String, e-mail used for registering to competition
        :param token: String, received after subscription to a competition
        :param competition_code: String, received after subscription to a competition
        :param first_prediction: Prediction, class generated from .proto file. Used to initiate communication with the
        server. Not influencing the results. Should contain appropriate fields from .proto file.
        """

        self.batch_size = batch_size
        self.stop_thread = False
        self.predictions_to_send = Queue()
        self.channel = grpc.insecure_channel('app.streaming-challenge.com:50051')
        self.stub = file_pb2_grpc.DataStreamerStub(self.channel)
        self.user_email = '*****@*****.**'
        self.competition_code = 'oj'
        self.token = 'eyJ0eXAiOiJKV1QiLCVmPCIZUIfk094SMnIpo5QMp1M'
        self.predictions_to_send.put(file_pb2.Prediction(rowID=1000, Target=333))
        self.metadata = self.create_metadata(user_id=self.user_email, code=self.competition_code, token=self.token)
示例#3
0
    def generate_predictions(self):
        """
        Sending predictions

        :return: Prediction
        """
        while True:
            try:
                pred = self.predictions_to_send.pop()
                prediction_as_dict = ast.literal_eval(pred)
                new_dict = {}
                for key, value in prediction_as_dict.items():
                    if isinstance(value, list) and len(value) == 1:
                        new_dict[key] = value[0]
                    else:
                        new_dict[key] = value
                prediction = file_pb2.Prediction(**new_dict)
                # print("Prediction: ", prediction)
                yield prediction
                if self.stop_thread and not self.predictions_to_send:
                    break
            except Exception as e:
                pass
示例#4
0
    def loop_messages(self):
        """
        Getting messages (data instances) from the stream.

        :return:
        """
        messages = self.stub.sendData(self.generate_predictions(), metadata=self.metadata)
        try:
            for message in messages:
                message = json.loads(json_format.MessageToJson(message))
                if message['tag'] == 'TEST':
                    # v = message['Target'] + 10
                    v = 54321
                    prediction = file_pb2.Prediction(rowID=message['rowID'], Target=v)
                    self.predictions_to_send.put(prediction)
                if message['tag'] == 'INIT':
                    i = 1
                if message['tag'] == 'TRAIN':
                    i = 1
                if self.stop_thread:
                    break
        except Exception as e:
            print(str(e))
            pass
示例#5
0
    def loop_messages(self):
        """
        Getting messages (data instances) from the stream.

        :return:
        """

        #generate prediction -> get prediction from predictions_to_send one by one ans SEND to server

        messages = self.stub.sendData(self.generate_predictions(),
                                      metadata=self.metadata)
        test_idx = 0
        test_feature = self.previous_target_3

        try:
            for message in messages:

                message = json.loads(json_format.MessageToJson(message))
                print("message:", message)
                if message['tag'] == 'TEST':
                    print('test')
                    test_feature['prev3'] = test_feature['prev2']
                    test_feature['prev2'] = test_feature['prev1']
                    test_feature['prev1'] = float(
                        self.previous_train_batch[test_idx])

                    pred = self.mfr.predict(test_feature.values.reshape(1, -1))
                    prediction = file_pb2.Prediction(rowID=message['rowID'],
                                                     target=pred)
                    self.predictions_to_send.put(prediction)

                    #
                    test_idx = (test_idx + 1) % 5
                    print(test_idx)

                    print('test end')

                if message['tag'] == 'TRAIN':
                    print('train')
                    #training data to train my model.

                    target = message['target']
                    target = self.anomaly_detection(target)

                    print(self.previous_target_3)

                    # i-5, i-6, i-7 의 값을 갖고 학습
                    if self.previous_target_3['prev3'] < 0:
                        self.previous_target_3['prev3'] = target
                    elif self.previous_target_3['prev2'] < 0:
                        self.previous_target_3['prev2'] = target
                    elif self.previous_target_3['prev1'] < 0:
                        self.previous_target_3['prev1'] = target
                    else:
                        print('else')
                        #replace the oldest value
                        self.previous_target_3[
                            'prev3'] = self.previous_target_3['prev2']  #-7
                        self.previous_target_3[
                            'prev2'] = self.previous_target_3['prev1']  #-6
                        self.previous_target_3['prev1'] = float(
                            self.previous_train_batch[0])  #-5

                        # partial fit with 3 previous values as feature
                        self.mfr.partial_fit(
                            self.previous_target_3.values.reshape(1, -1),
                            [target])

                        #현재 train data의 target값 저장
                        self.previous_train_batch = np.roll(
                            self.previous_train_batch, -1)
                        self.previous_train_batch[4] = target

                        print('else end')

                    print('train end')

                if self.stop_thread: break

        except Exception as e:
            print(str(e))
            pass