Question: raise ValueError('All labels must be in [0, n labels). '                                  'Got %d < 0' % np.min(labels)) <ins>        if n_labels is not None:</ins>             y_true = y_true[:, labels[:n_labels]]             y_pred = y_pred[:, labels[:n_labels]]

BUG Inconsistent f1score behavior when combining label indicator input with labels attribute When using label indicator inputs for ypred and ytrue, metrics.f1score calculates the macro average over all label-specific f-scores whenever the labels parameter includes column index 0. It should only average over the label-specific scores indicated by the labels parameter, as it does when 0 is not present in the labels parameter. <!-- Instructions For Filing a Bug: --<!-- Please paste or specifically describe the actual output or traceback. --Windows-7-6.1.7601-SP1 Python 3.5.3 (default, May 15 2017, 10:43:23) [MSC v.1900 64 bit (AMD64)] NumPy 1.13.1 SciPy 0.19.0 Scikit-Learn 0.19.0 <!-- Thanks for contributing! --


Question: ...         pass     ...     print(repr(w[-1].message))     ... # doctest: +NORMALIZE_WHITESPACE    FitFailedWarning("Classifier fit failed. The score on this train-test    partition for these parameters will be set to 0.000000. Details:    \\nValueError('Penalty term must be positive; got (C=-2)',)",)    FitFailedWarning('Estimator fit failed. The score on this train-test    partition for these parameters will be set to 0.000000.    Details: \\nValueError: Penalty term must be positive; got (C=-2)\\n',)      .. versionchanged:: 0.18        Moved from sklearn.cross_validation.diff --git a/sklearn/model_selection/_validation.py b/sklearn/model_selection/_validation.py-- a/sklearn/model_selection/_validation.py++ b/sklearn/model_selection/_validation.py

TfidfVectorizer dtype argument ignored TfidfVectorizer's fit/fittransform output is always np.float64 instead of the specified dtype FitFailedWarning raised by cross validation could do with better message Current message says "Classifier fit failed" but the estimator is not necessarily a classifier. (Perhaps that's too pedantic of me...) is not technically the best way to display an error message. We could either use and include the whole traceback; or we can use to print it properly (though I think this is the same as ). Another thing we can consider, now that provides structured results to and , is to store the full traceback in some array of .


Question: import warnings import numbers import time<ins>from traceback import format_exception_only</ins>  import numpy as np import scipy.sparse as sp


