(self, label_index, unlabel_index, oracles, pred_unlab, n_neighbors=10, eval_cost=False)
return Q_table, self._oracle_ind_name_dict
Query from oracles. Return the Q table and the oracle name/index of each row of Q_table.

        Parameters
        ----------
        label_index: {list, np.ndarray, IndexCollection}
            The indexes of labeled samples.

        unlabel_index: {list, np.ndarray, IndexCollection}
            The indexes of unlabeled samples.

        oracles: {list, alipy.oracle.Oracles}
            An alipy.oracle.Oracle object that contains all the
            available oracles or a list of oracles.
            Each oracle should be a alipy.oracle.Oracle object.

        predict: : 2d array, shape [n_samples, n_classes]
            The probabilistic prediction matrix for the unlabeled set.

        n_neighbors: int, optional (default=10)
            How many neighbors of the selected instance will be used
            to evaluate the oracles.

        eval_cost: bool, optional (default=False)
            To evaluate the cost of oracles or use the cost provided by oracles.

        Returns
        -------
        Q_table: 2D array
            The Q table.

        oracle_ind_name_dict: dict
            The oracle name/index of each row of Q_table.
Query from oracles. Return the Q table and the oracle name/index of each row of Q_table.
Query from oracles . Return the Q table and the oracle name / index of each row of Q_table .
def _calc_Q_table(self, label_index, unlabel_index, oracles, pred_unlab, n_neighbors=10, eval_cost=False):
        """Query from oracles. Return the Q table and the oracle name/index of each row of Q_table.

        Parameters
        ----------
        label_index: {list, np.ndarray, IndexCollection}
            The indexes of labeled samples.

        unlabel_index: {list, np.ndarray, IndexCollection}
            The indexes of unlabeled samples.

        oracles: {list, alipy.oracle.Oracles}
            An alipy.oracle.Oracle object that contains all the
            available oracles or a list of oracles.
            Each oracle should be a alipy.oracle.Oracle object.

        predict: : 2d array, shape [n_samples, n_classes]
            The probabilistic prediction matrix for the unlabeled set.

        n_neighbors: int, optional (default=10)
            How many neighbors of the selected instance will be used
            to evaluate the oracles.

        eval_cost: bool, optional (default=False)
            To evaluate the cost of oracles or use the cost provided by oracles.

        Returns
        -------
        Q_table: 2D array
            The Q table.

        oracle_ind_name_dict: dict
            The oracle name/index of each row of Q_table.
        """
        # Check parameter and initialize variables
        if self.X is None or self.y is None:
            raise Exception('Data matrix is not provided, use select_by_prediction_mat() instead.')
        assert (isinstance(unlabel_index, collections.Iterable))
        assert (isinstance(label_index, collections.Iterable))
        unlabel_index = np.asarray(unlabel_index)
        label_index = np.asarray(label_index)
        num_of_neighbors = n_neighbors
        if len(unlabel_index) <= 1:
            return unlabel_index

        Q_table = np.zeros((len(oracles), len(unlabel_index)))  # row:oracle, col:ins
        spv = np.shape(pred_unlab)
        # calc least_confident
        rx = np.partition(pred_unlab, spv[1] - 1, axis=1)
        rx = 1 - rx[:, spv[1] - 1]
        for unlab_ind, unlab_ins_ind in enumerate(unlabel_index):
            # evaluate oracles for each instance
            nn_dist, nn_of_selected_ins = self._nntree.kneighbors(X=self.X[unlab_ins_ind].reshape(1, -1),
                                                                  n_neighbors=num_of_neighbors,
                                                                  return_distance=True)
            nn_dist = nn_dist[0]
            nn_of_selected_ins = nn_of_selected_ins[0]
            nn_of_selected_ins = self._ini_ind[nn_of_selected_ins]  # map to the original population
            oracles_score = []
            for ora_ind, ora_name in enumerate(self._oracles_iterset):
                # calc q_i(x), expertise of this instance
                oracle = oracles[ora_name]
                labels, cost = oracle.query_by_index(nn_of_selected_ins)
                oracles_score.append(sum([nn_dist[i] * (labels[i] == self.y[nn_of_selected_ins[i]]) for i in
                                          range(num_of_neighbors)]) / num_of_neighbors)

                # calc c_i, cost of each labeler
                labels, cost = oracle.query_by_index(label_index)
                if eval_cost:
                    oracles_cost = sum([labels[i] == self.y[label_index[i]] for i in range(len(label_index))]) / len(label_index)
                else:
                    oracles_cost = cost[0]
                Q_table[ora_ind, unlab_ind] = oracles_score[ora_ind] * rx[unlab_ind] / max(oracles_cost, 0.0001)

        return Q_table, self._oracle_ind_name_dict
def _calc_Q_table ( self , label_index , unlabel_index , oracles , pred_unlab , n_neighbors = 10 , eval_cost = False ) : # Check parameter and initialize variables if self . X is None or self . y is None : raise Exception ( 'Data matrix is not provided, use select_by_prediction_mat() instead.' ) assert ( isinstance ( unlabel_index , collections . Iterable ) ) assert ( isinstance ( label_index , collections . Iterable ) ) unlabel_index = np . asarray ( unlabel_index ) label_index = np . asarray ( label_index ) num_of_neighbors = n_neighbors if len ( unlabel_index ) <= 1 : return unlabel_index Q_table = np . zeros ( ( len ( oracles ) , len ( unlabel_index ) ) ) # row:oracle, col:ins spv = np . shape ( pred_unlab ) # calc least_confident rx = np . partition ( pred_unlab , spv [ 1 ] - 1 , axis = 1 ) rx = 1 - rx [ : , spv [ 1 ] - 1 ] for unlab_ind , unlab_ins_ind in enumerate ( unlabel_index ) : # evaluate oracles for each instance nn_dist , nn_of_selected_ins = self . _nntree . kneighbors ( X = self . X [ unlab_ins_ind ] . reshape ( 1 , - 1 ) , n_neighbors = num_of_neighbors , return_distance = True ) nn_dist = nn_dist [ 0 ] nn_of_selected_ins = nn_of_selected_ins [ 0 ] nn_of_selected_ins = self . _ini_ind [ nn_of_selected_ins ] # map to the original population oracles_score = [ ] for ora_ind , ora_name in enumerate ( self . _oracles_iterset ) : # calc q_i(x), expertise of this instance oracle = oracles [ ora_name ] labels , cost = oracle . query_by_index ( nn_of_selected_ins ) oracles_score . append ( sum ( [ nn_dist [ i ] * ( labels [ i ] == self . y [ nn_of_selected_ins [ i ] ] ) for i in range ( num_of_neighbors ) ] ) / num_of_neighbors ) # calc c_i, cost of each labeler labels , cost = oracle . query_by_index ( label_index ) if eval_cost : oracles_cost = sum ( [ labels [ i ] == self . y [ label_index [ i ] ] for i in range ( len ( label_index ) ) ] ) / len ( label_index ) else : oracles_cost = cost [ 0 ] Q_table [ ora_ind , unlab_ind ] = oracles_score [ ora_ind ] * rx [ unlab_ind ] / max ( oracles_cost , 0.0001 ) return Q_table , self . _oracle_ind_name_dict