Created
December 20, 2017 18:34
-
-
Save messiest/c168d77623c9b59a4c47e144db5a4368 to your computer and use it in GitHub Desktop.
bootstrap samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bootstrap(df, n, to_df=True): | |
""" | |
generate n bootstraped samples from a DataFrame | |
""" | |
assert isinstance(df, type(pd.DataFrame())),\ | |
f"Expected pandas.DataFrame, got type: {type(df)}" | |
sample = {column: np.random.choice(df[column], size=int(n)) for column in df.columns} # column: bootstrap sample | |
if to_df: sample = pd.DataFrame.from_dict(sample) # convert to DataFrame | |
return sample |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment