openml/datasets/functions.py
def create_dataset(
    name,
    description,
    creator,
    contributor,
    collection_date,
    language,
    licence,
    attributes,
    data,
    default_target_attribute,
    ignore_attribute,
    citation,
    row_id_attribute=None,
    original_data_url=None,
    paper_url=None,
    update_comment=None,
    version_label=None,
):
    """Create a dataset.

    This function creates an OpenMLDataset object.
    The OpenMLDataset object contains information related to the dataset
    and the actual data file.

    Parameters
    ----------
    name : str
        Name of the dataset.
    description : str
        Description of the dataset.
    creator : str
        The person who created the dataset.
    contributor : str
        People who contributed to the current version of the dataset.
    collection_date : str
        The date the data was originally collected, given by the uploader.
    language : str
        Language in which the data is represented.
        Starts with 1 upper case letter, rest lower case, e.g. 'English'.
    licence : str
        License of the data.
    attributes : list, dict, or 'auto'
        A list of tuples. Each tuple consists of the attribute name and type.
        If passing a pandas DataFrame, the attributes can be automatically
        inferred by passing ``'auto'``. Specific attributes can be manually
        specified by a passing a dictionary where the key is the name of the
        attribute and the value is the data type of the attribute.
    data : ndarray, list, dataframe, coo_matrix, shape (n_samples, n_features)
        An array that contains both the attributes and the targets. When
        providing a dataframe, the attribute names and type can be inferred by
        passing ``attributes='auto'``.
        The target feature is indicated as meta-data of the dataset.
    default_target_attribute : str
        The default target attribute, if it exists.
        Can have multiple values, comma separated.
    ignore_attribute : str | list
        Attributes that should be excluded in modelling,
        such as identifiers and indexes.
        Can have multiple values, comma separated.
    citation : str
        Reference(s) that should be cited when building on this data.
    version_label : str, optional
        Version label provided by user.
         Can be a date, hash, or some other type of id.
    row_id_attribute : str, optional
        The attribute that represents the row-id column, if present in the
        dataset. If ``data`` is a dataframe and ``row_id_attribute`` is not
        specified, the index of the dataframe will be used as the
        ``row_id_attribute``. If the name of the index is ``None``, it will
        be discarded.

        .. versionadded: 0.8
            Inference of ``row_id_attribute`` from a dataframe.
    original_data_url : str, optional
        For derived data, the url to the original dataset.
    paper_url : str, optional
        Link to a paper describing the dataset.
    update_comment : str, optional
        An explanation for when the dataset is uploaded.

    Returns
    -------
    class:`openml.OpenMLDataset`
        Dataset description."""

    if isinstance(data, pd.DataFrame):
        # infer the row id from the index of the dataset
        if row_id_attribute is None:
            row_id_attribute = data.index.name
        # When calling data.values, the index will be skipped.
        # We need to reset the index such that it is part of the data.
        if data.index.name is not None:
            data = data.reset_index()

    if attributes == "auto" or isinstance(attributes, dict):
        if not hasattr(data, "columns"):
            raise ValueError(
                "Automatically inferring attributes requires "
                "a pandas DataFrame. A {!r} was given instead.".format(data)
            )
        # infer the type of data for each column of the DataFrame
        attributes_ = attributes_arff_from_df(data)
        if isinstance(attributes, dict):
            # override the attributes which was specified by the user
            for attr_idx in range(len(attributes_)):
                attr_name = attributes_[attr_idx][0]
                if attr_name in attributes.keys():
                    attributes_[attr_idx] = (attr_name, attributes[attr_name])
    else:
        attributes_ = attributes
    ignore_attributes = _expand_parameter(ignore_attribute)
    _validated_data_attributes(ignore_attributes, attributes_, "ignore_attribute")

    default_target_attributes = _expand_parameter(default_target_attribute)
    _validated_data_attributes(default_target_attributes, attributes_, "default_target_attribute")

    if row_id_attribute is not None:
        is_row_id_an_attribute = any([attr[0] == row_id_attribute for attr in attributes_])
        if not is_row_id_an_attribute:
            raise ValueError(
                "'row_id_attribute' should be one of the data attribute. "
                " Got '{}' while candidates are {}.".format(
                    row_id_attribute, [attr[0] for attr in attributes_]
                )
            )

    if hasattr(data, "columns"):
        if all(isinstance(dtype, pd.SparseDtype) for dtype in data.dtypes):
            data = data.sparse.to_coo()
            # liac-arff only support COO matrices with sorted rows
            row_idx_sorted = np.argsort(data.row)
            data.row = data.row[row_idx_sorted]
            data.col = data.col[row_idx_sorted]
            data.data = data.data[row_idx_sorted]
        else:
            data = data.values

    if isinstance(data, (list, np.ndarray)):
        if isinstance(data[0], (list, np.ndarray)):
            data_format = "arff"
        elif isinstance(data[0], dict):
            data_format = "sparse_arff"
        else:
            raise ValueError(
                "When giving a list or a numpy.ndarray, "
                "they should contain a list/ numpy.ndarray "
                "for dense data or a dictionary for sparse "
                "data. Got {!r} instead.".format(data[0])
            )
    elif isinstance(data, coo_matrix):
        data_format = "sparse_arff"
    else:
        raise ValueError(
            "When giving a list or a numpy.ndarray, "
            "they should contain a list/ numpy.ndarray "
            "for dense data or a dictionary for sparse "
            "data. Got {!r} instead.".format(data[0])
        )

    arff_object = {
        "relation": name,
        "description": description,
        "attributes": attributes_,
        "data": data,
    }

    # serializes the ARFF dataset object and returns a string
    arff_dataset = arff.dumps(arff_object)
    try:
        # check if ARFF is valid
        decoder = arff.ArffDecoder()
        return_type = arff.COO if data_format == "sparse_arff" else arff.DENSE
        decoder.decode(arff_dataset, encode_nominal=True, return_type=return_type)
    except arff.ArffException:
        raise ValueError(
            "The arguments you have provided \
                             do not construct a valid ARFF file"
        )

    return OpenMLDataset(
        name=name,
        description=description,
        data_format=data_format,
        creator=creator,
        contributor=contributor,
        collection_date=collection_date,
        language=language,
        licence=licence,
        default_target_attribute=default_target_attribute,
        row_id_attribute=row_id_attribute,
        ignore_attribute=ignore_attribute,
        citation=citation,
        version_label=version_label,
        original_data_url=original_data_url,
        paper_url=paper_url,
        update_comment=update_comment,
        dataset=arff_dataset,
    )
