benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf 378c6a04-496a-533c-80b4-9df7ea10fd82

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
import matplotlib.pyplot as plt

def task_func(df, n_clusters=3, random_state=0):
    """
    Convert the 'date' column of a DataFrame to ordinal, perform KMeans clustering on 'date' and 'value' columns, and plot the clusters.

    Parameters:
        df (pandas.DataFrame): The DataFrame with columns 'group', 'date', and 'value'.
        n_clusters (int): The number of clusters for KMeans. Defaults to 3.
        random_state (int): Random state for KMeans to ensure reproducibility. Defaults to 0.


    Returns:
        matplotlib.axes.Axes: The Axes object containing the scatter plot of the clusters.

    Required names:
        x: 'Date (ordinal)'
        ylabel: 'Value'
        title: 'KMeans Clustering of Value vs Date'
    
    Raises:
        ValueError: If the DataFrame is empty or lacks required columns.

    Requirements:
        - pandas
        - sklearn.cluster
        - matplotlib.pyplot

    Example:
        >>> df = pd.DataFrame({
        ...     "group": ["A", "A", "A", "B", "B"],
        ...     "date": pd.to_datetime(["2022-01-02", "2022-01-13", "2022-02-01", "2022-02-23", "2022-03-05"]),
        ...     "value": [10, 20, 16, 31, 56],
        ... })
        >>> ax = task_func(df)
    """

instruct prompt

Convert the 'date' column of a DataFrame to ordinal, perform KMeans clustering on 'date' and 'value' columns, and plot the clusters. Required names: x: 'Date (ordinal)' ylabel: 'Value' title: 'KMeans Clustering of Value vs Date'
The function should raise the exception for: ValueError: If the DataFrame is empty or lacks required columns.
The function should output with:
    matplotlib.axes.Axes: The Axes object containing the scatter plot of the clusters.
You should write self-contained code starting with:

Code

import pandas as pd
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
def task_func(df, n_clusters=3, random_state=0):

code prompt

Code

import pandas as pd
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
def task_func(df, n_clusters=3, random_state=0):

entry point

task_func

libs

  • pandas
  • matplotlib
  • 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