Question: test_scores = error_score                 if return_train_score:                     train_scores = error_score            warnings.warn("Classifier fit failed. The score on this train-test"            warnings.warn("Estimator fit failed. The score on this train-test"                           " partition for these parameters will be set to %f. "                          "Details: \n%r" % (error_score, e), FitFailedWarning)                          "Details: \n%s" %                          (error_score, format_exception_only(type(e), e)[0]),                          FitFailedWarning)         else:             raise ValueError("error_score must be the string 'raise' or a"                              " numeric value. (Hint: if using 'raise', please"


Question: a one-versus-all approach. Concretely, this is implemented by taking     advantage of the multi-variate response support in Ridge.     """<ins></ins>     def __init__(self, alpha=1.0, fit_intercept=True, normalize=False,                  copy_X=True, max_iter=None, tol=1e-3, class_weight=None,                  solver="auto", random_state=None):

integers in RidgeCV alpha making one of the alphas a float fixes the problem. This should be handled internally. Python3.6 integers in RidgeCV alpha making one of the alphas a float fixes the problem. This should be handled internally. Python3.6


Question: scorer = check_scoring(self, scoring=self.scoring, allow_none=True)         error = scorer is None         if np.any(self.alphas < 0):            raise ValueError("alphas cannot be negative. "                             "Got {} containing some "                             "negative value instead.".format(self.alphas))         for i, alpha in enumerate(self.alphas):             if error:                out, c = _errors(alpha, y, v, Q, QT_y)                out, c = _errors(float(alpha), y, v, Q, QT_y)             else:                out, c = _values(alpha, y, v, Q, QT_y)                out, c = _values(float(alpha), y, v, Q, QT_y)             cv_values[:, i] = out.ravel()             C.append(c)


Question: fit_intercept=True, normalize=False, scoring=None,                  cv=None, gcv_mode=None,                  store_cv_values=False):        self.alphas = alphas        self.alphas = np.asarray(alphas)         self.fit_intercept = fit_intercept         self.normalize = normalize         self.scoring = scoring


Question: a one-versus-all approach. Concretely, this is implemented by taking     advantage of the multi-variate response support in Ridge.     """<ins></ins>     def __init__(self, alphas=(0.1, 1.0, 10.0), fit_intercept=True,                  normalize=False, scoring=None, cv=None, class_weight=None):         super(RidgeClassifierCV, self).__init__(


Question: >>> first_img_data.dtype               #doctest: +SKIP     dtype('uint8')     """    # Try to import imread from scipy. We do this lazily here to prevent    # this module from depending on PIL.    try:        try:            from scipy.misc import imread        except ImportError:            from scipy.misc.pilutil import imread    except ImportError:        raise ImportError("The Python Imaging Library (PIL) "                          "is required to load data from jpeg files")    # import PIL only when needed    from ..externals._pilutil import imread     module_path = join(dirname(__file__), "images")     with open(join(module_path, 'README.txt')) as f:         descr = f.read()diff --git a/sklearn/datasets/lfw.py b/sklearn/datasets/lfw.py-- a/sklearn/datasets/lfw.py++ b/sklearn/datasets/lfw.py

loadsampleimages uses deprecated imread DeprecationWarning: is deprecated! is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.


Question: from .base import get_data_home, _fetch_remote, RemoteFileMetadata from ..utils import Bunch from ..externals.joblib import Memory<del></del> from ..externals.six import b  logger = logging.getLogger(__name__)


Question: def _load_imgs(file_paths, slice_, color, resize):     """Internally used to load images"""    # Try to import imread and imresize from PIL. We do this here to prevent    # the whole sklearn.datasets module from depending on PIL.    try:        try:            from scipy.misc import imread        except ImportError:            from scipy.misc.pilutil import imread        from scipy.misc import imresize    except ImportError:        raise ImportError("The Python Imaging Library (PIL)"                          " is required to load data from jpeg files")    # import PIL only when needed    from ..externals._pilutil import imread, imresize      # compute the portion of the images to load to respect the slice_ parameter     # given by the callerdiff --git a/sklearn/externals/_pilutil.py b/sklearn/externals/_pilutil.pynew file mode 100644-- /dev/null++ b/sklearn/externals/_pilutil.py


Question: <ins>"""A collection of image utilities using the Python Imaging Library (PIL).This is a local version of utility functions from scipy that are wrapping PILfunctionality. These functions are deprecated in scipy 1.0.0 and will beremoved in scipy 1.2.0. Therefore, the functionality used in sklearn is copiedhere. This file is taken from scipy/misc/pilutil.py in scipy1.0.0. Modifications include: making this module importable if pillow is notinstalled, removal of DeprecationWarning, removal of functions scikit-learndoes not need.Copyright (c) 2001, 2002 Enthought, Inc.All rights reserved.Copyright (c) 2003-2017 SciPy Developers.All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions are met:  a. Redistributions of source code must retain the above copyright notice,     this list of conditions and the following disclaimer.  b. Redistributions in binary form must reproduce the above copyright     notice, this list of conditions and the following disclaimer in the     documentation and/or other materials provided with the distribution.  c. Neither the name of Enthought nor the names of the SciPy Developers     may be used to endorse or promote products derived from this software     without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSEARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORSBE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OFSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESSINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER INCONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OFTHE POSSIBILITY OF SUCH DAMAGE."""from __future__ import division, print_function, absolute_importimport numpyimport tempfilefrom numpy import (amin, amax, ravel, asarray, arange, ones, newaxis,                   transpose, iscomplexobj, uint8, issubdtype, array)# Modification of original scipy pilutil.py to make this module importable if# pillow is not installed. If pillow is not installed, functions will raise# ImportError when called.try:    try:        from PIL import Image    except ImportError:        import Image    pillow_installed = True    if not hasattr(Image, 'frombytes'):        Image.frombytes = Image.fromstringexcept ImportError:    pillow_installed = False__all__ = ['bytescale', 'imread', 'imsave', 'fromimage', 'toimage', 'imresize']def bytescale(data, cmin=None, cmax=None, high=255, low=0):    """    Byte scales an array (image).    Byte scaling means converting the input image to uint8 dtype and scaling    the range to ``(low, high)`` (default 0-255).    If the input image already has dtype uint8, no scaling is done.    This function is only available if Python Imaging Library (PIL) is installed.    Parameters    ----------    data : ndarray        PIL image data array.    cmin : scalar, optional        Bias scaling of small values. Default is ``data.min()``.    cmax : scalar, optional        Bias scaling of large values. Default is ``data.max()``.    high : scalar, optional        Scale max value to `high`.  Default is 255.    low : scalar, optional        Scale min value to `low`.  Default is 0.    Returns    -------    img_array : uint8 ndarray        The byte-scaled array.    Examples    --------    >>> from scipy.misc import bytescale    >>> img = np.array([[ 91.06794177,   3.39058326,  84.4221549 ],    ...                 [ 73.88003259,  80.91433048,   4.88878881],    ...                 [ 51.53875334,  34.45808177,  27.5873488 ]])    >>> bytescale(img)    array([[255,   0, 236],           [205, 225,   4],           [140,  90,  70]], dtype=uint8)    >>> bytescale(img, high=200, low=100)    array([[200, 100, 192],           [180, 188, 102],           [155, 135, 128]], dtype=uint8)    >>> bytescale(img, cmin=0, cmax=255)    array([[91,  3, 84],           [74, 81,  5],           [52, 34, 28]], dtype=uint8)    """    if data.dtype == uint8:        return data    if high > 255:        raise ValueError("`high` should be less than or equal to 255.")    if low < 0:        raise ValueError("`low` should be greater than or equal to 0.")    if high < low:        raise ValueError("`high` should be greater than or equal to `low`.")    if cmin is None:        cmin = data.min()    if cmax is None:        cmax = data.max()    cscale = cmax - cmin    if cscale < 0:        raise ValueError("`cmax` should be larger than `cmin`.")    elif cscale == 0:        cscale = 1    scale = float(high - low) / cscale    bytedata = (data - cmin) * scale + low    return (bytedata.clip(low, high) + 0.5).astype(uint8)def imread(name, flatten=False, mode=None):    """    Read an image from a file as an array.    This function is only available if Python Imaging Library (PIL) is installed.    Parameters    ----------    name : str or file object        The file name or file object to be read.    flatten : bool, optional        If True, flattens the color layers into a single gray-scale layer.    mode : str, optional        Mode to convert image to, e.g. ``'RGB'``.  See the Notes for more        details.    Returns    -------    imread : ndarray        The array obtained by reading the image.    Notes    -----    `imread` uses the Python Imaging Library (PIL) to read an image.    The following notes are from the PIL documentation.    `mode` can be one of the following strings:    * 'L' (8-bit pixels, black and white)    * 'P' (8-bit pixels, mapped to any other mode using a color palette)    * 'RGB' (3x8-bit pixels, true color)    * 'RGBA' (4x8-bit pixels, true color with transparency mask)    * 'CMYK' (4x8-bit pixels, color separation)    * 'YCbCr' (3x8-bit pixels, color video format)    * 'I' (32-bit signed integer pixels)    * 'F' (32-bit floating point pixels)    PIL also provides limited support for a few special modes, including    'LA' ('L' with alpha), 'RGBX' (true color with padding) and 'RGBa'    (true color with premultiplied alpha).    When translating a color image to black and white (mode 'L', 'I' or    'F'), the library uses the ITU-R 601-2 luma transform::        L = R * 299/1000 + G * 587/1000 + B * 114/1000    When `flatten` is True, the image is converted using mode 'F'.    When `mode` is not None and `flatten` is True, the image is first    converted according to `mode`, and the result is then flattened using    mode 'F'.    """    if not pillow_installed:        raise ImportError("The Python Imaging Library (PIL) "                          "is required to load data from jpeg files")    im = Image.open(name)    return fromimage(im, flatten=flatten, mode=mode)def imsave(name, arr, format=None):    """    Save an array as an image.    This function is only available if Python Imaging Library (PIL) is installed.    .. warning::        This function uses `bytescale` under the hood to rescale images to use        the full (0, 255) range if ``mode`` is one of ``None, 'L', 'P', 'l'``.        It will also cast data for 2-D images to ``uint32`` for ``mode=None``        (which is the default).    Parameters    ----------    name : str or file object        Output file name or file object.    arr : ndarray, MxN or MxNx3 or MxNx4        Array containing image values.  If the shape is ``MxN``, the array        represents a grey-level image.  Shape ``MxNx3`` stores the red, green        and blue bands along the last dimension.  An alpha layer may be        included, specified as the last colour band of an ``MxNx4`` array.    format : str        Image format. If omitted, the format to use is determined from the        file name extension. If a file object was used instead of a file name,        this parameter should always be used.    Examples    --------    Construct an array of gradient intensity values and save to file:    >>> from scipy.misc import imsave    >>> x = np.zeros((255, 255))    >>> x = np.zeros((255, 255), dtype=np.uint8)    >>> x[:] = np.arange(255)    >>> imsave('gradient.png', x)    Construct an array with three colour bands (R, G, B) and store to file:    >>> rgb = np.zeros((255, 255, 3), dtype=np.uint8)    >>> rgb[..., 0] = np.arange(255)    >>> rgb[..., 1] = 55    >>> rgb[..., 2] = 1 - np.arange(255)    >>> imsave('rgb_gradient.png', rgb)    """    im = toimage(arr, channel_axis=2)    if format is None:        im.save(name)    else:        im.save(name, format)    returndef fromimage(im, flatten=False, mode=None):    """    Return a copy of a PIL image as a numpy array.    This function is only available if Python Imaging Library (PIL) is installed.    Parameters    ----------    im : PIL image        Input image.    flatten : bool        If true, convert the output to grey-scale.    mode : str, optional        Mode to convert image to, e.g. ``'RGB'``.  See the Notes of the        `imread` docstring for more details.    Returns    -------    fromimage : ndarray        The different colour bands/channels are stored in the        third dimension, such that a grey-image is MxN, an        RGB-image MxNx3 and an RGBA-image MxNx4.    """    if not pillow_installed:        raise ImportError("The Python Imaging Library (PIL) "                          "is required to load data from jpeg files")    if not Image.isImageType(im):        raise TypeError("Input is not a PIL image.")    if mode is not None:        if mode != im.mode:            im = im.convert(mode)    elif im.mode == 'P':        # Mode 'P' means there is an indexed "palette".  If we leave the mode        # as 'P', then when we do `a = array(im)` below, `a` will be a 2-D        # containing the indices into the palette, and not a 3-D array        # containing the RGB or RGBA values.        if 'transparency' in im.info:            im = im.convert('RGBA')        else:            im = im.convert('RGB')    if flatten:        im = im.convert('F')    elif im.mode == '1':        # Workaround for crash in PIL. When im is 1-bit, the call array(im)        # can cause a seg. fault, or generate garbage. See        # https://github.com/scipy/scipy/issues/2138 and        # https://github.com/python-pillow/Pillow/issues/350.        #        # This converts im from a 1-bit image to an 8-bit image.        im = im.convert('L')    a = array(im)    return a_errstr = "Mode is unknown or incompatible with input array shape."def toimage(arr, high=255, low=0, cmin=None, cmax=None, pal=None,            mode=None, channel_axis=None):    """Takes a numpy array and returns a PIL image.    This function is only available if Python Imaging Library (PIL) is installed.    The mode of the PIL image depends on the array shape and the `pal` and    `mode` keywords.    For 2-D arrays, if `pal` is a valid (N,3) byte-array giving the RGB values    (from 0 to 255) then ``mode='P'``, otherwise ``mode='L'``, unless mode    is given as 'F' or 'I' in which case a float and/or integer array is made.    .. warning::        This function uses `bytescale` under the hood to rescale images to use        the full (0, 255) range if ``mode`` is one of ``None, 'L', 'P', 'l'``.        It will also cast data for 2-D images to ``uint32`` for ``mode=None``        (which is the default).    Notes    -----    For 3-D arrays, the `channel_axis` argument tells which dimension of the    array holds the channel data.    For 3-D arrays if one of the dimensions is 3, the mode is 'RGB'    by default or 'YCbCr' if selected.    The numpy array must be either 2 dimensional or 3 dimensional.    """    if not pillow_installed:        raise ImportError("The Python Imaging Library (PIL) "                          "is required to load data from jpeg files")    data = asarray(arr)    if iscomplexobj(data):        raise ValueError("Cannot convert a complex-valued array.")    shape = list(data.shape)    valid = len(shape) == 2 or ((len(shape) == 3) and                                ((3 in shape) or (4 in shape)))    if not valid:        raise ValueError("'arr' does not have a suitable array shape for "                         "any mode.")    if len(shape) == 2:        shape = (shape[1], shape[0])  # columns show up first        if mode == 'F':            data32 = data.astype(numpy.float32)            image = Image.frombytes(mode, shape, data32.tostring())            return image        if mode in [None, 'L', 'P']:            bytedata = bytescale(data, high=high, low=low,                                 cmin=cmin, cmax=cmax)            image = Image.frombytes('L', shape, bytedata.tostring())            if pal is not None:                image.putpalette(asarray(pal, dtype=uint8).tostring())                # Becomes a mode='P' automagically.            elif mode == 'P':  # default gray-scale                pal = (arange(0, 256, 1, dtype=uint8)[:, newaxis] *                       ones((3,), dtype=uint8)[newaxis, :])                image.putpalette(asarray(pal, dtype=uint8).tostring())            return image        if mode == '1':  # high input gives threshold for 1            bytedata = (data > high)            image = Image.frombytes('1', shape, bytedata.tostring())            return image        if cmin is None:            cmin = amin(ravel(data))        if cmax is None:            cmax = amax(ravel(data))        data = (data*1.0 - cmin)*(high - low)/(cmax - cmin) + low        if mode == 'I':            data32 = data.astype(numpy.uint32)            image = Image.frombytes(mode, shape, data32.tostring())        else:            raise ValueError(_errstr)        return image    # if here then 3-d array with a 3 or a 4 in the shape length.    # Check for 3 in datacube shape --- 'RGB' or 'YCbCr'    if channel_axis is None:        if (3 in shape):            ca = numpy.flatnonzero(asarray(shape) == 3)[0]        else:            ca = numpy.flatnonzero(asarray(shape) == 4)            if len(ca):                ca = ca[0]            else:                raise ValueError("Could not find channel dimension.")    else:        ca = channel_axis    numch = shape[ca]    if numch not in [3, 4]:        raise ValueError("Channel axis dimension is not valid.")    bytedata = bytescale(data, high=high, low=low, cmin=cmin, cmax=cmax)    if ca == 2:        strdata = bytedata.tostring()        shape = (shape[1], shape[0])    elif ca == 1:        strdata = transpose(bytedata, (0, 2, 1)).tostring()        shape = (shape[2], shape[0])    elif ca == 0:        strdata = transpose(bytedata, (1, 2, 0)).tostring()        shape = (shape[2], shape[1])    if mode is None:        if numch == 3:            mode = 'RGB'        else:            mode = 'RGBA'    if mode not in ['RGB', 'RGBA', 'YCbCr', 'CMYK']:        raise ValueError(_errstr)    if mode in ['RGB', 'YCbCr']:        if numch != 3:            raise ValueError("Invalid array shape for mode.")    if mode in ['RGBA', 'CMYK']:        if numch != 4:            raise ValueError("Invalid array shape for mode.")    # Here we know data and mode is correct    image = Image.frombytes(mode, shape, strdata)    return imagedef imresize(arr, size, interp='bilinear', mode=None):    """    Resize an image.    This function is only available if Python Imaging Library (PIL) is installed.    .. warning::        This function uses `bytescale` under the hood to rescale images to use        the full (0, 255) range if ``mode`` is one of ``None, 'L', 'P', 'l'``.        It will also cast data for 2-D images to ``uint32`` for ``mode=None``        (which is the default).    Parameters    ----------    arr : ndarray        The array of image to be resized.    size : int, float or tuple        * int   - Percentage of current size.        * float - Fraction of current size.        * tuple - Size of the output image (height, width).    interp : str, optional        Interpolation to use for re-sizing ('nearest', 'lanczos', 'bilinear',        'bicubic' or 'cubic').    mode : str, optional        The PIL image mode ('P', 'L', etc.) to convert `arr` before resizing.        If ``mode=None`` (the default), 2-D images will be treated like        ``mode='L'``, i.e. casting to long integer.  For 3-D and 4-D arrays,        `mode` will be set to ``'RGB'`` and ``'RGBA'`` respectively.    Returns    -------    imresize : ndarray        The resized array of image.    See Also    --------    toimage : Implicitly used to convert `arr` according to `mode`.    scipy.ndimage.zoom : More generic implementation that does not use PIL.    """    im = toimage(arr, mode=mode)    ts = type(size)    if issubdtype(ts, numpy.signedinteger):        percent = size / 100.0        size = tuple((array(im.size)*percent).astype(int))    elif issubdtype(type(size), numpy.floating):        size = tuple((array(im.size)*size).astype(int))    else:        size = (size[1], size[0])    func = {'nearest': 0, 'lanczos': 1, 'bilinear': 2, 'bicubic': 3, 'cubic': 3}    imnew = im.resize(size, resample=func[interp])    return fromimage(imnew)</ins>


Question: for check in _yield_clustering_checks(name, estimator):             yield check     yield check_fit2d_predict1d<ins>    yield check_methods_subset_invariance</ins>     if name != 'GaussianProcess':  # FIXME         # XXX GaussianProcess deprecated in 0.20         yield check_fit2d_1sample

Add common test to ensure all(predict(X[mask]) == predict(X)[mask]) I don't think we currently test that estimator predictions/transformations are invariant whether performed in batch or on subsets of a dataset. For some fitted estimator , data and any boolean mask of length , we need: where is any of {, , , , }. Testing that predictions for individual samples match the predictions across the dataset might be sufficient. This should be to common tests at Indeed, reports that this is broken for one-vs-one classification. :'(


Question: getattr(estimator, method), X[0])  <ins>def _apply_func(func, X):    # apply function on the whole set and on mini batches    result_full = func(X)    n_features = X.shape[1]    result_by_batch = [func(batch.reshape(1, n_features))                       for batch in X]    # func can output tuple (e.g. score_samples)    if type(result_full) == tuple:        result_full = result_full[0]        result_by_batch = list(map(lambda x: x[0], result_by_batch))    return np.ravel(result_full), np.ravel(result_by_batch)@ignore_warnings(category=(DeprecationWarning, FutureWarning))def check_methods_subset_invariance(name, estimator_orig):    # check that method gives invariant results if applied    # on mini bathes or the whole set    rnd = np.random.RandomState(0)    X = 3 * rnd.uniform(size=(20, 3))    X = pairwise_estimator_convert_X(X, estimator_orig)    y = X[:, 0].astype(np.int)    estimator = clone(estimator_orig)    y = multioutput_estimator_convert_y_2d(estimator, y)    if hasattr(estimator, "n_components"):        estimator.n_components = 1    if hasattr(estimator, "n_clusters"):        estimator.n_clusters = 1    set_random_state(estimator, 1)    estimator.fit(X, y)    for method in ["predict", "transform", "decision_function",                   "score_samples", "predict_proba"]:        msg = ("{method} of {name} is not invariant when applied "               "to a subset.").format(method=method, name=name)        # TODO remove cases when corrected        if (name, method) in [('SVC', 'decision_function'),                              ('SparsePCA', 'transform'),                              ('MiniBatchSparsePCA', 'transform'),                              ('BernoulliRBM', 'score_samples')]:            raise SkipTest(msg)        if hasattr(estimator, method):            result_full, result_by_batch = _apply_func(                getattr(estimator, method), X)            assert_allclose(result_full, result_by_batch,                            atol=1e-7, err_msg=msg)</ins> @ignore_warnings def check_fit2d_1sample(name, estimator_orig):     # Check that fitting a 2d array with only one sample either works or


Question: -------         self : instance         """        n_samples, n_features = check_array(X).shape        n_samples, n_features = check_array(X, accept_sparse=True).shape         combinations = self._combinations(n_features, self.degree,                                           self.interaction_only,                                           self.include_bias)

Polynomial Features for sparse data I'm not sure if that came up before but PolynomialFeatures doesn't support sparse data, which is not great. Should be easy but I haven't checked ;)


Question: Parameters         ----------        X : array-like, shape [n_samples, n_features]        X : array-like or sparse matrix, shape [n_samples, n_features]             The data to transform, row by row.            Sparse input should preferably be in CSC format.          Returns         -------        XP : np.ndarray shape [n_samples, NP]        XP : np.ndarray or CSC sparse matrix, shape [n_samples, NP]             The matrix of features, where NP is the number of polynomial             features generated from the combination of inputs.         """         check_is_fitted(self, ['n_input_features_', 'n_output_features_'])         X = check_array(X, dtype=FLOAT_DTYPES)        X = check_array(X, dtype=FLOAT_DTYPES, accept_sparse='csc')         n_samples, n_features = X.shape          if n_features != self.n_input_features_:             raise ValueError("X shape does not match training shape")         # allocate output data        XP = np.empty((n_samples, self.n_output_features_), dtype=X.dtype)         combinations = self._combinations(n_features, self.degree,                                           self.interaction_only,                                           self.include_bias)        for i, c in enumerate(combinations):            XP[:, i] = X[:, c].prod(1)        if sparse.isspmatrix(X):            columns = []            for comb in combinations:                if comb:                    out_col = 1                    for col_idx in comb:                        out_col = X[:, col_idx].multiply(out_col)                    columns.append(out_col)                else:                    columns.append(sparse.csc_matrix(np.ones((X.shape[0], 1))))            XP = sparse.hstack(columns, dtype=X.dtype).tocsc()        else:            XP = np.empty((n_samples, self.n_output_features_), dtype=X.dtype)            for i, comb in enumerate(combinations):                XP[:, i] = X[:, comb].prod(1)          return XP


Question: warnings.simplefilter('ignore', NonBLASDotWarning)  def _assert_all_finite(X):def _assert_all_finite(X, allow_nan=False):     """Like assert_all_finite, but only for ndarray."""     if _get_config()['assume_finite']:         return

[RFC] Dissociate NaN and Inf when considering forceallfinite in checkarray Due to changes proposed in , it seems that as currently a main limitation. will force both and to be rejected. If preprocessing methods (whenever this is possible) should let pass , this argument is not enough permissive. Before to implement anything, I think it could be good to have some feedback on the way to go. I see the following solutions: could still accept a bool to preserve the behaviour. Additionally, it could accept an to filter only . proposes to have an additional argument . was worried that it makes to complex. a private function (similarly to removing the numpy version checking) in the which can be shared between the preprocessing methods. They are the solutions that I have in mind for the moment but anything else is welcomed. I would be grateful for any insight.


Question: # First try an O(n) time, O(1) space solution for the common case that     # everything is finite; fall back to O(n) space np.isfinite to prevent     # false positives from overflow in sum method.    if (X.dtype.char in np.typecodes['AllFloat'] and not np.isfinite(X.sum())            and not np.isfinite(X).all()):        raise ValueError("Input contains NaN, infinity"                         " or a value too large for %r." % X.dtype)def assert_all_finite(X):    is_float = X.dtype.kind in 'fc'    if is_float and np.isfinite(X.sum()):        pass    elif is_float:        msg_err = "Input contains {} or a value too large for {!r}."        if (allow_nan and np.isinf(X).any() or                not allow_nan and not np.isfinite(X).all()):            type_err = 'infinity' if allow_nan else 'NaN, infinity'            raise ValueError(msg_err.format(type_err, X.dtype))def assert_all_finite(X, allow_nan=False):     """Throw a ValueError if X contains NaN or infinity.      Parameters     ----------     X : array or sparse matrix    allow_nan : bool     """    _assert_all_finite(X.data if sp.issparse(X) else X)    _assert_all_finite(X.data if sp.issparse(X) else X, allow_nan)   def as_float_array(X, copy=True, force_all_finite=True):


Question: If True, a copy of X will be created. If False, a copy may still be         returned if X's dtype is not a floating point type.     force_all_finite : boolean (default=True)        Whether to raise an error on np.inf and np.nan in X.    force_all_finite : boolean or 'allow-nan', (default=True)        Whether to raise an error on np.inf and np.nan in X. The possibilities        are:        - True: Force all values of X to be finite.        - False: accept both np.inf and np.nan in X.        - 'allow-nan':  accept  only  np.nan  values in  X.  Values  cannot  be          infinite.        .. versionadded:: 0.20           ``force_all_finite`` accepts the string ``'allow-nan'``.      Returns     -------


Question: Whether a forced copy will be triggered. If copy=False, a copy might         be triggered by a conversion.     force_all_finite : boolean        Whether to raise an error on np.inf and np.nan in X.    force_all_finite : boolean or 'allow-nan', (default=True)        Whether to raise an error on np.inf and np.nan in X. The possibilities        are:        - True: Force all values of X to be finite.        - False: accept both np.inf and np.nan in X.        - 'allow-nan':  accept  only  np.nan  values in  X.  Values  cannot  be          infinite.        .. versionadded:: 0.20           ``force_all_finite`` accepts the string ``'allow-nan'``.      Returns     -------


Question: warnings.warn("Can't check %s sparse matrix for nan or inf."                           % spmatrix.format)         else:            _assert_all_finite(spmatrix.data)            _assert_all_finite(spmatrix.data,                               allow_nan=force_all_finite == 'allow-nan')     return spmatrix


Question: Whether a forced copy will be triggered. If copy=False, a copy might         be triggered by a conversion.     force_all_finite : boolean (default=True)        Whether to raise an error on np.inf and np.nan in X.    force_all_finite : boolean or 'allow-nan', (default=True)        Whether to raise an error on np.inf and np.nan in X. The possibilities        are:        - True: Force all values of X to be finite.        - False: accept both np.inf and np.nan in X.        - 'allow-nan':  accept  only  np.nan  values in  X.  Values  cannot  be          infinite.        .. versionadded:: 0.20           ``force_all_finite`` accepts the string ``'allow-nan'``.      ensure_2d : boolean (default=True)         Whether to raise a value error if X is not 2d.


Question: # list of accepted types.             dtype = dtype[0] <ins>    if force_all_finite not in (True, False, 'allow-nan'):        raise ValueError('force_all_finite should be a bool or "allow-nan"'                         '. Got {!r} instead'.format(force_all_finite))</ins>     if estimator is not None:         if isinstance(estimator, six.string_types):             estimator_name = estimator


Question: raise ValueError("Found array with dim %d. %s expected <= 2."                              % (array.ndim, estimator_name))         if force_all_finite:            _assert_all_finite(array)            _assert_all_finite(array,                               allow_nan=force_all_finite == 'allow-nan')      shape_repr = _shape_repr(array.shape)     if ensure_min_samples > 0:


Question: Whether a forced copy will be triggered. If copy=False, a copy might         be triggered by a conversion.     force_all_finite : boolean (default=True)    force_all_finite : boolean or 'allow-nan', (default=True)         Whether to raise an error on np.inf and np.nan in X. This parameter         does not influence whether y can have np.inf or np.nan values.        The possibilities are:        - True: Force all values of X to be finite.        - False: accept both np.inf and np.nan in X.        - 'allow-nan':  accept  only  np.nan  values in  X.  Values  cannot  be          infinite.        .. versionadded:: 0.20           ``force_all_finite`` accepts the string ``'allow-nan'``.      ensure_2d : boolean (default=True)         Whether to make X at least 2d.


Question: from sklearn.datasets import load_boston from sklearn.ensemble import RandomForestRegressor from sklearn.pipeline import Pipelinefrom sklearn.preprocessing import Imputerfrom sklearn.impute import SimpleImputer from sklearn.model_selection import cross_val_score  rng = np.random.RandomState(0)

Move imputation out of preprocessing While we're considering additional imputers, I've wondered whether preprocessing is the right place for it. Yes, it is a preprocessing step before other learning, but it often makes use of other supervised and unsupervised learners and hence is a learning task of its own. And preprocessing is getting a bit cramped. We could also do as with other models and have imputers appear in modules on the basis of how they work rather than function: could appear in neighbors for instance. could appear where..? And the basic in dummy? probably not. In practice I think it is more useful for users to , akin to our clusterers and decomposition, and unlike our predictors and outlier detectors that are grouped by algorithm.


Question: X_missing = X_full.copy() X_missing[np.where(missing_samples)[0], missing_features] = 0 y_missing = y_full.copy()estimator = Pipeline([("imputer", Imputer(missing_values=0,                                          strategy="mean",                                          axis=0)),estimator = Pipeline([("imputer", SimpleImputer(missing_values=0,                                                strategy="mean",                                                axis=0)),                       ("forest", RandomForestRegressor(random_state=0,                                                        n_estimators=100))]) score = cross_val_score(estimator, X_missing, y_missing).mean()diff --git a/sklearn/__init__.py b/sklearn/__init__.py-- a/sklearn/__init__.py++ b/sklearn/__init__.py


Question: 'mixture', 'model_selection', 'multiclass', 'multioutput',                'naive_bayes', 'neighbors', 'neural_network', 'pipeline',                'preprocessing', 'random_projection', 'semi_supervised',               'svm', 'tree', 'discriminant_analysis',               'svm', 'tree', 'discriminant_analysis', 'impute',                # Non-modules:                'clone', 'get_config', 'set_config', 'config_context'] diff --git a/sklearn/impute.py b/sklearn/impute.pynew file mode 100644-- /dev/null++ b/sklearn/impute.py


Question: """Transformers for missing value imputation"""# Authors: Nicolas Tresegnie <nicolas.tresegnie@gmail.com># License: BSD 3 clauseimport warningsimport numpy as npimport numpy.ma as mafrom scipy import sparsefrom scipy import statsfrom .base import BaseEstimator, TransformerMixinfrom .utils import check_arrayfrom .utils.sparsefuncs import _get_medianfrom .utils.validation import check_is_fittedfrom .utils.validation import FLOAT_DTYPESfrom .externals import sixzip = six.moves.zipmap = six.moves.map__all__ = [    'SimpleImputer',]def _get_mask(X, value_to_mask):    """Compute the boolean mask X == missing_values."""    if value_to_mask == "NaN" or np.isnan(value_to_mask):        return np.isnan(X)    else:        return X == value_to_maskdef _most_frequent(array, extra_value, n_repeat):    """Compute the most frequent value in a 1d array extended with       [extra_value] * n_repeat, where extra_value is assumed to be not part       of the array."""    # Compute the most frequent value in array only    if array.size > 0:        mode = stats.mode(array)        most_frequent_value = mode[0][0]        most_frequent_count = mode[1][0]    else:        most_frequent_value = 0        most_frequent_count = 0    # Compare to array + [extra_value] * n_repeat    if most_frequent_count == 0 and n_repeat == 0:        return np.nan    elif most_frequent_count < n_repeat:        return extra_value    elif most_frequent_count > n_repeat:        return most_frequent_value    elif most_frequent_count == n_repeat:        # Ties the breaks. Copy the behaviour of scipy.stats.mode        if most_frequent_value < extra_value:            return most_frequent_value        else:            return extra_valueclass SimpleImputer(BaseEstimator, TransformerMixin):    """Imputation transformer for completing missing values.    Read more in the :ref:`User Guide <impute>`.    Parameters    ----------    missing_values : integer or "NaN", optional (default="NaN")        The placeholder for the missing values. All occurrences of        `missing_values` will be imputed. For missing values encoded as np.nan,        use the string value "NaN".    strategy : string, optional (default="mean")        The imputation strategy.        - If "mean", then replace missing values using the mean along          the axis.        - If "median", then replace missing values using the median along          the axis.        - If "most_frequent", then replace missing using the most frequent          value along the axis.    axis : integer, optional (default=0)        The axis along which to impute.        - If `axis=0`, then impute along columns.        - If `axis=1`, then impute along rows.    verbose : integer, optional (default=0)        Controls the verbosity of the imputer.    copy : boolean, optional (default=True)        If True, a copy of X will be created. If False, imputation will        be done in-place whenever possible. Note that, in the following cases,        a new copy will always be made, even if `copy=False`:        - If X is not an array of floating values;        - If X is sparse and `missing_values=0`;        - If `axis=0` and X is encoded as a CSR matrix;        - If `axis=1` and X is encoded as a CSC matrix.    Attributes    ----------    statistics_ : array of shape (n_features,)        The imputation fill value for each feature if axis == 0.    Notes    -----    - When ``axis=0``, columns which only contained missing values at `fit`      are discarded upon `transform`.    - When ``axis=1``, an exception is raised if there are rows for which it is      not possible to fill in the missing values (e.g., because they only      contain missing values).    """    def __init__(self, missing_values="NaN", strategy="mean",                 axis=0, verbose=0, copy=True):        self.missing_values = missing_values        self.strategy = strategy        self.axis = axis        self.verbose = verbose        self.copy = copy    def fit(self, X, y=None):        """Fit the imputer on X.        Parameters        ----------        X : {array-like, sparse matrix}, shape (n_samples, n_features)            Input data, where ``n_samples`` is the number of samples and            ``n_features`` is the number of features.        Returns        -------        self : SimpleImputer        """        # Check parameters        allowed_strategies = ["mean", "median", "most_frequent"]        if self.strategy not in allowed_strategies:            raise ValueError("Can only use these strategies: {0} "                             " got strategy={1}".format(allowed_strategies,                                                        self.strategy))        if self.axis not in [0, 1]:            raise ValueError("Can only impute missing values on axis 0 and 1, "                             " got axis={0}".format(self.axis))        # Since two different arrays can be provided in fit(X) and        # transform(X), the imputation data will be computed in transform()        # when the imputation is done per sample (i.e., when axis=1).        if self.axis == 0:            X = check_array(X, accept_sparse='csc', dtype=np.float64,                            force_all_finite=False)            if sparse.issparse(X):                self.statistics_ = self._sparse_fit(X,                                                    self.strategy,                                                    self.missing_values,                                                    self.axis)            else:                self.statistics_ = self._dense_fit(X,                                                   self.strategy,                                                   self.missing_values,                                                   self.axis)        return self    def _sparse_fit(self, X, strategy, missing_values, axis):        """Fit the transformer on sparse data."""        # Imputation is done "by column", so if we want to do it        # by row we only need to convert the matrix to csr format.        if axis == 1:            X = X.tocsr()        else:            X = X.tocsc()        # Count the zeros        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)        # Mean        if strategy == "mean":            if missing_values != 0:                n_non_missing = n_zeros_axis                # Mask the missing elements                mask_missing_values = _get_mask(X.data, missing_values)                mask_valids = np.logical_not(mask_missing_values)                # Sum only the valid elements                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)                # Count the elements != 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)            # Ignore the error, columns with a np.nan statistics_            # are not an error at this point. These columns will            # be removed in transform            with np.errstate(all="ignore"):                return np.ravel(sums) / np.ravel(n_non_missing)        # Median + Most frequent        else:            # Remove the missing values, for each column            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])            # astype necessary for bug in numpy.hsplit before v1.9            columns = [col[mask.astype(bool, copy=False)]                       for col, mask in zip(columns_all, mask_valids)]            # Median            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            # Most frequent            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    def _dense_fit(self, X, strategy, missing_values, axis):        """Fit the transformer on dense data."""        X = check_array(X, force_all_finite=False)        mask = _get_mask(X, missing_values)        masked_X = ma.masked_array(X, mask=mask)        # Mean        if strategy == "mean":            mean_masked = np.ma.mean(masked_X, axis=axis)            # Avoid the warning "Warning: converting a masked element to nan."            mean = np.ma.getdata(mean_masked)            mean[np.ma.getmask(mean_masked)] = np.nan            return mean        # Median        elif strategy == "median":            if tuple(int(v) for v in np.__version__.split('.')[:2]) < (1, 5):                # In old versions of numpy, calling a median on an array                # containing nans returns nan. This is different is                # recent versions of numpy, which we want to mimic                masked_X.mask = np.logical_or(masked_X.mask,                                              np.isnan(X))            median_masked = np.ma.median(masked_X, axis=axis)            # Avoid the warning "Warning: converting a masked element to nan."            median = np.ma.getdata(median_masked)            median[np.ma.getmaskarray(median_masked)] = np.nan            return median        # Most frequent        elif strategy == "most_frequent":            # scipy.stats.mstats.mode cannot be used because it will no work            # properly if the first element is masked and if its frequency            # is equal to the frequency of the most frequent valid element            # See https://github.com/scipy/scipy/issues/2636            # To be able access the elements by columns            if axis == 0:                X = X.transpose()                mask = mask.transpose()            most_frequent = np.empty(X.shape[0])            for i, (row, row_mask) in enumerate(zip(X[:], mask[:])):                row_mask = np.logical_not(row_mask).astype(np.bool)                row = row[row_mask]                most_frequent[i] = _most_frequent(row, np.nan, 0)            return most_frequent    def transform(self, X):        """Impute all missing values in X.        Parameters        ----------        X : {array-like, sparse matrix}, shape = [n_samples, n_features]            The input data to complete.        """        if self.axis == 0:            check_is_fitted(self, 'statistics_')            X = check_array(X, accept_sparse='csc', dtype=FLOAT_DTYPES,                            force_all_finite=False, copy=self.copy)            statistics = self.statistics_            if X.shape[1] != statistics.shape[0]:                raise ValueError("X has %d features per sample, expected %d"                                 % (X.shape[1], self.statistics_.shape[0]))        # Since two different arrays can be provided in fit(X) and        # transform(X), the imputation data need to be recomputed        # when the imputation is done per sample        else:            X = check_array(X, accept_sparse='csr', dtype=FLOAT_DTYPES,                            force_all_finite=False, copy=self.copy)            if sparse.issparse(X):                statistics = self._sparse_fit(X,                                              self.strategy,                                              self.missing_values,                                              self.axis)            else:                statistics = self._dense_fit(X,                                             self.strategy,                                             self.missing_values,                                             self.axis)        # Delete the invalid rows/columns        invalid_mask = np.isnan(statistics)        valid_mask = np.logical_not(invalid_mask)        valid_statistics = statistics[valid_mask]        valid_statistics_indexes = np.where(valid_mask)[0]        missing = np.arange(X.shape[not self.axis])[invalid_mask]        if self.axis == 0 and invalid_mask.any():            if self.verbose:                warnings.warn("Deleting features without "                              "observed values: %s" % missing)            X = X[:, valid_statistics_indexes]        elif self.axis == 1 and invalid_mask.any():            raise ValueError("Some rows only contain "                             "missing values: %s" % missing)        # Do actual imputation        if sparse.issparse(X) and self.missing_values != 0:            mask = _get_mask(X.data, self.missing_values)            indexes = np.repeat(np.arange(len(X.indptr) - 1, dtype=np.int),                                np.diff(X.indptr))[mask]            X.data[mask] = valid_statistics[indexes].astype(X.dtype,                                                            copy=False)        else:            if sparse.issparse(X):                X = X.toarray()            mask = _get_mask(X, self.missing_values)            n_missing = np.sum(mask, axis=self.axis)            values = np.repeat(valid_statistics, n_missing)            if self.axis == 0:                coordinates = np.where(mask.transpose())[::-1]            else:                coordinates = mask            X[coordinates] = values        return Xdiff --git a/sklearn/preprocessing/imputation.py b/sklearn/preprocessing/imputation.py-- a/sklearn/preprocessing/imputation.py++ b/sklearn/preprocessing/imputation.py


Question: from ..base import BaseEstimator, TransformerMixin from ..utils import check_array<ins>from ..utils import deprecated</ins> from ..utils.sparsefuncs import _get_median from ..utils.validation import check_is_fitted from ..utils.validation import FLOAT_DTYPES


Question: return extra_value  @deprecated("Imputer was deprecated in version 0.20 and will be "            "removed in 0.22. Import impute.SimpleImputer from "            "sklearn instead.") class Imputer(BaseEstimator, TransformerMixin):     """Imputation transformer for completing missing values. diff --git a/sklearn/utils/estimator_checks.py b/sklearn/utils/estimator_checks.py-- a/sklearn/utils/estimator_checks.py++ b/sklearn/utils/estimator_checks.py


Question: # cross-decomposition's "transform" returns X and Y         yield check_pipeline_consistency     if name not in ['Imputer']:    if name not in ['SimpleImputer', 'Imputer']:         # Test that all estimators check their input for NaN's and infs         yield check_estimators_nan_inf


Question: # To ensure that array flags are maintained             array = np.array(array, dtype=dtype, order=order, copy=copy) <ins>        # in the future np.flexible dtypes will be handled like object dtypes        if dtype_numeric and np.issubdtype(array.dtype, np.flexible):            warnings.warn(                "Beginning in version 0.22, arrays of strings will be "                "interpreted as decimal numbers if parameter 'dtype' is "                "'numeric'. It is recommended that you convert the array to "                "type np.float64 before passing it to check_array.",                FutureWarning)</ins>         # make sure we actually converted to numeric:         if dtype_numeric and array.dtype.kind == "O":             array = array.astype(np.float64)

checkarray(X, dtype='numeric') should fail if X has strings Currently, dtype='numeric' is defined as "dtype is preserved unless is object". This seems overly lenient and strange behaviour, as in where shows that works without error and produces an array of strings! This behaviour is not tested and it's hard to believe that it is useful and intended. Perhaps we need a deprecation cycle, but I think dtype='numeric' should raise an error, or attempt to coerce, if the data does not actually have a numeric, real-valued dtype. checkarray(X, dtype='numeric') should fail if X has strings Currently, dtype='numeric' is defined as "dtype is preserved unless is object". This seems overly lenient and strange behaviour, as in where shows that works without error and produces an array of strings! This behaviour is not tested and it's hard to believe that it is useful and intended. Perhaps we need a deprecation cycle, but I think dtype='numeric' should raise an error, or attempt to coerce, if the data does not actually have a numeric, real-valued dtype.


Question: """         check_is_fitted(self, 'classes_')         y = column_or_1d(y, warn=True)<ins>        # transform of empty array is empty array        if _num_samples(y) == 0:            return np.array([])</ins>          classes = np.unique(y)         if len(np.intersect1d(classes, self.classes_)) < len(classes):

LabelEncoder transform fails for empty lists (for certain inputs) Python 3.6.3, scikit_learn 0.19.1 Depending on which datatypes were used to fit the LabelEncoder, transforming empty lists works or not. Expected behavior would be that empty arrays are returned in both cases.


Question: y : numpy array of shape [n_samples]         """         check_is_fitted(self, 'classes_')<ins>        y = column_or_1d(y, warn=True)        # inverse transform of empty array is empty array        if _num_samples(y) == 0:            return np.array([])</ins>          diff = np.setdiff1d(y, np.arange(len(self.classes_)))         if len(diff):


Question: X_missing[np.where(missing_samples)[0], missing_features] = 0 y_missing = y_full.copy() estimator = Pipeline([("imputer", Imputer(missing_values=0,                                          strategy="mean",                                          axis=0)),                                          strategy="mean")),                       ("forest", RandomForestRegressor(random_state=0,                                                        n_estimators=100))]) score = cross_val_score(estimator, X_missing, y_missing).mean()diff --git a/sklearn/preprocessing/imputation.py b/sklearn/preprocessing/imputation.py-- a/sklearn/preprocessing/imputation.py++ b/sklearn/preprocessing/imputation.py

Deprecate Imputer with axis=1 After having tried to deal with a few issues related to extending behaviour, I believe we should be removing the parameter from . It seems a strange feature to support in a machine learning context, except perhaps where the features represent something like a time series. It is not stateful and can be performed with a FunctionTransformer. (We could even provide a function, if we felt it necessary, which would roughly be defined as .) It complicates the implementation, which already has a bunch of weird edge-cases (to handle sparse data with missing indicated by 0 which is an inefficient use of a sparse data structure; and to handle non-NaN missingness indicators), unnecessarily. It is often nonsensical to extend further features to the case. Do others agree? [MRG+1] Deprecate ` argument Deprecated the argument on the class.


Question: - If "most_frequent", then replace missing using the most frequent           value along the axis.     axis : integer, optional (default=0)    axis : integer, optional (default=None)         The axis along which to impute.          - If `axis=0`, then impute along columns.         - If `axis=1`, then impute along rows.         .. deprecated:: 0.20           Parameter ``axis`` has been deprecated in 0.20 and will be removed           in 0.22. Future (and default) behavior is equivalent to ``axis=0``           (impute along columns). Row-wise imputation can be performed with           FunctionTransformer (e.g.,           ``FunctionTransformer(lambda X: Imputer().fit_transform(X.T).T)``).     verbose : integer, optional (default=0)         Controls the verbosity of the imputer.


Question: contain missing values).     """     def __init__(self, missing_values="NaN", strategy="mean",                 axis=0, verbose=0, copy=True):                 axis=None, verbose=0, copy=True):         self.missing_values = missing_values         self.strategy = strategy         self.axis = axis


Question: " got strategy={1}".format(allowed_strategies,                                                         self.strategy))         if self.axis not in [0, 1]:        if self.axis is None:            self._axis = 0        else:            warnings.warn("Parameter 'axis' has been deprecated in 0.20 and "                          "will be removed in 0.22. Future (and default) "                          "behavior is equivalent to 'axis=0' (impute along "                          "columns). Row-wise imputation can be performed "                          "with FunctionTransformer.", DeprecationWarning)            self._axis = self.axis        if self._axis not in [0, 1]:             raise ValueError("Can only impute missing values on axis 0 and 1, "                             " got axis={0}".format(self.axis))                             " got axis={0}".format(self._axis))          # Since two different arrays can be provided in fit(X) and         # transform(X), the imputation data will be computed in transform()         # when the imputation is done per sample (i.e., when axis=1).        if self.axis == 0:        if self._axis == 0:             X = check_array(X, accept_sparse='csc', dtype=np.float64,                             force_all_finite=False)


Question: self.statistics_ = self._sparse_fit(X,                                                     self.strategy,                                                     self.missing_values,                                                    self.axis)                                                    self._axis)             else:                 self.statistics_ = self._dense_fit(X,                                                    self.strategy,                                                    self.missing_values,                                                   self.axis)                                                   self._axis)          return self