Create a dataset.

    This function creates an OpenMLDataset object.
    The OpenMLDataset object contains information related to the dataset
    and the actual data file.

    Parameters
    ----------
    name : str
        Name of the dataset.
    description : str
        Description of the dataset.
    creator : str
        The person who created the dataset.
    contributor : str
        People who contributed to the current version of the dataset.
    collection_date : str
        The date the data was originally collected, given by the uploader.
    language : str
        Language in which the data is represented.
        Starts with 1 upper case letter, rest lower case, e.g. 'English'.
    licence : str
        License of the data.
    attributes : list, dict, or 'auto'
        A list of tuples. Each tuple consists of the attribute name and type.
        If passing a pandas DataFrame, the attributes can be automatically
        inferred by passing ``'auto'``. Specific attributes can be manually
        specified by a passing a dictionary where the key is the name of the
        attribute and the value is the data type of the attribute.
    data : ndarray, list, dataframe, coo_matrix, shape (n_samples, n_features)
        An array that contains both the attributes and the targets. When
        providing a dataframe, the attribute names and type can be inferred by
        passing ``attributes='auto'``.
        The target feature is indicated as meta-data of the dataset.
    default_target_attribute : str
        The default target attribute, if it exists.
        Can have multiple values, comma separated.
    ignore_attribute : str | list
        Attributes that should be excluded in modelling,
        such as identifiers and indexes.
        Can have multiple values, comma separated.
    citation : str
        Reference(s) that should be cited when building on this data.
    version_label : str, optional
        Version label provided by user.
         Can be a date, hash, or some other type of id.
    row_id_attribute : str, optional
        The attribute that represents the row-id column, if present in the
        dataset. If ``data`` is a dataframe and ``row_id_attribute`` is not
        specified, the index of the dataframe will be used as the
        ``row_id_attribute``. If the name of the index is ``None``, it will
        be discarded.

        .. versionadded: 0.8
            Inference of ``row_id_attribute`` from a dataframe.
    original_data_url : str, optional
        For derived data, the url to the original dataset.
    paper_url : str, optional
        Link to a paper describing the dataset.
    update_comment : str, optional
        An explanation for when the dataset is uploaded.

    Returns
    -------
    class:`openml.OpenMLDataset`
        Dataset description.
Create a dataset.
This function creates an OpenMLDataset object.
The OpenMLDataset object contains information related to the dataset
and the actual data file.

Parameters

