benchmarks.wiki / Public workspace
BigCodeBench / BigCodeBench v0.1.0_hf d3f45f1b-504e-5ca8-8a95-4bae26778774
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(s1, s2, n_clusters=3):
"""
Perform K-Means clustering on data points from two pandas Series and visualize the clusters.
Parameters:
- s1 (pandas.Series): The first series of data. Each value in the series represents a data point's coordinate along one dimension.
- s2 (pandas.Series): The second series of data. Each value corresponds to a data point's coordinate along another dimension. The length of s2 must match that of s1.
- n_clusters (int, optional): The number of clusters to form as well as the number of centroids to generate. Defaults to 3.
Returns:
- tuple: A tuple containing the following elements:
- ndarray: An array of cluster labels indicating the cluster each data point belongs to.
- matplotlib.axes.Axes: The Axes object of the plot, which shows the data points colored according to their cluster labels.
Raises:
- ValueError: If either s1 or s2 is not a pandas Series, raise "s1 and s2 must be pandas Series"
- ValueError: If s1 and s2 have different lengths, raise "s1 and s2 must have the same length"
Requirements:
- pandas
- scikit-learn
- matplotlib
Notes:
- The function needs to ensure that s1 and s2 are pandas Series of equal length.
- It then performs K-Means clustering on the combined data points from s1 and s2.
- After clustering, it creates a scatter plot where each cluster is visualized with a different color.
- The plot title is set to "K-Means Clustering" to describe the visualization technique.
- A legend is added, which uses elements from the scatter plot to describe each cluster.
Example:
>>> s1 = pd.Series(np.random.rand(100), name='feature1')
>>> s2 = pd.Series(np.random.rand(100), name='feature2')
>>> labels, ax = task_func(s1, s2, n_clusters=4)
>>> print(ax.get_title())
K-Means Clustering
"""
instruct prompt
Perform K-Means clustering on data points from two pandas Series and visualize the clusters.
Note that: Notes: The function needs to ensure that s1 and s2 are pandas Series of equal length. It then performs K-Means clustering on the combined data points from s1 and s2. After clustering, it creates a scatter plot where each cluster is visualized with a different color. The plot title is set to "K-Means Clustering" to describe the visualization technique. A legend is added, which uses elements from the scatter plot to describe each cluster.
The function should raise the exception for: ValueError: If either s1 or s2 is not a pandas Series, raise "s1 and s2 must be pandas Series" ValueError: If s1 and s2 have different lengths, raise "s1 and s2 must have the same length"
The function should output with:
tuple: A tuple containing the following elements:
ndarray: An array of cluster labels indicating the cluster each data point belongs to.
matplotlib.axes.Axes: The Axes object of the plot, which shows the data points colored according to their cluster labels.
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(s1, s2, n_clusters=3):
code prompt
Code
import pandas as pd
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
def task_func(s1, s2, n_clusters=3):
entry point
task_func
libs
- pandas
- 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