Question: X : {array-like, sparse matrix}, shape = [n_samples, n_features]             The input data to complete.         """        if self.axis == 0:        if self._axis == 0:             check_is_fitted(self, 'statistics_')             X = check_array(X, accept_sparse='csc', dtype=FLOAT_DTYPES,                             force_all_finite=False, copy=self.copy)


Question: statistics = self._sparse_fit(X,                                               self.strategy,                                               self.missing_values,                                              self.axis)                                              self._axis)              else:                 statistics = self._dense_fit(X,                                              self.strategy,                                              self.missing_values,                                             self.axis)                                             self._axis)          # Delete the invalid rows/columns         invalid_mask = np.isnan(statistics)         valid_mask = np.logical_not(invalid_mask)         valid_statistics = statistics[valid_mask]         valid_statistics_indexes = np.where(valid_mask)[0]        missing = np.arange(X.shape[not self.axis])[invalid_mask]        missing = np.arange(X.shape[not self._axis])[invalid_mask]         if self.axis == 0 and invalid_mask.any():        if self._axis == 0 and invalid_mask.any():             if self.verbose:                 warnings.warn("Deleting features without "                               "observed values: %s" % missing)             X = X[:, valid_statistics_indexes]        elif self.axis == 1 and invalid_mask.any():        elif self._axis == 1 and invalid_mask.any():             raise ValueError("Some rows only contain "                              "missing values: %s" % missing)


Question: X = X.toarray()              mask = _get_mask(X, self.missing_values)            n_missing = np.sum(mask, axis=self.axis)            n_missing = np.sum(mask, axis=self._axis)             values = np.repeat(valid_statistics, n_missing)             if self.axis == 0:            if self._axis == 0:                 coordinates = np.where(mask.transpose())[::-1]             else:                 coordinates = mask


Question: import numpy as np import matplotlib.pyplot as plt from sklearn import svmfrom sklearn.datasets import make_blobs # we create clusters with 1000 and 100 pointsrng = np.random.RandomState(0)# we create two clusters of random points n_samples_1 = 1000 n_samples_2 = 100X = np.r_[1.5 * rng.randn(n_samples_1, 2),          0.5 * rng.randn(n_samples_2, 2) + [2, 2]]y = [0] * (n_samples_1) + [1] * (n_samples_2)centers = [[0.0, 0.0], [2.0, 2.0]]clusters_std = [1.5, 0.5]X, y = make_blobs(n_samples=[n_samples_1, n_samples_2],                  centers=centers,                  cluster_std=clusters_std,                  random_state=0, shuffle=False)  # fit the model and get the separating hyperplane clf = svm.SVC(kernel='linear', C=1.0)diff --git a/sklearn/datasets/samples_generator.py b/sklearn/datasets/samples_generator.py-- a/sklearn/datasets/samples_generator.py++ b/sklearn/datasets/samples_generator.py

Allow nsamples to be a tuple in makeblobs I'd like makeblobs to accept lists or tuples for nsamples to generate imbalanced classes. Could be used here for example: http://scikit-


Question: import numpy as np from scipy import linalg import scipy.sparse as sp<ins>from collections import Iterable</ins>  from ..preprocessing import MultiLabelBinarizer from ..utils import check_array, check_random_state


Question: return X, y  def make_blobs(n_samples=100, n_features=2, centers=3, cluster_std=1.0,def make_blobs(n_samples=100, n_features=2, centers=None, cluster_std=1.0,                center_box=(-10.0, 10.0), shuffle=True, random_state=None):     """Generate isotropic Gaussian blobs for clustering.


