Exemplo n.º 1
0
    def evaluate(self, train_index, test_index):

        inner_pool = ThreadPool(self._algorithm_params["n_threads"])
        async_result = {}
        for i in range(self._algorithm_params["grid_search_folds"]):
            async_result[i] = {}

        outer_kernel = self._kernel[train_index, :][:, train_index]
        y_train = self._y[train_index]

        skf = StratifiedKFold(
            n_splits=self._algorithm_params["grid_search_folds"], shuffle=True)
        inner_cv = list(skf.split(np.zeros(len(y_train)), y_train))

        for i in range(len(inner_cv)):
            inner_train_index, inner_test_index = inner_cv[i]

            inner_kernel = outer_kernel[
                inner_train_index, :][:, inner_train_index]
            x_test_inner = outer_kernel[inner_test_index, :][:,
                                                             inner_train_index]
            y_train_inner, y_test_inner = (
                y_train[inner_train_index],
                y_train[inner_test_index],
            )

            for c in self._algorithm_params["c_range"]:
                async_result[i][c] = inner_pool.apply_async(
                    self._grid_search,
                    (inner_kernel, x_test_inner, y_train_inner, y_test_inner,
                     c),
                )
        inner_pool.close()
        inner_pool.join()

        best_parameter = self._select_best_parameter(async_result)
        x_test = self._kernel[test_index, :][:, train_index]
        y_train, y_test = self._y[train_index], self._y[test_index]

        _, y_hat, y_hat_train = self._launch_svc(outer_kernel, x_test, y_train,
                                                 y_test, best_parameter["c"])

        result = dict()
        result["best_parameter"] = best_parameter
        result["evaluation"] = utils.evaluate_prediction_multiclass(
            y_test, y_hat)
        result["evaluation_train"] = utils.evaluate_prediction_multiclass(
            y_train, y_hat_train)
        result["y_hat"] = y_hat
        result["y_hat_train"] = y_hat_train
        result["y"] = y_test
        result["y_train"] = y_train
        result["y_index"] = test_index
        result["x_index"] = train_index

        return result
Exemplo n.º 2
0
    def _grid_search(self, kernel_train, x_test, y_train, y_test, c):
        # y_hat is the value predicted

        _, y_hat, _ = self._launch_svc(kernel_train, x_test, y_train, y_test, c)
        res = utils.evaluate_prediction_multiclass(y_test, y_hat)

        return res['balanced_accuracy']