<commit_before>from builtins import range
from ..base import MLClassifierBase
from ..utils import get_matrix_in_format
from sklearn.neighbors import NearestNeighbors
import scipy.sparse as sparse
import numpy as np

class BinaryRelevanceKNN(MLClassifierBase):
    """Binary Relevance adapted kNN Multi-Label Classifier."""

    def __init__(self, k = 10):
        """Initializes the classifier

        Attributes
        ----------
        k : int (default is 10)
            number of neighbours
        """
        super(BinaryRelevanceKNN, self).__init__()
        self.k = k # Number of neighbours
        self.copyable_attrs = ['k']

    def fit(self, X, y):
        """Fit classifier with training data

        Internally this method uses a sparse CSC representation for y
        (:class:`scipy.sparse.csc_matrix`).

        Parameters
        ----------
        X : numpy.ndarray or scipy.sparse
            input features, can be a dense or sparse matrix of size
            :code:`(n_samples, n_features)`
        y : numpy.ndaarray or scipy.sparse {0,1}
            binary indicator matrix with label assignments.

        Returns
        -------
        skmultilearn.adapt.brknn.BinaryRelevanceKNN
            fitted instance of self
        """
        self.train_labelspace = get_matrix_in_format(y, 'csc')
        self.num_instances = self.train_labelspace.shape[0]
        self.num_labels = self.train_labelspace.shape[1]
        self.knn = NearestNeighbors(self.k).fit(X)
        return self

    def compute_confidences(self):
        """Helper function to compute for the confidences

        Performs a computation involving the percent of neighbours that
        have a given label assigned, then summed over each label columns
        after subsetting for neighbours.Then normalization is done.
        """
        self.confidences = np.vstack([self.train_labelspace[n,:].tocsc().sum(axis=0) / float(self.num_labels) for n in self.neighbors])
        return self.confidences

    def predict(self, X):
        """Predict labels for X<commit_msg>fix normalization of confidence computation<commit_after>from builtins import range
from ..base import MLClassifierBase
from ..utils import get_matrix_in_format
from sklearn.neighbors import NearestNeighbors
import scipy.sparse as sparse
import numpy as np

class BinaryRelevanceKNN(MLClassifierBase):
    """Binary Relevance adapted kNN Multi-Label Classifier."""

    def __init__(self, k = 10):
        """Initializes the classifier

        Attributes
        ----------
        k : int (default is 10)
            number of neighbours
        """
        super(BinaryRelevanceKNN, self).__init__()
        self.k = k # Number of neighbours
        self.copyable_attrs = ['k']

    def fit(self, X, y):
        """Fit classifier with training data

        Internally this method uses a sparse CSC representation for y
        (:class:`scipy.sparse.csc_matrix`).

        Parameters
        ----------
        X : numpy.ndarray or scipy.sparse
            input features, can be a dense or sparse matrix of size
            :code:`(n_samples, n_features)`
        y : numpy.ndaarray or scipy.sparse {0,1}
            binary indicator matrix with label assignments.

        Returns
        -------
        skmultilearn.adapt.brknn.BinaryRelevanceKNN
            fitted instance of self
        """
        self.train_labelspace = get_matrix_in_format(y, 'csc')
        self.num_instances = self.train_labelspace.shape[0]
        self.num_labels = self.train_labelspace.shape[1]
        self.knn = NearestNeighbors(self.k).fit(X)
        return self

    def compute_confidences(self):
        """Helper function to compute for the confidences

        Performs a computation involving the percent of neighbours that
        have a given label assigned, then summed over each label columns
        after subsetting for neighbours.Then normalization is done.
        """
        self.confidences = np.vstack([self.train_labelspace[n,:].tocsc().sum(axis=0) / self.k for n in self.neighbors])
        return self.confidences

    def predict(self, X):
        """Predict labels for X
{'fraction_non_alphanumeric': 0.09007707129094412, 'fraction_numerical': 0.004335260115606936, 'mean_word_length': 2.3957481602616517, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5080557', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1125, 'n_words': 353}