Question: Parameters     ----------    n_samples : int, optional (default=100)        The total number of points equally divided among clusters.    n_samples : int or array-like, optional (default=100)        If int, it is the the total number of points equally divided among        clusters.        If array-like, each element of the sequence indicates        the number of samples per cluster.      n_features : int, optional (default=2)         The number of features for each sample.      centers : int or array of shape [n_centers, n_features], optional        (default=3)        (default=None)         The number of centers to generate, or the fixed center locations.        If n_samples is an int and centers is None, 3 centers are generated.        If n_samples is array-like, centers must be        either None or an array of length equal to the length of n_samples.      cluster_std : float or sequence of floats, optional (default=1.0)         The standard deviation of the clusters.


Question: (10, 2)     >>> y     array([0, 0, 1, 0, 2, 2, 2, 1, 1, 0])<ins>    >>> X, y = make_blobs(n_samples=[3, 3, 4], centers=None, n_features=2,    ...                   random_state=0)    >>> print(X.shape)    (10, 2)    >>> y    array([0, 1, 2, 0, 2, 2, 2, 1, 1, 0])</ins>      See also     --------


Question: """     generator = check_random_state(random_state)     if isinstance(centers, numbers.Integral):        centers = generator.uniform(center_box[0], center_box[1],                                    size=(centers, n_features))    if isinstance(n_samples, numbers.Integral):        # Set n_centers by looking at centers arg        if centers is None:            centers = 3        if isinstance(centers, numbers.Integral):            n_centers = centers            centers = generator.uniform(center_box[0], center_box[1],                                        size=(n_centers, n_features))        else:            centers = check_array(centers)            n_features = centers.shape[1]            n_centers = centers.shape[0]     else:        centers = check_array(centers)        n_features = centers.shape[1]        # Set n_centers by looking at [n_samples] arg        n_centers = len(n_samples)        if centers is None:            centers = generator.uniform(center_box[0], center_box[1],                                        size=(n_centers, n_features))        try:            assert len(centers) == n_centers        except TypeError:            raise ValueError("Parameter `centers` must be array-like. "                             "Got {!r} instead".format(centers))        except AssertionError:            raise ValueError("Length of `n_samples` not consistent"                             " with number of centers. Got n_samples = {} "                             "and centers = {}".format(n_samples, centers))        else:            centers = check_array(centers)            n_features = centers.shape[1]    # stds: if cluster_std is given as list, it must be consistent    # with the n_centers    if (hasattr(cluster_std, "__len__") and len(cluster_std) != n_centers):        raise ValueError("Length of `clusters_std` not consistent with "                         "number of centers. Got centers = {} "                         "and cluster_std = {}".format(centers, cluster_std))      if isinstance(cluster_std, numbers.Real):         cluster_std = np.ones(len(centers)) * cluster_std


Question: X = []     y = []     n_centers = centers.shape[0]    n_samples_per_center = [int(n_samples // n_centers)] * n_centers    if isinstance(n_samples, Iterable):        n_samples_per_center = n_samples    else:        n_samples_per_center = [int(n_samples // n_centers)] * n_centers     for i in range(n_samples % n_centers):        n_samples_per_center[i] += 1        for i in range(n_samples % n_centers):            n_samples_per_center[i] += 1      for i, (n, std) in enumerate(zip(n_samples_per_center, cluster_std)):        X.append(centers[i] + generator.normal(scale=std,                                               size=(n, n_features)))        X.append(generator.normal(loc=centers[i], scale=std,                                  size=(n, n_features)))         y += [i] * n      X = np.concatenate(X)     y = np.array(y)      if shuffle:        indices = np.arange(n_samples)        total_n_samples = np.sum(n_samples)        indices = np.arange(total_n_samples)         generator.shuffle(indices)         X = X[indices]         y = y[indices]


Question: raise ValueError('precompute should be one of True, False or'                              ' array-like. Got %r' % self.precompute)         # Remember if X is copied        X_copied = False         # We expect X and y to be float64 or float32 Fortran ordered arrays         # when bypassing checks         if check_input:            X_copied = self.copy_X and self.fit_intercept             X, y = check_X_y(X, y, accept_sparse='csc',                              order='F', dtype=[np.float64, np.float32],                             copy=self.copy_X and self.fit_intercept,                             multi_output=True, y_numeric=True)                             copy=X_copied, multi_output=True, y_numeric=True)             y = check_array(y, order='F', copy=False, dtype=X.dtype.type,                             ensure_2d=False)         # Ensure copying happens only once, don't do it again if done above        should_copy = self.copy_X and not X_copied         X, y, X_offset, y_offset, X_scale, precompute, Xy = \             _pre_fit(X, y, None, self.precompute, self.normalize,                     self.fit_intercept, copy=False)                     self.fit_intercept, copy=should_copy)         if y.ndim == 1:             y = y[:, np.newaxis]         if Xy is not None and Xy.ndim == 1:

ElasticNet overwrites X even with copyX=True The function of an , called with , overwrites X, even when : ElasticNet overwrites X even with copyX=True The function of an , called with , overwrites X, even when : [MRG] FIX ElasticNet overwrites X even with copy_X=True Made changes as suggested by please review and suggest changes


Question: if n_targets == 1:             self.n_iter_ = self.n_iter_[0]            self.coef_ = coef_[0]            self.dual_gap_ = dual_gaps_[0]        else:            self.coef_ = coef_            self.dual_gap_ = dual_gaps_         self.coef_, self.dual_gap_ = map(np.squeeze, [coef_, dual_gaps_])         self._set_intercept(X_offset, y_offset, X_scale)          # workaround since _set_intercept will cast self.coef_ into X.dtype

Shape of wrong for linearmodel.Lasso when using <!-- If your issue is a usage question, submit it here instead: StackOverflow with the scikit-learn tag: Mailing List: For more information, see User Questions: http://scikit- --<!-- Instructions For Filing a Bug: --Shape of wrong for linearmodel.Lasso when using Example: the second snippet should not raise, but it does. The first snippet is ok. I pasted it as a reference Linux-3.2.0-4-amd64-x8664-with-debian-7.11 ('Python', '2.7.3 (default, Mar 13 2014, 11:03:55) \n[GCC 4.7.2]') ('NumPy', '1.13.3') ('SciPy', '0.19.1') ('Scikit-Learn', '0.18.2') <!-- Thanks for contributing! --[MRG] Shape of wrong for linearmodel.Lasso when using <!-- Thanks for contributing a pull request! Please ensure you have taken a look at the contribution guidelines: --


Question: if len(self.vocabulary_) == 0:             raise ValueError("Vocabulary is empty") <ins>    def _validate_params(self):        """Check validity of ngram_range parameter"""        min_n, max_m = self.ngram_range        if min_n > max_m:            raise ValueError(                "Invalid value for ngram_range=%s "                "lower boundary larger than the upper boundary."                % str(self.ngram_range))</ins>  class HashingVectorizer(BaseEstimator, VectorizerMixin, TransformerMixin):     """Convert a collection of text documents to a matrix of token occurrences

