Skip to content

Instantly share code, notes, and snippets.

@Mason-McGough
Created March 6, 2019 03:54
Show Gist options
  • Save Mason-McGough/4e3391c699056b31e9fe65da5655cd48 to your computer and use it in GitHub Desktop.
Save Mason-McGough/4e3391c699056b31e9fe65da5655cd48 to your computer and use it in GitHub Desktop.
A collection of functions for examining and using Numpy arrays
import numpy as np
def subsample(x, n_samples, return_choices=False, replace=False):
"""
Randomly sample a subset of x.
Inputs:
x - The array to sample from.
n_samples - The number of samples to select from x.
return_choices - If True, returns an array of the random indices used to
construct output x. (Default: False)
replace - If True, samples are taken with replacement. (Default: False)
Outputs:
x - The subsampled array.
"""
choices = np.random.choice(range(x.shape[0]), size=[n_samples], replace=replace)
if return_choices:
return x[choices], choices
else:
return x[choices]
def describe(x):
"""
Print the shape, min, max, and datatype of array.
Inputs:
x - Numpy-like array to be described.
Outputs:
None
"""
print('{}, {}, {}'.format(x.shape, [np.min(x), np.max(x)], x.dtype))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment