benchmarks.wiki / Public workspace
BigCodeBench / BigCodeBench v0.1.0_hf 50e0ee88-f46d-5ca5-8ddb-3ee3208f9ae7
Problem
Answer published by the source. Consult the official source to check your work against its answer.
complete prompt
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
def task_func(df, age: int, height: int):
"""
Filters the input DataFrame based on specified 'Age' and 'Height' conditions and applies KMeans clustering.
- If the filtered dataframe has less than 3 columns, add to it a column 'Cluster' with 0 for each row.
- Otherwise, do a KMeans clustering (by Age and Height) with 3 clusters and add a column 'Cluster' to the dataframe which corresponds to the cluster
index of the cluster to which each row belongs to.
- Plot a scatter plot of the 'Age' and 'height' and colored by the cluster indices.
- the xlabel should be 'Age', the ylabel 'Height' and the title 'KMeans Clustering based on Age and Height'.
Parameters:
df (DataFrame): The text to analyze.
age (int): Filter out the rows of the dataframe which 'Age' value is less than or equal to this value.
height (int): Filter out the rows of the dataframe which 'Height' value is greater than or equal to this value.
Returns:
DataFrame: The filtered dataframe with the new column.
matplotlib.axes.Axes: The Axes object of the plotted data. If no KMeans was done, returns None.
Requirements:
- sklearn
- matplotlib
Example:
>>> import pandas as pd
>>> df = pd.DataFrame({
... 'Age': [30, 45, 60, 75],
... 'Height': [160, 170, 165, 190],
... 'Weight': [55, 65, 75, 85]
... })
>>> selected_df, ax = task_func(df, 50, 180)
>>> print(selected_df)
Age Height Weight Cluster
2 60 165 75 0
"""
instruct prompt
Filters the input DataFrame based on specified 'Age' and 'Height' conditions and applies KMeans clustering. - If the filtered dataframe has less than 3 columns, add to it a column 'Cluster' with 0 for each row. - Otherwise, do a KMeans clustering (by Age and Height) with 3 clusters and add a column 'Cluster' to the dataframe which corresponds to the cluster index of the cluster to which each row belongs to. - Plot a scatter plot of the 'Age' and 'height' and colored by the cluster indices. - the xlabel should be 'Age', the ylabel 'Height' and the title 'KMeans Clustering based on Age and Height'.
The function should output with:
DataFrame: The filtered dataframe with the new column.
matplotlib.axes.Axes: The Axes object of the plotted data. If no KMeans was done, returns None.
You should write self-contained code starting with:
Code
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
def task_func(df, age: int, height: int):
code prompt
Code
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
def task_func(df, age: int, height: int):
entry point
task_func
libs
- matplotlib
- sklearn
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
initial import