no error on CountVectorizer(ngramrange=(2, 1)) I think if ngramrange[0] is greater than ngram_range[1] we should throw an error. Not sure what the current behavior is.


Question: "Iterable over raw text documents expected, "                 "string object received.") <ins>        self._validate_params()</ins>         self._get_hasher().fit(X, y=y)         return self


Question: "Iterable over raw text documents expected, "                 "string object received.") <ins>        self._validate_params()</ins>         analyzer = self.build_analyzer()         X = self._get_hasher().transform(analyzer(doc) for doc in X)         if self.binary:


Question: "Iterable over raw text documents expected, "                 "string object received.") <ins>        self._validate_params()</ins>         self._validate_vocabulary()         max_df = self.max_df         min_df = self.min_df


Question: import numpy as np from scipy.special import gammainc from ..base import BaseEstimatorfrom ..utils import check_array, check_random_statefrom ..utils import check_array, check_random_state, check_consistent_length from ..utils.extmath import row_norms from .ball_tree import BallTree, DTYPE from .kd_tree import KDTree

weighted KDE Not sure this is the correct place, but I would very much appreciate the ability to pass a weight for each sample in kde density estimation. There exits a adapted version of : weighted KDE Not sure this is the correct place, but I would very much appreciate the ability to pass a weight for each sample in kde density estimation. There exits a adapted version of :


