'Fit the transformer on sparse data.'

def _sparse_fit(self, X, strategy, missing_values, axis):

    if (axis == 1):
        X = X.tocsr()
    else:
        X = X.tocsc()
    if (missing_values == 0):
        n_zeros_axis = np.zeros(X.shape[(not axis)], dtype=int)
    else:
        n_zeros_axis = (X.shape[axis] - np.diff(X.indptr))
    if (strategy == 'mean'):
        if (missing_values != 0):
            n_non_missing = n_zeros_axis
            mask_missing_values = _get_mask(X.data, missing_values)
            mask_valids = np.logical_not(mask_missing_values)
            new_data = X.data.copy()
            new_data[mask_missing_values] = 0
            X = sparse.csc_matrix((new_data, X.indices, X.indptr), copy=False)
            sums = X.sum(axis=0)
            mask_non_zeros = sparse.csc_matrix((mask_valids.astype(np.float64), X.indices, X.indptr), copy=False)
            s = mask_non_zeros.sum(axis=0)
            n_non_missing = np.add(n_non_missing, s)
        else:
            sums = X.sum(axis=axis)
            n_non_missing = np.diff(X.indptr)
        with np.errstate(all='ignore'):
            return (np.ravel(sums) / np.ravel(n_non_missing))
    else:
        columns_all = np.hsplit(X.data, X.indptr[1:(-1)])
        mask_missing_values = _get_mask(X.data, missing_values)
        mask_valids = np.hsplit(np.logical_not(mask_missing_values), X.indptr[1:(-1)])
        columns = [col[mask.astype(bool, copy=False)] for (col, mask) in zip(columns_all, mask_valids)]
        if (strategy == 'median'):
            median = np.empty(len(columns))
            for (i, column) in enumerate(columns):
                median[i] = _get_median(column, n_zeros_axis[i])
            return median
        elif (strategy == 'most_frequent'):
            most_frequent = np.empty(len(columns))
            for (i, column) in enumerate(columns):
                most_frequent[i] = _most_frequent(column, 0, n_zeros_axis[i])
            return most_frequent
