openml/datasets/dataset.py
def _convert_array_format(data, array_format, attribute_names):
        """Convert a dataset to a given array format.

        Converts to numpy array if data is non-sparse.
        Converts to a sparse dataframe if data is sparse.

        Parameters
        ----------
        array_format : str {'array', 'dataframe'}
            Desired data type of the output
            - If array_format='array'
                If data is non-sparse
                    Converts to numpy-array
                    Enforces numeric encoding of categorical columns
                    Missing values are represented as NaN in the numpy-array
                else returns data as is
            - If array_format='dataframe'
                If data is sparse
                    Works only on sparse data
                    Converts sparse data to sparse dataframe
                else returns data as is

        """

        if array_format == "array" and not scipy.sparse.issparse(data):
            # We encode the categories such that they are integer to be able
            # to make a conversion to numeric for backward compatibility
            def _encode_if_category(column):
                if column.dtype.name == "category":
                    column = column.cat.codes.astype(np.float32)
                    mask_nan = column == -1
                    column[mask_nan] = np.nan
                return column

            if data.ndim == 2:
                columns = {
                    column_name: _encode_if_category(data.loc[:, column_name])
                    for column_name in data.columns
                }
                data = pd.DataFrame(columns)
            else:
                data = _encode_if_category(data)
            try:
                return np.asarray(data, dtype=np.float32)
            except ValueError:
                raise PyOpenMLError(
                    "PyOpenML cannot handle string when returning numpy"
                    ' arrays. Use dataset_format="dataframe".'
                )
        elif array_format == "dataframe":
            if scipy.sparse.issparse(data):
                data = pd.DataFrame.sparse.from_spmatrix(data, columns=attribute_names)
        else:
            data_type = "sparse-data" if scipy.sparse.issparse(data) else "non-sparse data"
            logger.warning(
                "Cannot convert %s (%s) to '%s'. Returning input data."
                % (data_type, type(data), array_format)
            )
        return data
Convert a dataset to a given array format.

        Converts to numpy array if data is non-sparse.
        Converts to a sparse dataframe if data is sparse.

        Parameters
        ----------
        array_format : str {'array', 'dataframe'}
            Desired data type of the output
            - If array_format='array'
                If data is non-sparse
                    Converts to numpy-array
                    Enforces numeric encoding of categorical columns
                    Missing values are represented as NaN in the numpy-array
                else returns data as is
            - If array_format='dataframe'
                If data is sparse
                    Works only on sparse data
                    Converts sparse data to sparse dataframe
                else returns data as is

        
Convert a dataset to a given array format.
Converts to numpy array if data is non-sparse.
Converts to a sparse dataframe if data is sparse.

Parameters


Convert a dataset to a given array format . Converts to numpy array if data is non - sparse . Converts to a sparse dataframe if data is sparse . Parameters
def _convert_array_format(data, array_format, attribute_names):
        if array_format == "array" and not scipy.sparse.issparse(data):
            def _encode_if_category(column):
                if column.dtype.name == "category":
                    column = column.cat.codes.astype(np.float32)
                    mask_nan = column == -1
                    column[mask_nan] = np.nan
                return column
            if data.ndim == 2:
                columns = {
                    column_name: _encode_if_category(data.loc[:, column_name])
                    for column_name in data.columns
                }
                data = pd.DataFrame(columns)
            else:
                data = _encode_if_category(data)
            try:
                return np.asarray(data, dtype=np.float32)
            except ValueError:
                raise PyOpenMLError(
                    "PyOpenML cannot handle string when returning numpy"
                    ' arrays. Use dataset_format="dataframe".'
                )
        elif array_format == "dataframe":
            if scipy.sparse.issparse(data):
                data = pd.DataFrame.sparse.from_spmatrix(data, columns=attribute_names)
        else:
            data_type = "sparse-data" if scipy.sparse.issparse(data) else "non-sparse data"
            logger.warning(
                "Cannot convert %s (%s) to '%s'. Returning input data."
                % (data_type, type(data), array_format)
            )
        return data
def _convert_array_format ( data , array_format , attribute_names ) : if array_format == "array" and not scipy . sparse . issparse ( data ) : def _encode_if_category ( column ) : if column . dtype . name == "category" : column = column . cat . codes . astype ( np . float32 ) mask_nan = column == - 1 column [ mask_nan ] = np . nan return column if data . ndim == 2 : columns = { column_name : _encode_if_category ( data . loc [ : , column_name ] ) for column_name in data . columns } data = pd . DataFrame ( columns ) else : data = _encode_if_category ( data ) try : return np . asarray ( data , dtype = np . float32 ) except ValueError : raise PyOpenMLError ( "PyOpenML cannot handle string when returning numpy" ' arrays. Use dataset_format="dataframe".' ) elif array_format == "dataframe" : if scipy . sparse . issparse ( data ) : data = pd . DataFrame . sparse . from_spmatrix ( data , columns = attribute_names ) else : data_type = "sparse-data" if scipy . sparse . issparse ( data ) else "non-sparse data" logger . warning ( "Cannot convert %s (%s) to '%s'. Returning input data." % ( data_type , type ( data ) , array_format ) ) return data
Convert a dataset to a given array format.
Convert a dataset to a given array format .
"""Convert a dataset to a given array format.

        Converts to numpy array if data is non-sparse.
        Converts to a sparse dataframe if data is sparse.

        Parameters
        ----------
        array_format : str {'array', 'dataframe'}
            Desired data type of the output
            - If array_format='array'
                If data is non-sparse
                    Converts to numpy-array
                    Enforces numeric encoding of categorical columns
                    Missing values are represented as NaN in the numpy-array
                else returns data as is
            - If array_format='dataframe'
                If data is sparse
                    Works only on sparse data
                    Converts sparse data to sparse dataframe
                else returns data as is

        """ # We encode the categories such that they are integer to be able # to make a conversion to numeric for backward compatibility
data
array_format
attribute_names