Question: else:             raise ValueError("invalid algorithm: '{0}'".format(algorithm))     def fit(self, X, y=None):    def fit(self, X, y=None, sample_weight=None):         """Fit the Kernel Density model on the data.          Parameters


Question: X : array_like, shape (n_samples, n_features)             List of n_features-dimensional data points.  Each row             corresponds to a single data point.        sample_weight: array_like, shape (n_samples,), optional            List of sample weights attached to the data X.         """         algorithm = self._choose_algorithm(self.algorithm, self.metric)         X = check_array(X, order='C', dtype=DTYPE)         if sample_weight is not None:            sample_weight = check_array(sample_weight, order='C', dtype=DTYPE,                                        ensure_2d=False)            if sample_weight.ndim != 1:                raise ValueError("the shape of sample_weight must be ({0},),"                                 " but was {1}".format(X.shape[0],                                                       sample_weight.shape))            check_consistent_length(X, sample_weight)            if sample_weight.min() <= 0:                raise ValueError("sample_weight must have positive values")         kwargs = self.metric_params         if kwargs is None:             kwargs = {}         self.tree_ = TREE_DICT[algorithm](X, metric=self.metric,                                           leaf_size=self.leaf_size,                                          sample_weight=sample_weight,                                           **kwargs)         return self


