Beispiel #1
0
    def export(hp_item: HPItem, path: Path) -> Path:
        PathBuilder.build(path)
        preproc_filename = MLExporter._store_preprocessing_sequence(
            hp_item.hp_setting.preproc_sequence, path).name
        encoder_filename = MLExporter._store_encoder(
            hp_item.hp_setting.encoder, path).name

        hp_item.method.store(path, hp_item.method.get_feature_names())
        labels_with_values = {
            hp_item.method.get_label(): hp_item.method.get_classes()
        }

        method_config = MLMethodConfiguration(
            labels_with_values=labels_with_values,
            software_used=hp_item.method.get_package_info(),
            encoding_name=hp_item.hp_setting.encoder_name,
            encoding_parameters=hp_item.hp_setting.encoder_params,
            encoding_file=encoder_filename,
            encoding_class=type(hp_item.hp_setting.encoder).__name__,
            ml_method=type(hp_item.method).__name__,
            ml_method_name=hp_item.method.name,
            train_dataset_id=hp_item.train_dataset.identifier,
            train_dataset_name=hp_item.train_dataset.name,
            preprocessing_sequence_name=hp_item.hp_setting.
            preproc_sequence_name,
            preprocessing_file=os.path.basename(preproc_filename),
            preprocessing_parameters={
                type(seq).__name__: vars(seq)
                for seq in hp_item.hp_setting.preproc_sequence
            })

        method_config.store(path / 'ml_config.yaml')

        return path
Beispiel #2
0
    def import_hp_setting(config_dir: Path) -> Tuple[HPSetting, Label]:

        config = MLMethodConfiguration()
        config.load(config_dir / 'ml_config.yaml')

        ml_method = ReflectionHandler.get_class_by_name(
            config.ml_method, 'ml_methods/')()
        ml_method.load(config_dir)

        encoder = MLImport.import_encoder(config, config_dir)
        preprocessing_sequence = MLImport.import_preprocessing_sequence(
            config, config_dir)

        labels = list(config.labels_with_values.keys())
        assert len(
            labels
        ) == 1, "MLImport: Multiple labels set in a single ml_config file."

        label = Label(labels[0], config.labels_with_values[labels[0]])

        return HPSetting(
            encoder=encoder,
            encoder_params=config.encoding_parameters,
            encoder_name=config.encoding_name,
            ml_method=ml_method,
            ml_method_name=config.ml_method_name,
            ml_params={},
            preproc_sequence=preprocessing_sequence,
            preproc_sequence_name=config.preprocessing_sequence_name), label
Beispiel #3
0
    def test_load(self):
        x = np.array([[1, 0, 0], [0, 1, 1], [1, 1, 1], [0, 1, 1]])
        y = {"default": np.array([1, 0, 2, 0])}

        knn = KNN()
        knn.fit(EncodedData(examples=sparse.csr_matrix(x), labels=y), Label("default"))

        path = EnvironmentSettings.root_path / "test/tmp/loadtestsklearn/"
        PathBuilder.build(path)

        with open(path / "knn.pickle", "wb") as file:
            pickle.dump(knn.model, file)

        config = MLMethodConfiguration()
        config.labels_with_values = {"default": [0, 1, 2]}
        config.store(path / "config.json")

        knn2 = KNN()
        knn2.load(path)

        self.assertTrue(isinstance(knn2.model, KNeighborsClassifier))

        shutil.rmtree(path)