name : str
Name of the dataset.
description : str
Description of the dataset.
creator : str
The person who created the dataset.
contributor : str
People who contributed to the current version of the dataset.
collection_date : str
The date the data was originally collected, given by the uploader.
language : str
Language in which the data is represented.
Starts with 1 upper case letter, rest lower case, e.g. 'English'.
licence : str
License of the data.
attributes : list, dict, or 'auto'
A list of tuples. Each tuple consists of the attribute name and type.
If passing a pandas DataFrame, the attributes can be automatically
inferred by passing ``'auto'``. Specific attributes can be manually
specified by a passing a dictionary where the key is the name of the
attribute and the value is the data type of the attribute.
data : ndarray, list, dataframe, coo_matrix, shape (n_samples, n_features)
An array that contains both the attributes and the targets. When
providing a dataframe, the attribute names and type can be inferred by
passing ``attributes='auto'``.
The target feature is indicated as meta-data of the dataset.
default_target_attribute : str
The default target attribute, if it exists.
Can have multiple values, comma separated.
ignore_attribute : str | list
Attributes that should be excluded in modelling,
such as identifiers and indexes.
Can have multiple values, comma separated.
citation : str
Reference(s) that should be cited when building on this data.
version_label : str, optional
Version label provided by user.
Can be a date, hash, or some other type of id.
row_id_attribute : str, optional
The attribute that represents the row-id column, if present in the
dataset.

0.8
Inference of ``row_id_attribute`` from a dataframe.
original_data_url : str, optional
For derived data, the url to the original dataset.
paper_url : str, optional
Link to a paper describing the dataset.
update_comment : str, optional
An explanation for when the dataset is uploaded.

Returns


Create a dataset . This function creates an OpenMLDataset object . The OpenMLDataset object contains information related to the dataset and the actual data file . Parameters name : str Name of the dataset . description : str Description of the dataset . creator : str The person who created the dataset . contributor : str People who contributed to the current version of the dataset . collection_date : str The date the data was originally collected given by the uploader . language : str Language in which the data is represented . Starts with 1 upper case letter rest lower case e . g . ' English ' . licence : str License of the data . attributes : list dict or ' auto ' A list of tuples . Each tuple consists of the attribute name and type . If passing a pandas DataFrame the attributes can be automatically inferred by passing ` ` ' auto ' ` ` . Specific attributes can be manually specified by a passing a dictionary where the key is the name of the attribute and the value is the data type of the attribute . data : ndarray list dataframe coo_matrix shape ( n_samples n_features ) An array that contains both the attributes and the targets . When providing a dataframe the attribute names and type can be inferred by passing ` ` attributes = ' auto ' ` ` . The target feature is indicated as meta - data of the dataset . default_target_attribute : str The default target attribute if it exists . Can have multiple values comma separated . ignore_attribute : str | list Attributes that should be excluded in modelling such as identifiers and indexes . Can have multiple values comma separated . citation : str Reference ( s ) that should be cited when building on this data . version_label : str optional Version label provided by user . Can be a date hash or some other type of id . row_id_attribute : str optional The attribute that represents the row - id column if present in the dataset . 0 . 8 Inference of ` ` row_id_attribute ` ` from a dataframe . original_data_url : str optional For derived data the url to the original dataset . paper_url : str optional Link to a paper describing the dataset . update_comment : str optional An explanation for when the dataset is uploaded . Returns
def create_dataset(
    name,
    description,
    creator,
    contributor,
    collection_date,
    language,
    licence,
    attributes,
    data,
    default_target_attribute,
    ignore_attribute,
    citation,
    row_id_attribute=None,
    original_data_url=None,
    paper_url=None,
    update_comment=None,
    version_label=None,
):
    if isinstance(data, pd.DataFrame):
        if row_id_attribute is None:
            row_id_attribute = data.index.name
        if data.index.name is not None:
            data = data.reset_index()
    if attributes == "auto" or isinstance(attributes, dict):
        if not hasattr(data, "columns"):
            raise ValueError(
                "Automatically inferring attributes requires "
                "a pandas DataFrame. A {!r} was given instead.".format(data)
            )
        attributes_ = attributes_arff_from_df(data)
        if isinstance(attributes, dict):
            for attr_idx in range(len(attributes_)):
                attr_name = attributes_[attr_idx][0]
                if attr_name in attributes.keys():
                    attributes_[attr_idx] = (attr_name, attributes[attr_name])
    else:
        attributes_ = attributes
    ignore_attributes = _expand_parameter(ignore_attribute)
    _validated_data_attributes(ignore_attributes, attributes_, "ignore_attribute")
    default_target_attributes = _expand_parameter(default_target_attribute)
    _validated_data_attributes(default_target_attributes, attributes_, "default_target_attribute")
    if row_id_attribute is not None:
        is_row_id_an_attribute = any([attr[0] == row_id_attribute for attr in attributes_])
        if not is_row_id_an_attribute:
            raise ValueError(
                "'row_id_attribute' should be one of the data attribute. "
                " Got '{}' while candidates are {}.".format(
                    row_id_attribute, [attr[0] for attr in attributes_]
                )
            )
    if hasattr(data, "columns"):
        if all(isinstance(dtype, pd.SparseDtype) for dtype in data.dtypes):
            data = data.sparse.to_coo()
            row_idx_sorted = np.argsort(data.row)
            data.row = data.row[row_idx_sorted]
            data.col = data.col[row_idx_sorted]
            data.data = data.data[row_idx_sorted]
        else:
            data = data.values
    if isinstance(data, (list, np.ndarray)):
        if isinstance(data[0], (list, np.ndarray)):
            data_format = "arff"
        elif isinstance(data[0], dict):
            data_format = "sparse_arff"
        else:
            raise ValueError(
                "When giving a list or a numpy.ndarray, "
                "they should contain a list/ numpy.ndarray "
                "for dense data or a dictionary for sparse "
                "data. Got {!r} instead.".format(data[0])
            )
    elif isinstance(data, coo_matrix):
        data_format = "sparse_arff"
    else:
        raise ValueError(
            "When giving a list or a numpy.ndarray, "
            "they should contain a list/ numpy.ndarray "
            "for dense data or a dictionary for sparse "
            "data. Got {!r} instead.".format(data[0])
        )
    arff_object = {
        "relation": name,
        "description": description,
        "attributes": attributes_,
        "data": data,
    }
    arff_dataset = arff.dumps(arff_object)
    try:
        decoder = arff.ArffDecoder()
        return_type = arff.COO if data_format == "sparse_arff" else arff.DENSE
        decoder.decode(arff_dataset, encode_nominal=True, return_type=return_type)
    except arff.ArffException:
        raise ValueError(
            "The arguments you have provided \
                             do not construct a valid ARFF file"
        )
    return OpenMLDataset(
        name=name,
        description=description,
        data_format=data_format,
        creator=creator,
        contributor=contributor,
        collection_date=collection_date,
        language=language,
        licence=licence,
        default_target_attribute=default_target_attribute,
        row_id_attribute=row_id_attribute,
        ignore_attribute=ignore_attribute,
        citation=citation,
        version_label=version_label,
        original_data_url=original_data_url,
        paper_url=paper_url,
        update_comment=update_comment,
        dataset=arff_dataset,
    )