Question: # For it to be a probability, we must scale it.  For this reason         # we'll also scale atol.         X = check_array(X, order='C', dtype=DTYPE)        N = self.tree_.data.shape[0]        if self.tree_.sample_weight is None:            N = self.tree_.data.shape[0]        else:            N = self.tree_.sum_weight         atol_N = self.atol * N         log_density = self.tree_.kernel_density(             X, h=self.bandwidth, kernel=self.kernel, atol=atol_N,


Question: data = np.asarray(self.tree_.data)          rng = check_random_state(random_state)        i = rng.randint(data.shape[0], size=n_samples)        u = rng.uniform(0, 1, size=n_samples)        if self.tree_.sample_weight is None:            i = (u * data.shape[0]).astype(np.int64)        else:            cumsum_weight = np.cumsum(np.asarray(self.tree_.sample_weight))            sum_weight = cumsum_weight[-1]            i = np.searchsorted(cumsum_weight, u * sum_weight)         if self.kernel == 'gaussian':             return np.atleast_2d(rng.normal(data[i], self.bandwidth))


Question: labels_true, labels_pred = check_clusterings(labels_true, labels_pred)     n_samples, = labels_true.shape     c = contingency_matrix(labels_true, labels_pred, sparse=True)    c = contingency_matrix(labels_true, labels_pred,                           sparse=True).astype(np.int64)     tk = np.dot(c.data, c.data) - n_samples     pk = np.sum(np.asarray(c.sum(axis=0)).ravel() ** 2) - n_samples     qk = np.sum(np.asarray(c.sum(axis=1)).ravel() ** 2) - n_samples    return tk / np.sqrt(pk * qk) if tk != 0. else 0.    return np.sqrt(tk / pk) * np.sqrt(tk / qk) if tk != 0. else 0.   def entropy(labels):

fowlkesmallowsscore returns RuntimeWarning when variables get too big <!-- If your issue is a usage question, submit it here instead: StackOverflow with the scikit-learn tag: Mailing List: For more information, see User Questions: http://scikit- --<!-- Instructions For Filing a Bug: --<!-- Example: Joblib Error thrown when calling fit on LatentDirichletAllocation with evaluateevery 0--sklearn\metrics\cluster\ return tk / (pk qk) if tk != 0. else 0. This line produces RuntimeWarning: overflow encountered in intscalars when (pk qk) is bigger than 2*32, thus bypassing the int32 limit. Any code when pk and qk gets too big. <!-- Example: If the code is too long, feel free to put it in a public gist and link it in the issue: --<!-- Example: No error is thrown. Please paste or describe the expected results.--Be able to calculate tk / (pk qk) and return a float. <!-- Please paste or specifically describe the actual output or traceback. --it returns 'nan' instead. I propose to use (tk / pk) * (tk / qk) instead, which gives same result and ensuring not bypassing int32 <!-- Please run the following snippet and paste the output below. import platform; print(platform.platform()) import sys; print("Python", sys.version) import numpy; print("NumPy", numpy.version) import scipy; print("SciPy", scipy.version) import sklearn; print("Scikit-Learn", sklearn.version) --18.1 <!-- Thanks for contributing! --


Question: def fit(self, X, y=None):         """Estimate model parameters with the EM algorithm.         The method fits the model `n_init` times and set the parameters with        The method fits the model ``n_init`` times and sets the parameters with         which the model has the largest likelihood or lower bound. Within each        trial, the method iterates between E-step and M-step for `max_iter`        trial, the method iterates between E-step and M-step for ``max_iter``         times until the change of likelihood or lower bound is less than        `tol`, otherwise, a `ConvergenceWarning` is raised.        ``tol``, otherwise, a ``ConvergenceWarning`` is raised.        If ``warm_start`` is ``True``, then ``n_init`` is ignored and a single        initialization is performed upon the first call. Upon consecutive        calls, training starts where it left off.          Parameters         ----------

In Gaussian mixtures, when ninit 1, the lowerbound is not always the max In Gaussian mixtures, when is set to any value greater than 1, the is not the max lower bound across all initializations, but just the lower bound of the last initialization. The bug can be fixed by adding the following line just before in : The test that should have caught this bug is in , but it just does a single test, so it had a 50% chance of missing the issue. It should be updated to try many random states. No error. In Gaussian mixtures, when ninit 1, the lowerbound is not always the max In Gaussian mixtures, when is set to any value greater than 1, the is not the max lower bound across all initializations, but just the lower bound of the last initialization. The bug can be fixed by adding the following line just before in : The test that should have caught this bug is in , but it just does a single test, so it had a 50% chance of missing the issue. It should be updated to try many random states. No error.


Question: if do_init:                 self._initialize_parameters(X, random_state)                self.lower_bound_ = -np.infty            lower_bound = (-np.infty if do_init else self.lower_bound_)              for n_iter in range(1, self.max_iter + 1):                prev_lower_bound = self.lower_bound_                prev_lower_bound = lower_bound                  log_prob_norm, log_resp = self._e_step(X)                 self._m_step(X, log_resp)                self.lower_bound_ = self._compute_lower_bound(                lower_bound = self._compute_lower_bound(                     log_resp, log_prob_norm)                 change = self.lower_bound_ - prev_lower_bound                change = lower_bound - prev_lower_bound                 self._print_verbose_msg_iter_end(n_iter, change)                  if abs(change) < self.tol:                     self.converged_ = True                     break             self._print_verbose_msg_init_end(self.lower_bound_)            self._print_verbose_msg_init_end(lower_bound)             if self.lower_bound_ > max_lower_bound:                max_lower_bound = self.lower_bound_            if lower_bound > max_lower_bound:                max_lower_bound = lower_bound                 best_params = self._get_parameters()                 best_n_iter = n_iter


Question: self._set_parameters(best_params)         self.n_iter_ = best_n_iter        self.lower_bound_ = max_lower_bound          return log_resp.argmax(axis=1) diff --git a/sklearn/mixture/gaussian_mixture.py b/sklearn/mixture/gaussian_mixture.py-- a/sklearn/mixture/gaussian_mixture.py++ b/sklearn/mixture/gaussian_mixture.py


Question: If 'warm_start' is True, the solution of the last fitting is used as         initialization for the next call of fit(). This can speed up         convergence when fit is called several times on similar problems.<ins>        In that case, 'n_init' is ignored and only a single initialization        occurs upon the first call.</ins>         See :term:`the Glossary <warm_start>`.      verbose : int, default to 0.


Question: Number of step used by the best fit of EM to reach the convergence.      lower_bound_ : float        Log-likelihood of the best fit of EM.        Lower bound value on the log-likelihood (of the training data with        respect to the model) of the best fit of EM.      See Also     --------


Question: func, w0, fprime=None,                 args=(X, target, 1. / C, sample_weight),                 iprint=(verbose > 0) - 1, pgtol=tol, maxiter=max_iter)            if info["warnflag"] == 1 and verbose > 0:            if info["warnflag"] == 1:                 warnings.warn("lbfgs failed to converge. Increase the number "                               "of iterations.", ConvergenceWarning)             # In scipy <= 1.0.0, nit may exceed maxiter.diff --git a/sklearn/svm/base.py b/sklearn/svm/base.py-- a/sklearn/svm/base.py++ b/sklearn/svm/base.py

No warning when LogisticRegression does not converge <!-- If your issue is a usage question, submit it here instead: StackOverflow with the scikit-learn tag: Mailing List: For more information, see User Questions: http://scikit- --<!-- Instructions For Filing a Bug: --<!-- Example: Joblib Error thrown when calling fit on LatentDirichletAllocation with evaluateevery 0--I've run LogisticRegressionCV on the Wisconsin Breast Cancer data, and the output of was 100 for all but 1 of the variables. The default of 100 iterations was probably not sufficient in this case. Should there not be some kind of warning? I have done some tests and ~3000 iterations was probably a better choice for maxiter... <!-- Example: If the code is too long, feel free to put it in a public gist and link it in the issue: --<!-- Example: No error is thrown. Please paste or describe the expected results.--Some kind of error to be shown. E.g: "result did not converge, try increasing the maximum number of iterations (maxiter)" <!-- Please run the following snippet and paste the output below. import platform; print(platform.platform()) import sys; print("Python", sys.version) import numpy; print("NumPy", numpy.version) import scipy; print("SciPy", scipy.version) import sklearn; print("Scikit-Learn", sklearn.version) --import platform; print(platform.platform()) Darwin-16.7.0-x8664-i386-64bit import sys; print("Python", sys.version) ('Python', '2.7.14 (default, Oct 5 2017, 02:28:52) \n[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]') import numpy; print("NumPy", numpy.version) ('NumPy', '1.13.3') import scipy; print("SciPy", scipy.version) ('SciPy', '1.0.0') import sklearn; print("Scikit-Learn", sklearn.version) ('Scikit-Learn', '0.19.1') <!-- Thanks for contributing! --


Question: # on 32-bit platforms, we can't get to the UINT_MAX limit that     # srand supports     n_iter_ = max(n_iter_)    if n_iter_ >= max_iter and verbose > 0:    if n_iter_ >= max_iter:         warnings.warn("Liblinear failed to converge, increase "                       "the number of iterations.", ConvergenceWarning)


Question: sublinear_tf : boolean, default=False         Apply sublinear tf scaling, i.e. replace tf with 1 + log(tf). <ins>    Attributes    ----------    idf_ : array, shape (n_features)        The inverse document frequency (IDF) vector; only defined        if  ``use_idf`` is True.</ins>     References     ----------

Setting idf is impossible <!-- Instructions For Filing a Bug: --Rather than a bug i guess that would go as a sort of "enhancement proposition" ? I'm currently trying to persist a by basically saving its parameters in a mongoDB database and then rebuilding it alike. This technique works for but simply blocks for as there is no way to set . Is there any actual architectural reason why setting this attributes raise an error ? if yes, do you have an idea for a workaround ? I obviously want to avoid keeping the matrix on which it has been fitted as it would completely mess up the architecture (i believe that the saving/loading process should be separated from the whole treatment/learning process and trying to keep both would mean having to propagate a dirty return) functioning example on CountVectorizer This might not seem very impressive, but dictionnaries can be stored inside of mongoDB databases, which means that you can basically restore the or at least an identical copy of it by simply storing and the output of . Now the incriminated piece of code I would expect that at least one would work. However, both return an error. In the first case, it seems logical, as there is no idf/idf_ parameter In the second case, i suppose that encapsulation forbids the direct setting I think that being able to reproduce a fitted object (even if it is only for non-classifier objects) without having to recompute it at each launch would benefit a lot of applications. I'm currently developping a RestAPI that has to do heavy computations on data before feeding it to the vectorizer, having to relearn the whole model with each computing is very slow, and means i have to currently wait up to half an hour for modifications that are sometimes about 1 line of code. Windows-10-10.0. ('Python', '2.7.11 (default, Feb 16 2016, 09:58:36) [MSC v.1500 64 bit (AMD64)]') ('NumPy', '1.11.0') ('SciPy', '0.17.1') ('Scikit-Learn', '0.17.1') <!-- Thanks for contributing! --


Question: # which means hasattr(self, "idf_") is False         return np.ravel(self._idf_diag.sum(axis=0)) <ins>    @idf_.setter    def idf_(self, value):        value = np.asarray(value, dtype=np.float64)        n_features = value.shape[0]        self._idf_diag = sp.spdiags(value, diags=0, m=n_features,                                    n=n_features, format='csr')</ins>  class TfidfVectorizer(CountVectorizer):     """Convert a collection of raw documents to a matrix of TF-IDF features.


