| |
- _covhelper(x, y=None, rowvar=True, allow_masked=True)
- Private function for the computation of covariance and correlation
coefficients.
- apply_along_axis(func1d, axis, arr, *args, **kwargs)
- Apply a function to 1-D slices along the given axis.
Execute `func1d(a, *args)` where `func1d` operates on 1-D arrays and `a`
is a 1-D slice of `arr` along `axis`.
Parameters
----------
func1d : function
This function should accept 1-D arrays. It is applied to 1-D
slices of `arr` along the specified axis.
axis : integer
Axis along which `arr` is sliced.
arr : ndarray
Input array.
args : any
Additional arguments to `func1d`.
Returns
-------
outarr : ndarray
The output array. The shape of `outarr` is identical to the shape of
`arr`, except along the `axis` dimension, where the length of `outarr`
is equal to the size of the return value of `func1d`. If `func1d`
returns a scalar `outarr` will have one fewer dimensions than `arr`.
See Also
--------
apply_over_axes : Apply a function repeatedly over multiple axes.
Examples
--------
>>> def my_func(a):
... """Average first and last element of a 1-D array"""
... return (a[0] + a[-1]) * 0.5
>>> b = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> np.apply_along_axis(my_func, 0, b)
array([4., 5., 6.])
>>> np.apply_along_axis(my_func, 1, b)
array([2., 5., 8.])
For a function that doesn't return a scalar, the number of dimensions in
`outarr` is the same as `arr`.
>>> def new_func(a):
... """Divide elements of a by 2."""
... return a * 0.5
>>> b = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> np.apply_along_axis(new_func, 0, b)
array([[ 0.5, 1. , 1.5],
[ 2. , 2.5, 3. ],
[ 3.5, 4. , 4.5]])
- average(a, axis=None, weights=None, returned=False)
- Average the array over the given axis.
Parameters
----------
axis : {None,int}, optional
Axis along which to perform the operation.
If None, applies to a flattened version of the array.
weights : {None, sequence}, optional
Sequence of weights.
The weights must have the shape of a, or be 1D with length
the size of a along the given axis.
If no weights are given, weights are assumed to be 1.
returned : {False, True}, optional
Flag indicating whether a tuple (result, sum of weights/counts)
should be returned as output (True), or just the result (False).
- compress_cols(a)
- Suppress whole columns of a 2D array that contain masked values.
- compress_rowcols(x, axis=None)
- Suppress the rows and/or columns of a 2D array that contain
masked values.
The suppression behavior is selected with the `axis` parameter.
- If axis is None, rows and columns are suppressed.
- If axis is 0, only rows are suppressed.
- If axis is 1 or -1, only columns are suppressed.
Parameters
----------
axis : int, optional
Axis along which to perform the operation.
If None, applies to a flattened version of the array.
Returns
-------
compressed_array : an ndarray.
- compress_rows(a)
- Suppress whole rows of a 2D array that contain masked values.
- corrcoef(x, y=None, rowvar=True, bias=False, allow_masked=True)
- The correlation coefficients formed from the array x, where the
rows are the observations, and the columns are variables.
corrcoef(x,y) where x and y are 1d arrays is the same as
corrcoef(transpose([x,y]))
Parameters
----------
x : ndarray
Input data. If x is a 1D array, returns the variance.
If x is a 2D array, returns the covariance matrix.
y : {None, ndarray} optional
Optional set of variables.
rowvar : {False, True} optional
If True, then each row is a variable with observations in columns.
If False, each column is a variable and the observations are in the rows.
bias : {False, True} optional
Whether to use a biased (True) or unbiased (False) estimate of the
covariance.
If True, then the normalization is by N, the number of non-missing
observations.
Otherwise, the normalization is by (N-1).
allow_masked : {True, False} optional
If True, masked values are propagated pair-wise: if a value is masked
in x, the corresponding value is masked in y.
If False, raises an exception.
See Also
--------
cov
- count_masked(arr, axis=None)
- Count the number of masked elements along the given axis.
Parameters
----------
axis : int, optional
Axis along which to count.
If None (default), a flattened version of the array is used.
- cov(x, y=None, rowvar=True, bias=False, allow_masked=True)
- Estimates the covariance matrix.
Normalization is by (N-1) where N is the number of observations (unbiased
estimate). If bias is True then normalization is by N.
By default, masked values are recognized as such. If x and y have the same
shape, a common mask is allocated: if x[i,j] is masked, then y[i,j] will
also be masked.
Setting `allow_masked` to False will raise an exception if values are
missing in either of the input arrays.
Parameters
----------
x : array_like
Input data.
If x is a 1D array, returns the variance.
If x is a 2D array, returns the covariance matrix.
y : array_like, optional
Optional set of variables.
rowvar : {False, True} optional
If rowvar is true, then each row is a variable with observations in
columns.
If rowvar is False, each column is a variable and the observations are
in the rows.
bias : {False, True} optional
Whether to use a biased (True) or unbiased (False) estimate of the
covariance.
If bias is True, then the normalization is by N, the number of
observations.
Otherwise, the normalization is by (N-1).
allow_masked : {True, False} optional
If True, masked values are propagated pair-wise: if a value is masked
in x, the corresponding value is masked in y.
If False, raises a ValueError exception when some values are missing.
Raises
------
ValueError:
Raised if some values are missing and allow_masked is False.
- dot(a, b, strict=False)
- Return the dot product of two 2D masked arrays a and b.
Like the generic numpy equivalent, the product sum is over the last
dimension of a and the second-to-last dimension of b. If strict is True,
masked values are propagated: if a masked value appears in a row or column,
the whole row or column is considered masked.
Parameters
----------
strict : {boolean}
Whether masked data are propagated (True) or set to 0 for the computation.
Notes
-----
The first argument is not conjugated.
- ediff1d(arr, to_end=None, to_begin=None)
- Computes the differences between consecutive elements of an array.
This function is the equivalent of `numpy.ediff1d` that takes masked
values into account.
See Also
--------
numpy.eddif1d : equivalent function for ndarrays.
Returns
-------
output : MaskedArray
- flatnotmasked_contiguous(a)
- Find contiguous unmasked data in a flattened masked array.
Return a sorted sequence of slices (start index, end index).
- flatnotmasked_edges(a)
- Find the indices of the first and last not masked values in a
1D masked array. If all values are masked, returns None.
- flatten_inplace(seq)
- Flatten a sequence in place.
- intersect1d(ar1, ar2)
- Returns the repeated or unique elements belonging to the two arrays.
Masked values are assumed equals one to the other.
The output is always a masked array
See Also
--------
numpy.intersect1d : equivalent function for ndarrays.
Examples
--------
>>> x = array([1, 3, 3, 3], mask=[0, 0, 0, 1])
>>> y = array([3, 1, 1, 1], mask=[0, 0, 0, 1])
>>> intersect1d(x, y)
masked_array(data = [1 1 3 3 --],
mask = [False False False False True],
fill_value = 999999)
- intersect1d_nu(ar1, ar2)
- Returns the unique elements common to both arrays.
Masked values are considered equal one to the other.
The output is always a masked array.
See Also
--------
intersect1d : Returns repeated or unique common elements.
numpy.intersect1d_nu : equivalent function for ndarrays.
Examples
--------
>>> x = array([1, 3, 3, 3], mask=[0, 0, 0, 1])
>>> y = array([3, 1, 1, 1], mask=[0, 0, 0, 1])
>>> intersect1d_nu(x, y)
masked_array(data = [1 3 --],
mask = [False False True],
fill_value = 999999)
- issequence(seq)
- Is seq a sequence (ndarray, list or tuple)?
- mask_cols(a, axis=None)
- Mask columns of a 2D array that contain masked values.
This function is a shortcut to ``mask_rowcols`` with `axis` equal to 1.
See Also
--------
mask_rowcols : Mask rows and/or columns of a 2D array.
masked_where : Mask where a condition is met.
Examples
--------
>>> import numpy.ma as ma
>>> a = np.zeros((3, 3), dtype=np.int)
>>> a[1, 1] = 1
>>> a
array([[0, 0, 0],
[0, 1, 0],
[0, 0, 0]])
>>> a = ma.masked_equal(a, 1)
>>> a
masked_array(data =
[[0 0 0]
[0 -- 0]
[0 0 0]],
mask =
[[False False False]
[False True False]
[False False False]],
fill_value=999999)
>>> ma.mask_cols(a)
masked_array(data =
[[0 -- 0]
[0 -- 0]
[0 -- 0]],
mask =
[[False True False]
[False True False]
[False True False]],
fill_value=999999)
- mask_rowcols(a, axis=None)
- Mask rows and/or columns of a 2D array that contain masked values.
Mask whole rows and/or columns of a 2D array that contain
masked values. The masking behavior is selected using the
`axis` parameter.
- If `axis` is None, rows *and* columns are masked.
- If `axis` is 0, only rows are masked.
- If `axis` is 1 or -1, only columns are masked.
Parameters
----------
a : array_like, MaskedArray
The array to mask. If not a MaskedArray instance (or if no array
elements are masked). The result is a MaskedArray with `mask` set
to `nomask` (False). Must be a 2D array.
axis : int, optional
Axis along which to perform the operation. If None, applies to a
flattened version of the array.
Returns
-------
a : MaskedArray
A modified version of the input array, masked depending on the value
of the `axis` parameter.
Raises
------
NotImplementedError
If input array `a` is not 2D.
See Also
--------
mask_rows : Mask rows of a 2D array that contain masked values.
mask_cols : Mask cols of a 2D array that contain masked values.
masked_where : Mask where a condition is met.
Notes
-----
The input array's mask is modified by this function.
Examples
--------
>>> import numpy.ma as ma
>>> a = np.zeros((3, 3), dtype=np.int)
>>> a[1, 1] = 1
>>> a
array([[0, 0, 0],
[0, 1, 0],
[0, 0, 0]])
>>> a = ma.masked_equal(a, 1)
>>> a
masked_array(data =
[[0 0 0]
[0 -- 0]
[0 0 0]],
mask =
[[False False False]
[False True False]
[False False False]],
fill_value=999999)
>>> ma.mask_rowcols(a)
masked_array(data =
[[0 -- 0]
[-- -- --]
[0 -- 0]],
mask =
[[False True False]
[ True True True]
[False True False]],
fill_value=999999)
- mask_rows(a, axis=None)
- Mask rows of a 2D array that contain masked values.
This function is a shortcut to ``mask_rowcols`` with `axis` equal to 0.
See Also
--------
mask_rowcols : Mask rows and/or columns of a 2D array.
masked_where : Mask where a condition is met.
Examples
--------
>>> import numpy.ma as ma
>>> a = np.zeros((3, 3), dtype=np.int)
>>> a[1, 1] = 1
>>> a
array([[0, 0, 0],
[0, 1, 0],
[0, 0, 0]])
>>> a = ma.masked_equal(a, 1)
>>> a
masked_array(data =
[[0 0 0]
[0 -- 0]
[0 0 0]],
mask =
[[False False False]
[False True False]
[False False False]],
fill_value=999999)
>>> ma.mask_rows(a)
masked_array(data =
[[0 0 0]
[-- -- --]
[0 0 0]],
mask =
[[False False False]
[ True True True]
[False False False]],
fill_value=999999)
- masked_all(shape, dtype=<type 'float'>)
- Empty masked array with all elements masked.
Return an empty masked array of the given shape and dtype, where all the
data are masked.
Parameters
----------
shape : tuple
Shape of the required MaskedArray.
dtype : dtype, optional
Data type of the output.
Returns
-------
a : MaskedArray
A masked array with all data masked.
See Also
--------
masked_all_like : Empty masked array modelled on an existing array.
Examples
--------
>>> import numpy.ma as ma
>>> ma.masked_all((3, 3))
masked_array(data =
[[-- -- --]
[-- -- --]
[-- -- --]],
mask =
[[ True True True]
[ True True True]
[ True True True]],
fill_value=1e+20)
The `dtype` parameter defines the underlying data type.
>>> a = ma.masked_all((3, 3))
>>> a.dtype
dtype('float64')
>>> a = ma.masked_all((3, 3), dtype=np.int32)
>>> a.dtype
dtype('int32')
- masked_all_like(arr)
- Empty masked array with the properties of an existing array.
Return an empty masked array of the same shape and dtype as
the array `arr`, where all the data are masked.
Parameters
----------
arr : ndarray
An array describing the shape and dtype of the required MaskedArray.
Returns
-------
a : MaskedArray
A masked array with all data masked.
Raises
------
AttributeError
If `arr` doesn't have a shape attribute (i.e. not an ndarray)
See Also
--------
masked_all : Empty masked array with all elements masked.
Examples
--------
>>> import numpy.ma as ma
>>> arr = np.zeros((2, 3), dtype=np.float32)
>>> arr
array([[ 0., 0., 0.],
[ 0., 0., 0.]], dtype=float32)
>>> ma.masked_all_like(arr)
masked_array(data =
[[-- -- --]
[-- -- --]],
mask =
[[ True True True]
[ True True True]],
fill_value=1e+20)
The dtype of the masked array matches the dtype of `arr`.
>>> arr.dtype
dtype('float32')
>>> ma.masked_all_like(arr).dtype
dtype('float32')
- median(a, axis=None, out=None, overwrite_input=False)
- Compute the median along the specified axis.
Returns the median of the array elements.
Parameters
----------
a : array_like
Input array or object that can be converted to an array
axis : int, optional
Axis along which the medians are computed. The default (axis=None) is
to compute the median along a flattened version of the array.
out : ndarray, optional
Alternative output array in which to place the result. It must
have the same shape and buffer length as the expected output
but the type will be cast if necessary.
overwrite_input : {False, True}, optional
If True, then allow use of memory of input array (a) for
calculations. The input array will be modified by the call to
median. This will save memory when you do not need to preserve
the contents of the input array. Treat the input as undefined,
but it will probably be fully or partially sorted. Default is
False. Note that, if overwrite_input is true, and the input
is not already an ndarray, an error will be raised.
Returns
-------
median : ndarray.
A new array holding the result is returned unless out is
specified, in which case a reference to out is returned.
Return datatype is float64 for ints and floats smaller than
float64, or the input datatype otherwise.
See Also
--------
mean
Notes
-----
Given a vector V with N non masked values, the median of V is the middle
value of a sorted copy of V (Vs) - i.e. Vs[(N-1)/2], when N is odd, or
{Vs[N/2 - 1] + Vs[N/2]}/2. when N is even.
- notmasked_contiguous(a, axis=None)
- Find contiguous unmasked data in a masked array along the given axis.
Parameters
----------
axis : int, optional
Axis along which to perform the operation.
If None, applies to a flattened version of the array.
Returns
-------
A sorted sequence of slices (start index, end index).
Notes
-----
Only accepts 2D arrays at most.
- notmasked_edges(a, axis=None)
- Find the indices of the first and last not masked values along
the given axis in a masked array.
If all values are masked, return None. Otherwise, return a list
of 2 tuples, corresponding to the indices of the first and last
unmasked values respectively.
Parameters
----------
axis : int, optional
Axis along which to perform the operation.
If None, applies to a flattened version of the array.
- nxarray = array(...)
- array(object, dtype=None, copy=True, order=None, subok=False, ndmin=True)
Create an array.
Parameters
----------
object : array_like
An array, any object exposing the array interface, an
object whose __array__ method returns an array, or any
(nested) sequence.
dtype : data-type, optional
The desired data-type for the array. If not given, then
the type will be determined as the minimum type required
to hold the objects in the sequence. This argument can only
be used to 'upcast' the array. For downcasting, use the
.astype(t) method.
copy : bool, optional
If true (default), then the object is copied. Otherwise, a copy
will only be made if __array__ returns a copy, if obj is a
nested sequence, or if a copy is needed to satisfy any of the other
requirements (`dtype`, `order`, etc.).
order : {'C', 'F', 'A'}, optional
Specify the order of the array. If order is 'C' (default), then the
array will be in C-contiguous order (last-index varies the
fastest). If order is 'F', then the returned array
will be in Fortran-contiguous order (first-index varies the
fastest). If order is 'A', then the returned array may
be in any order (either C-, Fortran-contiguous, or even
discontiguous).
subok : bool, optional
If True, then sub-classes will be passed-through, otherwise
the returned array will be forced to be a base-class array (default).
ndmin : int, optional
Specifies the minimum number of dimensions that the resulting
array should have. Ones will be pre-pended to the shape as
needed to meet this requirement.
Examples
--------
>>> np.array([1, 2, 3])
array([1, 2, 3])
Upcasting:
>>> np.array([1, 2, 3.0])
array([ 1., 2., 3.])
More than one dimension:
>>> np.array([[1, 2], [3, 4]])
array([[1, 2],
[3, 4]])
Minimum dimensions 2:
>>> np.array([1, 2, 3], ndmin=2)
array([[1, 2, 3]])
Type provided:
>>> np.array([1, 2, 3], dtype=complex)
array([ 1.+0.j, 2.+0.j, 3.+0.j])
Data-type consisting of more than one element:
>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
>>> x['a']
array([1, 3])
Creating an array from sub-classes:
>>> np.array(np.mat('1 2; 3 4'))
array([[1, 2],
[3, 4]])
>>> np.array(np.mat('1 2; 3 4'), subok=True)
matrix([[1, 2],
[3, 4]])
- polyfit(x, y, deg, rcond=None, full=False)
- Least squares polynomial fit.
Fit a polynomial ``p(x) = p[0] * x**deg + ... + p[deg]`` of degree `deg`
to points `(x, y)`. Returns a vector of coefficients `p` that minimises
the squared error.
Parameters
----------
x : array_like, shape (M,)
x-coordinates of the M sample points ``(x[i], y[i])``.
y : array_like, shape (M,) or (M, K)
y-coordinates of the sample points. Several data sets of sample
points sharing the same x-coordinates can be fitted at once by
passing in a 2D-array that contains one dataset per column.
deg : int
Degree of the fitting polynomial
rcond : float, optional
Relative condition number of the fit. Singular values smaller than this
relative to the largest singular value will be ignored. The default
value is len(x)*eps, where eps is the relative precision of the float
type, about 2e-16 in most cases.
full : bool, optional
Switch determining nature of return value. When it is
False (the default) just the coefficients are returned, when True
diagnostic information from the singular value decomposition is also
returned.
Returns
-------
p : ndarray, shape (M,) or (M, K)
Polynomial coefficients, highest power first.
If `y` was 2-D, the coefficients for `k`-th data set are in ``p[:,k]``.
residuals, rank, singular_values, rcond : present only if `full` = True
Residuals of the least-squares fit, the effective rank of the scaled
Vandermonde coefficient matrix, its singular values, and the specified
value of `rcond`. For more details, see `linalg.lstsq`.
Warns
-----
RankWarning
The rank of the coefficient matrix in the least-squares fit is
deficient. The warning is only raised if `full` = False.
The warnings can be turned off by
>>> import warnings
>>> warnings.simplefilter('ignore', np.RankWarning)
See Also
--------
polyval : Computes polynomial values.
linalg.lstsq : Computes a least-squares fit.
scipy.interpolate.UnivariateSpline : Computes spline fits.
Notes
-----
The solution minimizes the squared error
.. math ::
E = \sum_{j=0}^k |p(x_j) - y_j|^2
in the equations::
x[0]**n * p[n] + ... + x[0] * p[1] + p[0] = y[0]
x[1]**n * p[n] + ... + x[1] * p[1] + p[0] = y[1]
...
x[k]**n * p[n] + ... + x[k] * p[1] + p[0] = y[k]
The coefficient matrix of the coefficients `p` is a Vandermonde matrix.
`polyfit` issues a `RankWarning` when the least-squares fit is badly
conditioned. This implies that the best fit is not well-defined due
to numerical error. The results may be improved by lowering the polynomial
degree or by replacing `x` by `x` - `x`.mean(). The `rcond` parameter
can also be set to a value smaller than its default, but the resulting
fit may be spurious: including contributions from the small singular
values can add numerical noise to the result.
Note that fitting polynomial coefficients is inherently badly conditioned
when the degree of the polynomial is large or the interval of sample points
is badly centered. The quality of the fit should always be checked in these
cases. When polynomial fits are not satisfactory, splines may be a good
alternative.
References
----------
.. [1] Wikipedia, "Curve fitting",
http://en.wikipedia.org/wiki/Curve_fitting
.. [2] Wikipedia, "Polynomial interpolation",
http://en.wikipedia.org/wiki/Polynomial_interpolation
Examples
--------
>>> x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
>>> y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
>>> z = np.polyfit(x, y, 3)
array([ 0.08703704, -0.81349206, 1.69312169, -0.03968254])
It is convenient to use `poly1d` objects for dealing with polynomials:
>>> p = np.poly1d(z)
>>> p(0.5)
0.6143849206349179
>>> p(3.5)
-0.34732142857143039
>>> p(10)
22.579365079365115
High-order polynomials may oscillate wildly:
>>> p30 = np.poly1d(np.polyfit(x, y, 30))
/... RankWarning: Polyfit may be poorly conditioned...
>>> p30(4)
-0.80000000000000204
>>> p30(5)
-0.99999999999999445
>>> p30(4.5)
-0.10547061179440398
Illustration:
>>> import matplotlib.pyplot as plt
>>> xp = np.linspace(-2, 6, 100)
>>> plt.plot(x, y, '.', xp, p(xp), '-', xp, p30(xp), '--')
>>> plt.ylim(-2,2)
>>> plt.show()
Notes
-----
Any masked values in x is propagated in y, and vice-versa.
- setdiff1d(ar1, ar2)
- Set difference of 1D arrays with unique elements.
See Also
--------
numpy.setdiff1d : equivalent function for ndarrays
- setmember1d(ar1, ar2)
- Return a boolean array set True where first element is in second array.
See Also
--------
numpy.setmember1d : equivalent function for ndarrays.
- setxor1d(ar1, ar2)
- Set exclusive-or of 1D arrays with unique elements.
See Also
--------
numpy.setxor1d : equivalent function for ndarrays
- union1d(ar1, ar2)
- Union of 1D arrays with unique elements.
See also
--------
numpy.union1d : equivalent function for ndarrays.
- unique1d(ar1, return_index=False, return_inverse=False)
- Finds the unique elements of an array.
Masked values are considered the same element (masked).
The output array is always a MaskedArray.
See Also
--------
np.unique1d : equivalent function for ndarrays.
- vander(x, n=None)
- Generate a Van der Monde matrix.
The columns of the output matrix are decreasing powers of the input
vector. Specifically, the i-th output column is the input vector to
the power of ``N - i - 1``.
Parameters
----------
x : array_like
Input array.
N : int, optional
Order of (number of columns in) the output.
Returns
-------
out : ndarray
Van der Monde matrix of order `N`. The first column is ``x^(N-1)``,
the second ``x^(N-2)`` and so forth.
Examples
--------
>>> x = np.array([1, 2, 3, 5])
>>> N = 3
>>> np.vander(x, N)
array([[ 1, 1, 1],
[ 4, 2, 1],
[ 9, 3, 1],
[25, 5, 1]])
>>> np.column_stack([x**(N-1-i) for i in range(N)])
array([[ 1, 1, 1],
[ 4, 2, 1],
[ 9, 3, 1],
[25, 5, 1]])
Notes
-----
Masked values in the input array result in rows of zeros.
|