def create_dataset ( name , description , creator , contributor , collection_date , language , licence , attributes , data , default_target_attribute , ignore_attribute , citation , row_id_attribute = None , original_data_url = None , paper_url = None , update_comment = None , version_label = None , ) : if isinstance ( data , pd . DataFrame ) : if row_id_attribute is None : row_id_attribute = data . index . name if data . index . name is not None : data = data . reset_index ( ) if attributes == "auto" or isinstance ( attributes , dict ) : if not hasattr ( data , "columns" ) : raise ValueError ( "Automatically inferring attributes requires " "a pandas DataFrame. A {!r} was given instead." . format ( data ) ) attributes_ = attributes_arff_from_df ( data ) if isinstance ( attributes , dict ) : for attr_idx in range ( len ( attributes_ ) ) : attr_name = attributes_ [ attr_idx ] [ 0 ] if attr_name in attributes . keys ( ) : attributes_ [ attr_idx ] = ( attr_name , attributes [ attr_name ] ) else : attributes_ = attributes ignore_attributes = _expand_parameter ( ignore_attribute ) _validated_data_attributes ( ignore_attributes , attributes_ , "ignore_attribute" ) default_target_attributes = _expand_parameter ( default_target_attribute ) _validated_data_attributes ( default_target_attributes , attributes_ , "default_target_attribute" ) if row_id_attribute is not None : is_row_id_an_attribute = any ( [ attr [ 0 ] == row_id_attribute for attr in attributes_ ] ) if not is_row_id_an_attribute : raise ValueError ( "'row_id_attribute' should be one of the data attribute. " " Got '{}' while candidates are {}." . format ( row_id_attribute , [ attr [ 0 ] for attr in attributes_ ] ) ) if hasattr ( data , "columns" ) : if all ( isinstance ( dtype , pd . SparseDtype ) for dtype in data . dtypes ) : data = data . sparse . to_coo ( ) row_idx_sorted = np . argsort ( data . row ) data . row = data . row [ row_idx_sorted ] data . col = data . col [ row_idx_sorted ] data . data = data . data [ row_idx_sorted ] else : data = data . values if isinstance ( data , ( list , np . ndarray ) ) : if isinstance ( data [ 0 ] , ( list , np . ndarray ) ) : data_format = "arff" elif isinstance ( data [ 0 ] , dict ) : data_format = "sparse_arff" else : raise ValueError ( "When giving a list or a numpy.ndarray, " "they should contain a list/ numpy.ndarray " "for dense data or a dictionary for sparse " "data. Got {!r} instead." . format ( data [ 0 ] ) ) elif isinstance ( data , coo_matrix ) : data_format = "sparse_arff" else : raise ValueError ( "When giving a list or a numpy.ndarray, " "they should contain a list/ numpy.ndarray " "for dense data or a dictionary for sparse " "data. Got {!r} instead." . format ( data [ 0 ] ) ) arff_object = { "relation" : name , "description" : description , "attributes" : attributes_ , "data" : data , } arff_dataset = arff . dumps ( arff_object ) try : decoder = arff . ArffDecoder ( ) return_type = arff . COO if data_format == "sparse_arff" else arff . DENSE decoder . decode ( arff_dataset , encode_nominal = True , return_type = return_type ) except arff . ArffException : raise ValueError ( "The arguments you have provided \
                             do not construct a valid ARFF file" ) return OpenMLDataset ( name = name , description = description , data_format = data_format , creator = creator , contributor = contributor , collection_date = collection_date , language = language , licence = licence , default_target_attribute = default_target_attribute , row_id_attribute = row_id_attribute , ignore_attribute = ignore_attribute , citation = citation , version_label = version_label , original_data_url = original_data_url , paper_url = paper_url , update_comment = update_comment , dataset = arff_dataset , )
Create a dataset.
Create a dataset .
"""Create a dataset.

    This function creates an OpenMLDataset object.
    The OpenMLDataset object contains information related to the dataset
    and the actual data file.

    Parameters
    ----------
    name : str
        Name of the dataset.
    description : str
        Description of the dataset.
    creator : str
        The person who created the dataset.
    contributor : str
        People who contributed to the current version of the dataset.
    collection_date : str
        The date the data was originally collected, given by the uploader.
    language : str
        Language in which the data is represented.
        Starts with 1 upper case letter, rest lower case, e.g. 'English'.
    licence : str
        License of the data.
    attributes : list, dict, or 'auto'
        A list of tuples. Each tuple consists of the attribute name and type.
        If passing a pandas DataFrame, the attributes can be automatically
        inferred by passing ``'auto'``. Specific attributes can be manually
        specified by a passing a dictionary where the key is the name of the
        attribute and the value is the data type of the attribute.
    data : ndarray, list, dataframe, coo_matrix, shape (n_samples, n_features)
        An array that contains both the attributes and the targets. When
        providing a dataframe, the attribute names and type can be inferred by
        passing ``attributes='auto'``.
        The target feature is indicated as meta-data of the dataset.
    default_target_attribute : str
        The default target attribute, if it exists.
        Can have multiple values, comma separated.
    ignore_attribute : str | list
        Attributes that should be excluded in modelling,
        such as identifiers and indexes.
        Can have multiple values, comma separated.
    citation : str
        Reference(s) that should be cited when building on this data.
    version_label : str, optional
        Version label provided by user.
         Can be a date, hash, or some other type of id.
    row_id_attribute : str, optional
        The attribute that represents the row-id column, if present in the
        dataset. If ``data`` is a dataframe and ``row_id_attribute`` is not
        specified, the index of the dataframe will be used as the
        ``row_id_attribute``. If the name of the index is ``None``, it will
        be discarded.

        .. versionadded: 0.8
            Inference of ``row_id_attribute`` from a dataframe.
    original_data_url : str, optional
        For derived data, the url to the original dataset.
    paper_url : str, optional
        Link to a paper describing the dataset.
    update_comment : str, optional
        An explanation for when the dataset is uploaded.

    Returns
    -------
    class:`openml.OpenMLDataset`
        Dataset description.""" # infer the row id from the index of the dataset # When calling data.values, the index will be skipped. # We need to reset the index such that it is part of the data. # infer the type of data for each column of the DataFrame # override the attributes which was specified by the user # liac-arff only support COO matrices with sorted rows # serializes the ARFF dataset object and returns a string # check if ARFF is valid
name
description
creator
contributor
collection_date
language
licence
attributes
data
default_target_attribute
ignore_attribute
citation
row_id_attribute
original_data_url
paper_url
update_comment
version_label