コード例 #1
0
ファイル: brain.py プロジェクト: DerOetzi/learninghouse-core
    def prediction(cls, name: str, request_data: Dict[str, Any]):
        try:
            brain = cls.load_brain(name)
            request_data = DatasetPreprocessing.add_time_information(
                request_data)

            data = pd.DataFrame([request_data])
            prepared_data = DatasetPreprocessing.prepare_prediction(
                brain, data)

            prediction = brain.estimator().predict(prepared_data)

            if (brain.configuration.dependent_encode
                    and brain.configuration.estimator.typed
                    == BrainEstimatorType.CLASSIFIER):
                prediction = brain.dataset.dependent_encoder.inverse_transform(
                    prediction)
                prediction = list(map(bool, prediction))
            else:
                prediction = list(map(float, prediction))

            return BrainPredictionResult(
                brain=brain.info,
                preprocessed=prepared_data.head(1).to_dict('records')[0],
                prediction=prediction[0])
        except FileNotFoundError as exc:
            raise BrainNotTrained(name) from exc
        except Exception as exc:  # pylint: disable=broad-except
            logger.exception(exc)
            raise LearningHouseException() from exc
コード例 #2
0
ファイル: brain.py プロジェクト: DerOetzi/learninghouse-core
 def get_brain(name: str) -> BrainConfiguration:
     try:
         return BrainConfiguration.from_json_file(name)
     except FileNotFoundError as exc:
         raise BrainNoConfiguration(name) from exc
     except Exception as exc:  # pylint: disable=broad-except
         logger.exception(exc)
         raise LearningHouseException() from exc
コード例 #3
0
ファイル: brain.py プロジェクト: DerOetzi/learninghouse-core
    def train(name: str, data: pd.DataFrame) -> BrainInfo:
        try:
            brain = Brain(name)

            if len(data.index) < 10:
                raise BrainNotEnoughData()

            brain, x_train, x_test, y_train, y_test = DatasetPreprocessing.prepare_training(
                brain, data, False)

            estimator = brain.estimator()

            selector = SelectFromModel(estimator)
            selector.fit(x_train, y_train)

            brain.dataset.features = x_train.columns[(
                selector.get_support())].values.tolist()

            brain, x_train, x_test, y_train, y_test = DatasetPreprocessing.prepare_training(
                brain, data, True)

            estimator.fit(x_train, y_train)

            if BrainEstimatorType.CLASSIFIER == brain.configuration.estimator.typed:
                y_pred = estimator.predict(x_test)
                score = accuracy_score(y_test, y_pred)
            else:
                score = estimator.score(x_test, y_test)

            brain.store_trained(x_train.columns.tolist(), len(data.index),
                                score)

            return brain.info
        except FileNotFoundError as exc:
            raise BrainNoConfiguration(name) from exc
        except Exception as exc:  # pylint: disable=broad-except
            logger.exception(exc)
            raise LearningHouseException() from exc