CuPy: NumPy & SciPy for GPU - A NumPy/SciPy-compatible array library for GPU-accelerated computing with Python, specifically built for AMD ROCm 4.3 platform
—
Statistical functions, sorting algorithms, and searching operations for data analysis and processing. All functions are GPU-accelerated and maintain NumPy compatibility.
Functions for computing statistical measures of data distributions.
def mean(a, axis=None, dtype=None, out=None, keepdims=False):
"""
Compute arithmetic mean along specified axis.
Parameters:
- a: array-like, input array
- axis: None or int or tuple, axis to compute mean over
- dtype: data type, output data type
- out: cupy.ndarray, output array
- keepdims: bool, keep reduced dimensions
Returns:
cupy.ndarray: Mean values
"""
def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
"""
Compute standard deviation along specified axis.
Parameters:
- a: array-like, input array
- axis: None or int or tuple, axis to compute std over
- dtype: data type, output data type
- out: cupy.ndarray, output array
- ddof: int, delta degrees of freedom
- keepdims: bool, keep reduced dimensions
Returns:
cupy.ndarray: Standard deviation
"""
def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
"""Compute variance along specified axis."""
def median(a, axis=None, out=None, overwrite_input=False, keepdims=False):
"""
Compute median along specified axis.
Parameters:
- a: array-like, input array
- axis: None or int or tuple, axis to compute median over
- out: cupy.ndarray, output array
- overwrite_input: bool, whether input can be overwritten
- keepdims: bool, keep reduced dimensions
Returns:
cupy.ndarray: Median values
"""
def average(a, axis=None, weights=None, returned=False):
"""
Compute weighted average along specified axis.
Parameters:
- a: array-like, input array
- axis: None or int or tuple, axis to average over
- weights: array-like, weights for each value
- returned: bool, return tuple (average, sum_of_weights)
Returns:
cupy.ndarray: Weighted average
tuple: (average, sum_of_weights) if returned=True
"""Functions for finding minimum, maximum, and quantile values.
def amax(a, axis=None, out=None, keepdims=False, initial=None, where=None):
"""
Return maximum of array or maximum along axis.
Parameters:
- a: array-like, input array
- axis: None or int or tuple, axis for maximum
- out: cupy.ndarray, output array
- keepdims: bool, keep reduced dimensions
- initial: scalar, minimum value of output
- where: array-like, elements to include
Returns:
cupy.ndarray: Maximum values
"""
def amin(a, axis=None, out=None, keepdims=False, initial=None, where=None):
"""Return minimum of array or minimum along axis."""
def max(a, axis=None, out=None, keepdims=False, initial=None, where=None):
"""Alias for amax."""
def min(a, axis=None, out=None, keepdims=False, initial=None, where=None):
"""Alias for amin."""
def ptp(a, axis=None, out=None, keepdims=False):
"""
Return range (maximum - minimum) along axis.
Returns:
cupy.ndarray: Peak-to-peak values
"""
def percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False):
"""
Compute qth percentile along specified axis.
Parameters:
- a: array-like, input array
- q: float or sequence, percentile(s) to compute (0-100)
- axis: None or int or tuple, axis to compute over
- out: cupy.ndarray, output array
- overwrite_input: bool, whether input can be overwritten
- interpolation: str, interpolation method
- keepdims: bool, keep reduced dimensions
Returns:
cupy.ndarray: Percentile values
"""
def quantile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False):
"""
Compute qth quantile along specified axis.
Parameters:
- q: float or sequence, quantile(s) to compute (0-1)
Returns:
cupy.ndarray: Quantile values
"""Efficient GPU-accelerated sorting algorithms.
def sort(a, axis=-1, kind=None, order=None):
"""
Return sorted copy of array.
Parameters:
- a: array-like, input array
- axis: int or None, axis to sort along
- kind: str, sorting algorithm (ignored, uses GPU-optimized method)
- order: str or list, field order for structured arrays
Returns:
cupy.ndarray: Sorted array
"""
def argsort(a, axis=-1, kind=None, order=None):
"""
Return indices that would sort array.
Returns:
cupy.ndarray: Indices that sort a along specified axis
"""
def lexsort(keys, axis=-1):
"""
Perform indirect stable sort using multiple keys.
Parameters:
- keys: tuple of arrays, sort keys (last key is primary)
- axis: int, axis to sort along
Returns:
cupy.ndarray: Indices that sort the keys
"""
def msort(a):
"""Sort array along first axis."""
def sort_complex(a):
"""Sort complex array using real part first, then imaginary."""
def partition(a, kth, axis=-1, kind='introselect', order=None):
"""
Return partitioned copy where kth element is in final sorted position.
Parameters:
- a: array-like, input array
- kth: int or sequence, element index(es) for partitioning
- axis: int or None, axis to partition along
- kind: str, selection algorithm
- order: str or list, field order for structured arrays
Returns:
cupy.ndarray: Partitioned array
"""
def argpartition(a, kth, axis=-1, kind='introselect', order=None):
"""Return indices that partition array."""Functions for finding elements in arrays.
def argmax(a, axis=None, out=None):
"""
Return indices of maximum values along axis.
Parameters:
- a: array-like, input array
- axis: int or None, axis to find maximum along
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: Indices of maximum values
"""
def argmin(a, axis=None, out=None):
"""Return indices of minimum values along axis."""
def nonzero(a):
"""
Return indices of non-zero elements.
Returns:
tuple: Tuple of arrays, one for each dimension
"""
def where(condition, x=None, y=None):
"""
Return elements chosen from x or y depending on condition.
Parameters:
- condition: array-like, boolean condition
- x, y: array-like, values to choose from
Returns:
cupy.ndarray: Elements from x where condition is True, y elsewhere
tuple: If x and y not given, equivalent to nonzero(condition)
"""
def searchsorted(a, v, side='left', sorter=None):
"""
Find indices where elements should be inserted to maintain order.
Parameters:
- a: array-like, sorted input array
- v: array-like, values to insert
- side: {'left', 'right'}, insertion side for equal values
- sorter: array-like, indices that sort a
Returns:
cupy.ndarray: Insertion indices
"""Functions for computing histograms and frequency distributions.
def histogram(a, bins=10, range=None, normed=None, weights=None, density=None):
"""
Compute histogram of array.
Parameters:
- a: array-like, input data
- bins: int or sequence, bin specification
- range: tuple, range of bins
- normed: bool, deprecated, use density
- weights: array-like, weights for each value
- density: bool, return probability density
Returns:
tuple: (hist, bin_edges)
"""
def histogram2d(x, y, bins=10, range=None, normed=None, weights=None, density=None):
"""
Compute 2D histogram of two arrays.
Parameters:
- x, y: array-like, input data
- bins: int or sequence, bin specification
- range: array-like, bin ranges
- normed: bool, deprecated, use density
- weights: array-like, weights for each value
- density: bool, return probability density
Returns:
tuple: (H, xedges, yedges)
"""
def histogramdd(sample, bins=10, range=None, normed=None, weights=None, density=None):
"""Compute multidimensional histogram."""
def bincount(x, weights=None, minlength=0):
"""
Count occurrences of each value in array of non-negative integers.
Parameters:
- x: array-like, input array of non-negative integers
- weights: array-like, weights for each value
- minlength: int, minimum number of bins
Returns:
cupy.ndarray: Number of occurrences of each value
"""
def digitize(x, bins, right=False):
"""
Return indices of bins to which each value belongs.
Parameters:
- x: array-like, input array
- bins: array-like, bin edges (monotonic)
- right: bool, whether intervals include right edge
Returns:
cupy.ndarray: Bin indices for each value in x
"""Functions for computing correlations and covariances.
def corrcoef(x, y=None, rowvar=True, bias=None, ddof=None):
"""
Return Pearson correlation coefficients.
Parameters:
- x: array-like, input array
- y: array-like, additional input array
- rowvar: bool, whether rows are variables
- bias: deprecated
- ddof: deprecated
Returns:
cupy.ndarray: Correlation coefficient matrix
"""
def cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None):
"""
Estimate covariance matrix.
Parameters:
- m: array-like, input array
- y: array-like, additional input array
- rowvar: bool, whether rows are variables
- bias: bool, use biased estimator
- ddof: int, delta degrees of freedom
- fweights: array-like, frequency weights
- aweights: array-like, observation weights
Returns:
cupy.ndarray: Covariance matrix
"""
def correlate(a, v, mode='valid'):
"""
Cross-correlation of two 1D sequences.
Parameters:
- a, v: array-like, input sequences
- mode: {'valid', 'same', 'full'}, output size
Returns:
cupy.ndarray: Cross-correlation result
"""import cupy as cp
# Generate sample data
data = cp.random.normal(10, 2, size=10000)
# Descriptive statistics
mean_val = cp.mean(data)
std_val = cp.std(data)
var_val = cp.var(data)
median_val = cp.median(data)
# Percentiles
q25 = cp.percentile(data, 25)
q75 = cp.percentile(data, 75)
iqr = q75 - q25
print(f"Mean: {mean_val:.2f}, Std: {std_val:.2f}")
print(f"Median: {median_val:.2f}, IQR: {iqr:.2f}")import cupy as cp
# Create test array
arr = cp.random.randint(0, 100, size=1000)
# Sort array
sorted_arr = cp.sort(arr)
sort_indices = cp.argsort(arr)
# Find extreme values
max_idx = cp.argmax(arr)
min_idx = cp.argmin(arr)
# Search for values
search_values = cp.array([25, 50, 75])
insertion_points = cp.searchsorted(sorted_arr, search_values)
# Boolean indexing
mask = arr > 50
high_values = arr[mask]
high_indices = cp.nonzero(mask)[0]import cupy as cp
# Generate data from multiple distributions
normal_data = cp.random.normal(0, 1, 5000)
uniform_data = cp.random.uniform(-3, 3, 5000)
# Compute histograms
hist_normal, bins_normal = cp.histogram(normal_data, bins=50, density=True)
hist_uniform, bins_uniform = cp.histogram(uniform_data, bins=50, density=True)
# 2D histogram
x = cp.random.normal(0, 1, 1000)
y = x + cp.random.normal(0, 0.5, 1000) # Correlated data
hist_2d, xedges, yedges = cp.histogram2d(x, y, bins=20)Install with Tessl CLI
npx tessl i tessl/pypi-cupy-rocm-4-3