benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf 77743fe6-9670-58a8-85c6-a5d0a04e6626

Problem

Answer published by the source. Consult the official source to check your work against its answer.

complete prompt

import pandas as pd
from sklearn.cluster import KMeans


def task_func(data, n_clusters=3, seed=None):
    """
    Perform K-Means clustering on the given DataFrame using the sklearn KMeans algorithm. 

    The function expects a DataFrame with numerical values, as KMeans cannot handle categorical data. 
    It applies standard KMeans clustering from the sklearn library to form clusters. The number of clusters is 
    configurable via the 'n_clusters' parameter, defaulting to 3. The Number of times the k-means algorithm is run with 
    different centroid seeds (n_init) is set to 10. The function returns an array of cluster labels 
    corresponding to each data point in the input as well as the fitted KMeans model.

    Parameters:
    data (pandas.DataFrame): A DataFrame consisting of only numerical data. Each row represents a distinct data point.
    n_clusters (int, optional): The number of clusters to form. Defaults to 3.
    seed (int, optional): The seed used for setting the random stat in the KMeans clustering algorith.
                          Used for making results reproducable.

    Returns:
    numpy.ndarray: An array of integers (cluster labels) corresponding to the input data. Each label is an integer 
                   representing the cluster to which a row of data has been assigned.
    sklearn.cluster.KMeans: The fitted KMeans Model.

    Raises:
    - ValueError: If the DataFrame contains non numeric entries.

    Requirements:
    - pandas
    - sklearn.cluster.KMeans

    Example:
    >>> np.random.seed(12)
    >>> data = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
    >>> labels, model = task_func(data, n_clusters=4, seed=12)
    >>> print(labels) 
    [1 0 1 0 1 2 1 3 3 1 0 3 0 0 2 2 2 3 3 3 1 0 1 0 3 1 1 1 1 3 1 3 0 3 1 0 0
     2 0 3 2 1 2 1 1 3 1 1 1 1 2 2 1 0 0 3 3 0 0 1 1 2 0 0 2 2 0 2 2 2 0 3 2 3
     3 1 2 1 1 3 1 1 1 2 1 0 0 1 2 1 3 0 0 2 3 3 3 2 3 2]
    >>> print(model)
    KMeans(n_clusters=4, n_init=10, random_state=12)

    >>> data = pd.DataFrame({
    ...     'a': [1, 20, 2, 22, 100],
    ...     'b': [1, 20, 2, 22, 100]
    ... })
    >>> labels, model = task_func(data, seed=213)
    >>> print(labels)
    [2 0 2 0 1]
    >>> print(model)
    KMeans(n_clusters=3, n_init=10, random_state=213)
    """

instruct prompt

Perform K-Means clustering on the given DataFrame using the sklearn KMeans algorithm. The function expects a DataFrame with numerical values, as KMeans cannot handle categorical data. It applies standard KMeans clustering from the sklearn library to form clusters. The number of clusters is configurable via the 'n_clusters' parameter, defaulting to 3. The Number of times the k-means algorithm is run with different centroid seeds (n_init) is set to 10. The function returns an array of cluster labels corresponding to each data point in the input as well as the fitted KMeans model. >>> data = pd.DataFrame({ ...     'a': [1, 20, 2, 22, 100], ...     'b': [1, 20, 2, 22, 100] ... }) >>> labels, model = task_func(data, seed=213) >>> print(labels) [2 0 2 0 1] >>> print(model) KMeans(n_clusters=3, n_init=10, random_state=213)
The function should raise the exception for: ValueError: If the DataFrame contains non numeric entries.
The function should output with:
    numpy.ndarray: An array of integers (cluster labels) corresponding to the input data. Each label is an integer
    representing the cluster to which a row of data has been assigned.
    sklearn.cluster.KMeans: The fitted KMeans Model.
You should write self-contained code starting with:

Code

import pandas as pd
from sklearn.cluster import KMeans
def task_func(data, n_clusters=3, seed=None):

code prompt

Code

import pandas as pd
from sklearn.cluster import KMeans
def task_func(data, n_clusters=3, seed=None):

entry point

task_func

libs

  • pandas
  • sklearn

Discussion

Discussion

No discussion posts on this page yet. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.

See answer Answer published by the source

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.

Source and history

Official source

initial import