benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf 6a103c44-d77f-5098-8912-2cf6464644a1

Problem

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

complete prompt

import numpy as np
from sklearn.cluster import KMeans


def task_func(data, n_clusters):
    """
    Apply KMeans clustering to a 2D numeric array and find the indices of the data points in each cluster.

    Parameters:
    data (numpy array): The 2D numpy array for clustering.
    n_clusters (int): The number of clusters to form.

    Returns:
    dict: A dictionary where keys are cluster labels and values are lists of indices for data points in the cluster.

    Requirements:
    - numpy
    - sklearn.cluster

    Example:
    >>> data = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
    >>> cluster = task_func(data, 2)
    >>> cluster_list = list(cluster.values())
    >>> cluster_list.sort(key=lambda x: x[0])
    >>> print(cluster_list)
    [array([0, 1]), array([2, 3])]

    >>> data = np.array([[1, 1], [2, 2]])
    >>> cluster = task_func(data, 2)
    >>> cluster_list = list(cluster.values())
    >>> cluster_list.sort(key=lambda x: x[0])
    >>> print(cluster_list)
    [array([0]), array([1])]
    """

instruct prompt

Apply KMeans clustering to a 2D numeric array and find the indices of the data points in each cluster. >>> data = np.array([[1, 1], [2, 2]]) >>> cluster = task_func(data, 2) >>> cluster_list = list(cluster.values()) >>> cluster_list.sort(key=lambda x: x[0]) >>> print(cluster_list) [array([0]), array([1])]
The function should output with:
    dict: A dictionary where keys are cluster labels and values are lists of indices for data points in the cluster.
You should write self-contained code starting with:

Code

import numpy as np
from sklearn.cluster import KMeans
def task_func(data, n_clusters):

code prompt

Code

import numpy as np
from sklearn.cluster import KMeans
def task_func(data, n_clusters):

entry point

task_func

libs

  • numpy
  • 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