def check_sparse_model(self, model):
        # Given
        model.fit(self.X_sparse, self.y_sparse)

        # When
        serialized_model = skljson.to_dict(model)
        deserialized_model = skljson.from_dict(serialized_model)

        # Then
        expected_predictions = model.predict(self.X_sparse)
        actual_predictions = deserialized_model.predict(self.X_sparse)

        testing.assert_array_equal(expected_predictions, actual_predictions)
Beispiel #2
0
    def check_sparse_model(self, model, abs=False):
        # Given
        if abs:
            model.fit(np.absolute(self.X_sparse), self.y_sparse)
        else:
            model.fit(self.X_sparse, self.y_sparse)

        # When
        serialized_model = skljson.to_dict(model)
        deserialized_model = skljson.from_dict(serialized_model)

        # Then
        expected_predictions = model.predict(self.X)
        actual_predictions = deserialized_model.predict(self.X)

        testing.assert_array_equal(expected_predictions, actual_predictions)
    def check_model(self, model):
        expected_vectors = model.fit_transform(self.X)

        serialized_model = skljson.to_dict(model)
        deserialized_model = skljson.from_dict(serialized_model)

        actual_vectors = deserialized_model.fit_transform(self.X)

        if model.sparse:
            testing.assert_array_equal(expected_vectors.indptr,
                                       actual_vectors.indptr)
            testing.assert_array_equal(expected_vectors.indices,
                                       actual_vectors.indices)
            testing.assert_array_equal(expected_vectors.data,
                                       actual_vectors.data)
        else:
            testing.assert_array_equal(expected_vectors, actual_vectors)
Beispiel #4
0
def open_model(model_path):
    """Open and return a model from json file
    
    :param model_path: path to the model
    """
    if model_path.endswith('gz'):
        with gzip.open(model_path, 'r') as f:
            model = f.read()
            model = json.loads(model.decode('utf-8'))
            model = from_dict(model)
        return model

    else:
        with open(model_path, 'r') as f:
            a = f.readline()

        if a.startswith('{"learner"'):
            model = xgb.Booster()
            model.load_model(model_path)
            return model

        else:
            model = from_json(model_path)
            return model
Beispiel #5
0
def get_tree(tree_json):
    return skljson.from_dict(tree_json)