Question: vocabulary_ : dict         A mapping of terms to feature indices.     idf_ : array, shape = [n_features], or None        The learned idf vector (global term weights)        when ``use_idf`` is set to True, None otherwise.    idf_ : array, shape (n_features)        The inverse document frequency (IDF) vector; only defined        if  ``use_idf`` is True.      stop_words_ : set         Terms that were ignored because they either:


Question: def idf_(self):         return self._tfidf.idf_ <ins>    @idf_.setter    def idf_(self, value):        self._validate_vocabulary()        if hasattr(self, 'vocabulary_'):            if len(self.vocabulary_) != len(value):                raise ValueError("idf length = %d must be equal "                                 "to vocabulary size = %d" %                                 (len(value), len(self.vocabulary)))        self._tfidf.idf_ = value</ins>     def fit(self, raw_documents, y=None):         """Learn vocabulary and idf from training set.


Question: def get_feature_names(self):         """Array mapping from feature integer indices to feature name"""<ins>        if not hasattr(self, 'vocabulary_'):            self._validate_vocabulary()</ins>         self._check_vocabulary()          return [t for t, i in sorted(six.iteritems(self.vocabulary_),

CountVectorizer's getfeaturenames raise not NotFittedError when the vocabulary parameter is provided If you initialize a and try to perform a transformation without training you will get a exception. On the other hand if you provide the at the initialization of the vectorizer you could transform a corpus without a prior training, right? The 's calls method which sets the instance variable. In the same manner I believe that the method should not raise if the vocabulary parameter is provided but the vectorizer has not been trained.


Question: from collections import defaultdict import itertools import array<ins>import warnings</ins>  import numpy as np import scipy.sparse as sp

MultiLabelBinarizer breaks when seeing unseen there be an option to handle this instead? <!-- Instructions For Filing a Bug: --I am not sure if it's intended for MultiLabelBinarizer to fit and transform only seen data or not. However, there are many times that it is not possible/not in our interest to know all of the classes that we're fitting at training time. For convenience, I am wondering if there should be another parameter that allows us to ignore the unseen classes by just setting them to 0? Example: Result: array([[1, 0, 0], [0, 1, 0], [0, 0, 0]]) (the current version 0.19.0 would say ) I can open a PR for this if this is a desired behavior. Others also have similar issue:


Question: sklearn.preprocessing.OneHotEncoder : encode categorical integer features         using a one-hot aka one-of-K scheme.     """<ins></ins>     def __init__(self, classes=None, sparse_output=False):         self.classes = classes         self.sparse_output = sparse_output


Question: """         indices = array.array('i')         indptr = array.array('i', [0])        unknown = set()         for labels in y:            indices.extend(set(class_mapping[label] for label in labels))            index = set()            for label in labels:                try:                    index.add(class_mapping[label])                except KeyError:                    unknown.add(label)            indices.extend(index)             indptr.append(len(indices))        if unknown:            warnings.warn('unknown class(es) {0} will be ignored'                          .format(sorted(unknown, key=str)))         data = np.ones(len(indices), dtype=int)          return sp.csr_matrix((data, indices, indptr),


Question: # not a data type (e.g. a column named dtype in a pandas DataFrame)         dtype_orig = None <ins>    # check if the object contains several dtypes (typically a pandas    # DataFrame), and store them. If not, store None.    dtypes_orig = None    if hasattr(array, "dtypes") and hasattr(array, "__array__"):        dtypes_orig = np.array(array.dtypes)</ins>     if dtype_numeric:         if dtype_orig is not None and dtype_orig.kind == "O":             # if input is object, convert to float.

warnondtype with DataFrame No warning is thrown Linux-4.4.0-116-generic-x8664-with-debian-stretch-sid Python 3.6.3 (default, Nov 3 2017, 19:19:16) [GCC 7.2.0] NumPy 1.13.1 SciPy 0.19.1 Scikit-Learn 0.20.dev0 Pandas 0.21.0 warnondtype with DataFrame No warning is thrown Linux-4.4.0-116-generic-x8664-with-debian-stretch-sid Python 3.6.3 (default, Nov 3 2017, 19:19:16) [GCC 7.2.0] NumPy 1.13.1 SciPy 0.19.1 Scikit-Learn 0.20.dev0 Pandas 0.21.0


Question: if copy and np.may_share_memory(array, array_orig):         array = np.array(array, dtype=dtype, order=order) <ins>    if (warn_on_dtype and dtypes_orig is not None and            {array.dtype} != set(dtypes_orig)):        # if there was at the beginning some other types than the final one        # (for instance in a DataFrame that can contain several dtypes) then        # some data must have been converted        msg = ("Data with input dtype %s were all converted to %s%s."               % (', '.join(map(str, sorted(set(dtypes_orig)))), array.dtype,                  context))        warnings.warn(msg, DataConversionWarning, stacklevel=3)</ins>     return array


Question: # look up sampled parameter settings in parameter grid             param_grid = ParameterGrid(self.param_distributions)             grid_size = len(param_grid)            if grid_size < self.n_iter:                raise ValueError(                    "The total space of parameters %d is smaller "                    "than n_iter=%d. For exhaustive searches, use "                    "GridSearchCV." % (grid_size, self.n_iter))            for i in sample_without_replacement(grid_size, self.n_iter,            n_iter = self.n_iter            if grid_size < n_iter:                warnings.warn(                    'The total space of parameters %d is smaller '                    'than n_iter=%d. Running %d iterations. For exhaustive '                    'searches, use GridSearchCV.'                    % (grid_size, self.n_iter, grid_size), UserWarning)                n_iter = grid_size            for i in sample_without_replacement(grid_size, n_iter,                                                 random_state=rnd):                 yield param_grid[i]

[RandomizedSearchCV] Do not enforce that niter is less than or equal to size of search space Instantiating with greater than the size of (i.e. the product of the length of each distribution/array in the grid) will fail with an exception at . This is a bit annoying for me because I have an app where I'm letting the user specify the number of iterations to run from the command line, also I've been fiddling around with the param grid so keeps changing. I don't want to have to work out the exact grid size when it goes below, say, 50; if I specify that should be interpreted as an upper bound on the number of iterations. Would it be possible to add an option (off by default) to the constructor specifying whether to throw in such cases? e.g. By passing (the option would default to )


Question: 'shape (%d, %d) or (%d, %d)' % (                         coef.shape[0], coef.shape[1], classes.size,                         n_features, classes.size, n_features + 1))            w0[:, :coef.shape[1]] = coef            if n_classes == 1:                w0[0, :coef.shape[1]] = -coef                w0[1, :coef.shape[1]] = coef            else:                w0[:, :coef.shape[1]] = coef      if multi_class == 'multinomial':         # fmin_l_bfgs_b and newton-cg accepts only ravelled parameters.

Warm start bug when fitting a LogisticRegression model on binary outcomes with . <!-- If your issue is a usage question, submit it here instead: StackOverflow with the scikit-learn tag: Mailing List: For more information, see User Questions: http://scikit- --<!-- Instructions For Filing a Bug: --Bug when fitting a LogisticRegression model on binary outcomes with multiclass='multinomial' when using warm start. Note that it is similar to the issue here i.e. only using a 1D object on binary outcomes even when using as opposed to a 2D object. <!-- Example: If the code is too long, feel free to put it in a public gist and link it in the issue: --from sklearn.linearmodel import LogisticRegression import sklearn.metrics import numpy as np # Set up a logistic regression object lr = LogisticRegression(C=, multiclass='multinomial', solver='sag', tol=0.0001, warmstart=True, verbose=0) # Set independent variable values Z = ([ [ 0. , 0. ], [ 1., 0. ], [ 1., -0.], [-0., -0.], [ 1., 1.], [-0., -1.], [-1., -0.], [ 2. , -0.], [-0., -1.], [ 2., -0.], [-1., 2.], [-1., 2. ], [ 2., -1.], [ 0., -0.], [ 1., 0.], [ 1., -0.], [ 0., 1.], [-0., -0.], [-1., -0.], [-1. , 0.]])