Python 将编码类目标转换为K个编码目标中的1个

Python 将编码类目标转换为K个编码目标中的1个,python,neural-network,Python,Neural Network,我正在研究手写数字的MNIST数据集,我有一个任务,在那里我得到一个目标数组 例如 我需要将其转换为一对一编码: e、 g 我得到了一个使用以下方法的解决方案: def to_one_of_k(self, int_targets): """Converts integer coded class target to 1 of K coded targets. Args: int_targets (nda

我正在研究手写数字的MNIST数据集,我有一个任务,在那里我得到一个目标数组 例如

我需要将其转换为一对一编码: e、 g

我得到了一个使用以下方法的解决方案:

    def to_one_of_k(self, int_targets):
        """Converts integer coded class target to 1 of K coded targets.
        Args:
            int_targets (ndarray): Array of integer coded class targets (i.e.
                where an integer from 0 to `num_classes` - 1 is used to
                indicate which is the correct class). This should be of shape
                (num_data,).
        Returns:
            Array of 1 of K coded targets i.e. an array of shape
            (num_data, num_classes) where for each row all elements are equal
            to zero except for the column corresponding to the correct class
            which is equal to one.
        """
        one_of_k_targets = np.zeros((int_targets.shape[0], self.num_classes))
        one_of_k_targets[range(int_targets.shape[0]), int_targets] = 1
        return one_of_k_targets
有人能给我解释一下这个代码吗?我对Python很熟悉,只是对数学不太熟悉

[ [ 0 1 0 0 0 0 0 0 0 0] [ ... ] ..... [....] [....] ] #(in this case the first array would represent 1)
    def to_one_of_k(self, int_targets):
        """Converts integer coded class target to 1 of K coded targets.
        Args:
            int_targets (ndarray): Array of integer coded class targets (i.e.
                where an integer from 0 to `num_classes` - 1 is used to
                indicate which is the correct class). This should be of shape
                (num_data,).
        Returns:
            Array of 1 of K coded targets i.e. an array of shape
            (num_data, num_classes) where for each row all elements are equal
            to zero except for the column corresponding to the correct class
            which is equal to one.
        """
        one_of_k_targets = np.zeros((int_targets.shape[0], self.num_classes))
        one_of_k_targets[range(int_targets.shape[0]), int_targets] = 1
        return one_of_k_targets