benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf 207041a0-de51-50c0-9fb3-4832fe792d12

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

def task_func(df):
    """
    Perform Principal Component Analysis (PCA) on the dataframe and visualize the two main components.

    Parameters:
        df (DataFrame): The input dataframe containing numerical data.

    Returns:
        DataFrame: A pandas DataFrame with the principal components named 'Principal Component 1' and 'Principal Component 2'.
        Axes: A Matplotlib Axes object representing the scatter plot of the two principal components. The plot includes:
              - Title: '2 Component PCA'
              - X-axis label: 'Principal Component 1'
              - Y-axis label: 'Principal Component 2'

    Raises:
        ValueError: If the input is not a DataFrame, or if the DataFrame is empty.

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

    Example:
        >>> df = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)), columns=list('ABCD'))
        >>> pca_df, ax = task_func(df)
        >>> plt.show()
    """

instruct prompt

Perform Principal Component Analysis (PCA) on the dataframe and visualize the two main components.
The function should raise the exception for: ValueError: If the input is not a DataFrame, or if the DataFrame is empty.
The function should output with:
    DataFrame: A pandas DataFrame with the principal components named 'Principal Component 1' and 'Principal Component 2'.
    Axes: A Matplotlib Axes object representing the scatter plot of the two principal components. The plot includes:
    Title: '2 Component PCA'
    X-axis label: 'Principal Component 1'
    Y-axis label: 'Principal Component 2'
You should write self-contained code starting with:

Code

import pandas as pd
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
def task_func(df):

code prompt

Code

import pandas as pd
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
def task_func(df):

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