ucimlrepo

repository·main·Indexed 19 days ago

https://github.com/uci-ml-repo/ucimlrepo

A Python package for importing datasets from the UC Irvine Machine Learning Repository into scripts and Jupyter notebooks as pandas DataFrames. It provides the fetch_ucirepo function to load data and metadata using a dataset ID or name, and the list_available_datasets function to search and filter available datasets.

Tokens
2.4K
Snippets
11
Records
13
Agent score
64%

What's inside ucimlrepo

  1. Access dataset features, targets, and metadata

    main

    Once a dataset is fetched, you can access its components through the returned object:

    Data Access

    Data is stored in the .data attribute as pandas dataframes:

    • dataset.data.features: Feature columns.
    • dataset.data.targets: Target columns.
    • dataset.data.ids: ID columns.
    • dataset.data.original: A dataframe containing all IDs, features, and targets combined.
    • dataset.data.headers: A list of all variable names/headers.

    Metadata Access

    Metadata is stored in the .metadata attribute. Common fields include:

    • dataset.metadata.uci_id: Unique identifier.
    • dataset.metadata.num_instances: Number of rows/samples.
    • dataset.metadata.additional_info.summary: General summary text.

    Variable Information

    To see variable details (name, role, type, description, etc.) in a tabular format, use:

    • dataset.variables
    from ucimlrepo import fetch_ucirepo
    
    heart_disease = fetch_ucirepo(id=45)
    
    # Access data
    X = heart_disease.data.features
    y = heart_disease.data.targets
    
    # Access metadata
    print(heart_disease.metadata.uci_id)
    print(heart_disease.metadata.num_instances)
    
    # Access variable info
    print(heart_disease.variables)
  2. How the dataset object structure works

    main

    The fetch_ucirepo function returns a dotdict, which is a specialized dictionary that allows accessing keys as attributes. This is particularly useful for navigating the nested metadata and data structures returned by the API.

    Access Patterns

    • Data Access: dataset.data.features or dataset.data.targets.
    • Metadata Access: dataset.metadata.intro_paper or dataset.metadata.additional_info.
    • Variable Access: dataset.variables returns a pandas DataFrame, which is ideal for tabular inspection of feature roles and descriptions.
  3. Fetch a dataset using fetch_ucirepo

    main

    The fetch_ucirepo function loads a dataset from the UCI ML Repository, including dataframes and metadata. You must provide either a dataset id or a name (or a substring of the name) as a keyword argument. You cannot provide both.

    from ucimlrepo import fetch_ucirepo
    
    # Fetch by ID
    dataset = fetch_ucirepo(id=45)
    
    # Alternatively, fetch by name
    dataset = fetch_ucirepo(name='Heart Disease')
  4. List or search available datasets

    main

    Use list_available_datasets() to print a list of datasets available for import. You can refine the list using the following optional keyword arguments:

    • filter: Filter datasets based on a category (e.g., filter='aim-ahead').
    • search: Search for datasets whose name contains the provided query string.
    from ucimlrepo import list_available_datasets
    
    # List all
    list_available_datasets()
    
    # Search for a specific name
    list_available_datasets(search='Heart')
  5. Reference: fetch_ucirepo return object structure

    main

    The object returned by fetch_ucirepo contains the following structured data:

    ### data (pandas DataFrames)
    - ids
    - features
    - targets
    - original
    - headers (list)
    
    ### metadata
    - uci_id
    - name
    - abstract
    - area
    - task
    - characteristics
    - num_instances
    - num_features
    - feature_types
    - target_col
    - index_col
    - has_missing_values
    - missing_values_symbol
    - year_of_dataset_creation
    - dataset_doi
    - creators
    - intro_paper
    - repository_url
    - data_url
    - additional_info (dict with keys: summary, purpose, funding, instances_represent, recommended_data_splits, sensitive_data, preprocessing_description, variable_info, citation)
    - external_url
    
    ### variables (DataFrame)
    - name
    - role
    - type
    - demographic
    - description
    - units
    - missing_values
  6. Import a dataset using `fetch_ucirepo`

    main

    The fetch_ucirepo function allows you to import datasets from the UCI Machine Learning Repository using either a unique integer id or a string name.

    from ucimlrepo import fetch_ucirepo
    
    # Import by ID
    sepsis = fetch_ucirepo(id=827)
    
    # Import by Name
    glioma = fetch_ucirepo(name='glioma')
  7. List available datasets

    main

    Use list_available_datasets() to see which datasets are available for import. You can refine the list using the filter parameter for specific projects (e.g., filter='aim-ahead') or the search parameter to find datasets matching a specific string (e.g., search='diabe').

    from ucimlrepo import list_available_datasets
    
    # List all datasets
    list_available_datasets()
    
    # Filter by project
    list_available_datasets(filter='aim-ahead')
    
    # Search for specific keywords
    list_available_datasets(search='diabe')
  8. Access variable information

    main

    The .variables attribute provides detailed information about each variable in the dataset, including its type and role, presented in a tabular DataFrame format.

    # Display variable information in a DataFrame format
    print(sepsis.variables)
  9. Access dataset features and targets

    main

    The data attribute of a fetched dataset object provides access to the actual data stored in pandas DataFrames.

    Available data components:

    • data.features: The feature set (independent variables).
    • data.targets: The target set (dependent variables).
    • data.ids: Dataset identifiers (if available).
    • data.original: A combined DataFrame containing features, targets, and IDs.
    • data.headers: The column headers for the data.
    # Accessing data for model training
    X = iris.data.features
    y = iris.data.targets
    
    # Accessing other components
    print(iris.data.original)
    print(iris.data.headers)
  10. Access dataset metadata

    main

    Once a dataset is fetched, you can access its metadata attribute. This contains information such as the target column name, the title of the introductory paper, and additional summaries.

    Common metadata fields include:

    • metadata.target_col: The name of the target column.
    • metadata.intro_paper.title: The title of the paper associated with the dataset.
    • metadata.additional_info.summary: A summary of the dataset.
    import pprint
    
    # Access full metadata
    pprint.pp(sepsis.metadata)
    
    # Access specific metadata fields
    print(sepsis.metadata.target_col)
    print(sepsis.metadata.intro_paper.title)
    print(sepsis.metadata.additional_info.summary)
  11. List available datasets with list_available_datasets()

    main

    Use list_available_datasets() to print a formatted table of datasets available for import via Python. By default, it filters for datasets compatible with Python.

    Parameters

    • filter (str, optional): A label to filter datasets (e.g., 'python').
    • search (str, optional): A substring to search for in dataset names.
    • area (str, optional): A subject area to filter by.
    from ucimlrepo import list_available_datasets
    
    # List all python-compatible datasets
    list_available_datasets()
    
    # Search for specific datasets
    list_available_datasets(search='Heart')
    
    # Filter by area
    list_available_datasets(area='medical')