benchmarks.wiki / Public workspace
BigCodeBench / BigCodeBench v0.1.0_hf 98d53918-ee4c-5da3-914e-7f0faa69d224
Problem
Answer published by the source. Consult the official source to check your work against its answer.
complete prompt
import pandas as pd
import numpy as np
from sklearn.decomposition import PCA
import seaborn as sns
import matplotlib.pyplot as plt
def task_func(df: pd.DataFrame):
"""
Perform PCA on a DataFrame (excluding non-numeric columns) and draw a scatter plot of the first two main components. The principal columns should be name 'Component 1' and 'Component 2'.
Missing values are replaced by column's average.
Parameters:
df (DataFrame): The pandas DataFrame.
Returns:
DataFrame: A pandas DataFrame with the first two principal components. The columns should be 'principal component 1' and 'principal component 2'.
Axes: A matplotlib Axes object representing the scatter plot. The xlabel should be 'principal component' and the ylabel 'principal component 2'.
Requirements:
- pandas
- numpy
- sklearn.decomposition.PCA
- seaborn
- matplotlib
Example:
>>> df = pd.DataFrame([[1,2,3],[4,5,6],[7.0,np.nan,9.0]], columns=["c1","c2","c3"])
>>> principalDf, ax = task_func(df)
>>> print(principalDf)
Component 1 Component 2
0 4.450915 -0.662840
1 -0.286236 1.472436
2 -4.164679 -0.809596
"""
instruct prompt
Perform PCA on a DataFrame (excluding non-numeric columns) and draw a scatter plot of the first two main components. The principal columns should be name 'Component 1' and 'Component 2'. Missing values are replaced by column's average.
The function should output with:
DataFrame: A pandas DataFrame with the first two principal components. The columns should be 'principal component 1' and 'principal component 2'.
Axes: A matplotlib Axes object representing the scatter plot. The xlabel should be 'principal component' and the ylabel 'principal component 2'.
You should write self-contained code starting with:
Code
import pandas as pd
import numpy as np
from sklearn.decomposition import PCA
import seaborn as sns
import matplotlib.pyplot as plt
def task_func(df: pd.DataFrame):
code prompt
Code
import pandas as pd
import numpy as np
from sklearn.decomposition import PCA
import seaborn as sns
import matplotlib.pyplot as plt
def task_func(df: pd.DataFrame):
entry point
task_func
libs
- pandas
- matplotlib
- numpy
- sklearn
- seaborn
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