Variable | array_function_dispatch |
Undocumented |
Variable | i1 |
Undocumented |
Variable | i2 |
Undocumented |
Variable | i4 |
Undocumented |
Function | _diag_dispatcher |
Undocumented |
Function | _eye_dispatcher |
Undocumented |
Function | _flip_dispatcher |
Undocumented |
Function | _histogram2d_dispatcher |
Undocumented |
Function | _min_int |
get small int that fits the range |
Function | _tri_dispatcher |
Undocumented |
Function | _trilu_dispatcher |
Undocumented |
Function | _trilu_indices_form_dispatcher |
Undocumented |
Function | _vander_dispatcher |
Undocumented |
Function | diag |
Extract a diagonal or construct a diagonal array. |
Function | diagflat |
Create a two-dimensional array with the flattened input as a diagonal. |
Function | eye |
Return a 2-D array with ones on the diagonal and zeros elsewhere. |
Function | fliplr |
Reverse the order of elements along axis 1 (left/right). |
Function | flipud |
Reverse the order of elements along axis 0 (up/down). |
Function | histogram2d |
Compute the bi-dimensional histogram of two data samples. |
Function | mask_indices |
Return the indices to access (n, n) arrays, given a masking function. |
Function | tri |
An array with ones at and below the given diagonal and zeros elsewhere. |
Function | tril |
Lower triangle of an array. |
Function | tril_indices |
Return the indices for the lower-triangle of an (n, m) array. |
Function | tril_indices_from |
Return the indices for the lower-triangle of arr. |
Function | triu |
Upper triangle of an array. |
Function | triu_indices |
Return the indices for the upper-triangle of an (n, m) array. |
Function | triu_indices_from |
Return the indices for the upper-triangle of arr. |
Function | vander |
Generate a Vandermonde matrix. |
Variable | _eye_with_like |
Undocumented |
Variable | _tri_with_like |
Undocumented |
Undocumented
Extract a diagonal or construct a diagonal array.
See the more detailed documentation for numpy.diagonal if you use this function to extract a diagonal and wish to write to the resulting array; whether it returns a copy or a view depends on what version of numpy you are using.
diagonal : Return specified diagonals. diagflat : Create a 2-D array with the flattened input as a diagonal. trace : Sum along diagonals. triu : Upper triangle of an array. tril : Lower triangle of an array.
>>> x = np.arange(9).reshape((3,3)) >>> x array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
>>> np.diag(x) array([0, 4, 8]) >>> np.diag(x, k=1) array([1, 5]) >>> np.diag(x, k=-1) array([3, 7])
>>> np.diag(np.diag(x)) array([[0, 0, 0], [0, 4, 0], [0, 0, 8]])
Create a two-dimensional array with the flattened input as a diagonal.
diag : MATLAB work-alike for 1-D and 2-D arrays. diagonal : Return specified diagonals. trace : Sum along diagonals.
>>> np.diagflat([[1,2], [3,4]]) array([[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]])
>>> np.diagflat([1,2], 1) array([[0, 1, 0], [0, 0, 2], [0, 0, 0]])
Return a 2-D array with ones on the diagonal and zeros elsewhere.
N
.Whether the output should be stored in row-major (C-style) or column-major (Fortran-style) order in memory.
${ARRAY_FUNCTION_LIKE}
New in version 1.20.0.
k
-th
diagonal, whose values are equal to one.identity : (almost) equivalent function diag : diagonal 2-D array from a 1-D array specified by the user.
>>> np.eye(2, dtype=int) array([[1, 0], [0, 1]]) >>> np.eye(3, k=1) array([[0., 1., 0.], [0., 0., 1.], [0., 0., 0.]])
Reverse the order of elements along axis 1 (left/right).
For a 2-D array, this flips the entries in each row in the left/right direction. Columns are preserved, but appear in a different order than before.
m
with the columns reversed. Since a view
is returned, this operation is O(1).flipud : Flip array in the up/down direction. flip : Flip array in one or more dimensions. rot90 : Rotate array counterclockwise.
Equivalent to m[:,::-1] or np.flip(m, axis=1). Requires the array to be at least 2-D.
>>> A = np.diag([1.,2.,3.]) >>> A array([[1., 0., 0.], [0., 2., 0.], [0., 0., 3.]]) >>> np.fliplr(A) array([[0., 0., 1.], [0., 2., 0.], [3., 0., 0.]])
>>> A = np.random.randn(2,3,5) >>> np.all(np.fliplr(A) == A[:,::-1,...]) True
Reverse the order of elements along axis 0 (up/down).
For a 2-D array, this flips the entries in each column in the up/down direction. Rows are preserved, but appear in a different order than before.
m
with the rows reversed. Since a view is
returned, this operation is O(1).fliplr : Flip array in the left/right direction. flip : Flip array in one or more dimensions. rot90 : Rotate array counterclockwise.
Equivalent to m[::-1, ...] or np.flip(m, axis=0). Requires the array to be at least 1-D.
>>> A = np.diag([1.0, 2, 3]) >>> A array([[1., 0., 0.], [0., 2., 0.], [0., 0., 3.]]) >>> np.flipud(A) array([[0., 0., 3.], [0., 2., 0.], [1., 0., 0.]])
>>> A = np.random.randn(2,3,5) >>> np.all(np.flipud(A) == A[::-1,...]) True
>>> np.flipud([1,2]) array([2, 1])
Compute the bi-dimensional histogram of two data samples.
The bin specification:
- If int, the number of bins for the two dimensions (nx=ny=bins).
- If array_like, the bin edges for the two dimensions (x_edges=y_edges=bins).
- If [int, int], the number of bins in each dimension (nx, ny = bins).
- If [array, array], the bin edges in each dimension (x_edges, y_edges = bins).
- A combination [int, array] or [array, int], where int is the number of bins and array is the bin edges.
bins
parameters):
[[xmin, xmax], [ymin, ymax]]. All values outside of this range
will be considered outliers and not tallied in the histogram.histogram
, density
should be preferred.normed
is True. If normed
is
False, the values of the returned histogram are equal to the sum of
the weights belonging to the samples falling into each bin.x
and y
. Values in x
are histogrammed along the first dimension and values in y
are
histogrammed along the second dimension.histogram : 1D histogram histogramdd : Multidimensional histogram
When normed
is True, then the returned histogram is the sample
density, defined such that the sum over bins of the product
bin_value * bin_area is 1.
Please note that the histogram does not follow the Cartesian convention
where x
values are on the abscissa and y
values on the ordinate
axis. Rather, x
is histogrammed along the first dimension of the
array (vertical), and y
along the second dimension of the array
(horizontal). This ensures compatibility with histogramdd
.
>>> from matplotlib.image import NonUniformImage >>> import matplotlib.pyplot as plt
Construct a 2-D histogram with variable bin width. First define the bin edges:
>>> xedges = [0, 1, 3, 5] >>> yedges = [0, 2, 3, 4, 6]
Next we create a histogram H with random bin content:
>>> x = np.random.normal(2, 1, 100) >>> y = np.random.normal(1, 1, 100) >>> H, xedges, yedges = np.histogram2d(x, y, bins=(xedges, yedges)) >>> # Histogram does not follow Cartesian convention (see Notes), >>> # therefore transpose H for visualization purposes. >>> H = H.T
imshow
can only display square bins:
>>> fig = plt.figure(figsize=(7, 3)) >>> ax = fig.add_subplot(131, title='imshow: square bins') >>> plt.imshow(H, interpolation='nearest', origin='lower', ... extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]]) <matplotlib.image.AxesImage object at 0x...>
pcolormesh
can display actual edges:
>>> ax = fig.add_subplot(132, title='pcolormesh: actual edges', ... aspect='equal') >>> X, Y = np.meshgrid(xedges, yedges) >>> ax.pcolormesh(X, Y, H) <matplotlib.collections.QuadMesh object at 0x...>
NonUniformImage
can be used to
display actual bin edges with interpolation:
>>> ax = fig.add_subplot(133, title='NonUniformImage: interpolated', ... aspect='equal', xlim=xedges[[0, -1]], ylim=yedges[[0, -1]]) >>> im = NonUniformImage(ax, interpolation='bilinear') >>> xcenters = (xedges[:-1] + xedges[1:]) / 2 >>> ycenters = (yedges[:-1] + yedges[1:]) / 2 >>> im.set_data(xcenters, ycenters, H) >>> ax.images.append(im) >>> plt.show()
It is also possible to construct a 2-D histogram without specifying bin edges:
>>> # Generate non-symmetric test data >>> n = 10000 >>> x = np.linspace(1, 100, n) >>> y = 2*np.log(x) + np.random.rand(n) - 0.5 >>> # Compute 2d histogram. Note the order of x/y and xedges/yedges >>> H, yedges, xedges = np.histogram2d(y, x, bins=20)
Now we can plot the histogram using
pcolormesh
, and a
hexbin
for comparison.
>>> # Plot histogram using pcolormesh >>> fig, (ax1, ax2) = plt.subplots(ncols=2, sharey=True) >>> ax1.pcolormesh(xedges, yedges, H, cmap='rainbow') >>> ax1.plot(x, 2*np.log(x), 'k-') >>> ax1.set_xlim(x.min(), x.max()) >>> ax1.set_ylim(y.min(), y.max()) >>> ax1.set_xlabel('x') >>> ax1.set_ylabel('y') >>> ax1.set_title('histogram2d') >>> ax1.grid()
>>> # Create hexbin plot for comparison >>> ax2.hexbin(x, y, gridsize=20, cmap='rainbow') >>> ax2.plot(x, 2*np.log(x), 'k-') >>> ax2.set_title('hexbin') >>> ax2.set_xlim(x.min(), x.max()) >>> ax2.set_xlabel('x') >>> ax2.grid()
>>> plt.show()
Return the indices to access (n, n) arrays, given a masking function.
Assume mask_func
is a function that, for a square array a of size
(n, n) with a possible offset argument k
, when called as
mask_func(a, k) returns a new array with zeros in certain locations
(functions like triu
or tril
do precisely this). Then this function
returns the indices where the non-zero values would be located.
triu
, tril
.
That is, mask_func(x, k) returns a boolean array, shaped like x
.
k
is an optional argument to the function.mask_func
. Functions
like triu
, tril
take a second argument that is interpreted as an
offset.n
arrays of indices corresponding to the locations where
mask_func(np.ones((n, n)), k) is True.triu, tril, triu_indices, tril_indices
These are the indices that would allow you to access the upper triangular part of any 3x3 array:
>>> iu = np.mask_indices(3, np.triu)
For example, if a
is a 3x3 array:
>>> a = np.arange(9).reshape(3, 3) >>> a array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> a[iu] array([0, 1, 2, 4, 5, 8])
An offset can be passed also to the masking function. This gets us the indices starting on the first diagonal right of the main one:
>>> iu1 = np.mask_indices(3, np.triu, 1)
with which we now extract only three elements:
>>> a[iu1] array([1, 2, 5])
An array with ones at and below the given diagonal and zeros elsewhere.
M
is taken equal to N
.k
= 0 is the main diagonal, while k
< 0 is below it,
and k
> 0 is above. The default is 0.${ARRAY_FUNCTION_LIKE}
New in version 1.20.0.
>>> np.tri(3, 5, 2, dtype=int) array([[1, 1, 1, 0, 0], [1, 1, 1, 1, 0], [1, 1, 1, 1, 1]])
>>> np.tri(3, 5, -1) array([[0., 0., 0., 0., 0.], [1., 0., 0., 0., 0.], [1., 1., 0., 0., 0.]])
Lower triangle of an array.
Return a copy of an array with elements above the k
-th diagonal zeroed.
For arrays with ndim exceeding 2, tril
will apply to the final two
axes.
k = 0
(the default) is the
main diagonal, k < 0
is below it and k > 0
is above.triu : same thing, only for the upper triangle
>>> np.tril([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], -1) array([[ 0, 0, 0], [ 4, 0, 0], [ 7, 8, 0], [10, 11, 12]])
>>> np.tril(np.arange(3*4*5).reshape(3, 4, 5)) array([[[ 0, 0, 0, 0, 0], [ 5, 6, 0, 0, 0], [10, 11, 12, 0, 0], [15, 16, 17, 18, 0]], [[20, 0, 0, 0, 0], [25, 26, 0, 0, 0], [30, 31, 32, 0, 0], [35, 36, 37, 38, 0]], [[40, 0, 0, 0, 0], [45, 46, 0, 0, 0], [50, 51, 52, 0, 0], [55, 56, 57, 58, 0]]])
Return the indices for the lower-triangle of an (n, m) array.
triu_indices : similar function, for upper-triangular. mask_indices : generic function accepting an arbitrary mask function. tril, triu
Compute two different sets of indices to access 4x4 arrays, one for the lower triangular part starting at the main diagonal, and one starting two diagonals further right:
>>> il1 = np.tril_indices(4) >>> il2 = np.tril_indices(4, 2)
Here is how they can be used with a sample array:
>>> a = np.arange(16).reshape(4, 4) >>> a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]])
Both for indexing:
>>> a[il1] array([ 0, 4, 5, ..., 13, 14, 15])
And for assigning values:
>>> a[il1] = -1 >>> a array([[-1, 1, 2, 3], [-1, -1, 6, 7], [-1, -1, -1, 11], [-1, -1, -1, -1]])
These cover almost the whole array (two diagonals right of the main one):
>>> a[il2] = -10 >>> a array([[-10, -10, -10, 3], [-10, -10, -10, -10], [-10, -10, -10, -10], [-10, -10, -10, -10]])
Return the indices for the lower-triangle of arr.
See tril_indices
for full details.
tril
for details).tril_indices, tril
Upper triangle of an array.
Return a copy of an array with the elements below the k
-th diagonal
zeroed. For arrays with ndim exceeding 2, triu
will apply to the final
two axes.
Please refer to the documentation for tril
for further details.
tril : lower triangle of an array
>>> np.triu([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], -1) array([[ 1, 2, 3], [ 4, 5, 6], [ 0, 8, 9], [ 0, 0, 12]])
>>> np.triu(np.arange(3*4*5).reshape(3, 4, 5)) array([[[ 0, 1, 2, 3, 4], [ 0, 6, 7, 8, 9], [ 0, 0, 12, 13, 14], [ 0, 0, 0, 18, 19]], [[20, 21, 22, 23, 24], [ 0, 26, 27, 28, 29], [ 0, 0, 32, 33, 34], [ 0, 0, 0, 38, 39]], [[40, 41, 42, 43, 44], [ 0, 46, 47, 48, 49], [ 0, 0, 52, 53, 54], [ 0, 0, 0, 58, 59]]])
Return the indices for the upper-triangle of an (n, m) array.
n
)n
, n
).tril_indices : similar function, for lower-triangular. mask_indices : generic function accepting an arbitrary mask function. triu, tril
Compute two different sets of indices to access 4x4 arrays, one for the upper triangular part starting at the main diagonal, and one starting two diagonals further right:
>>> iu1 = np.triu_indices(4) >>> iu2 = np.triu_indices(4, 2)
Here is how they can be used with a sample array:
>>> a = np.arange(16).reshape(4, 4) >>> a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]])
Both for indexing:
>>> a[iu1] array([ 0, 1, 2, ..., 10, 11, 15])
And for assigning values:
>>> a[iu1] = -1 >>> a array([[-1, -1, -1, -1], [ 4, -1, -1, -1], [ 8, 9, -1, -1], [12, 13, 14, -1]])
These cover only a small part of the whole array (two diagonals right of the main one):
>>> a[iu2] = -10 >>> a array([[ -1, -1, -10, -10], [ 4, -1, -1, -10], [ 8, 9, -1, -1], [ 12, 13, 14, -1]])
Return the indices for the upper-triangle of arr.
See triu_indices
for full details.
triu
for details).arr
.triu_indices, triu
Generate a Vandermonde matrix.
The columns of the output matrix are powers of the input vector. The
order of the powers is determined by the increasing
boolean argument.
Specifically, when increasing
is False, the i
-th output column is
the input vector raised element-wise to the power of N - i - 1. Such
a matrix with a geometric progression in each row is named for Alexandre-
Theophile Vandermonde.
N
is not specified, a square
array is returned (N = len(x)).Order of the powers of the columns. If True, the powers increase from left to right, if False (the default) they are reversed.
increasing
is False, the first column is
x^(N-1), the second x^(N-2) and so forth. If increasing
is
True, the columns are x^0, x^1, ..., x^(N-1).polynomial.polynomial.polyvander
>>> 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]])
>>> x = np.array([1, 2, 3, 5]) >>> np.vander(x) array([[ 1, 1, 1, 1], [ 8, 4, 2, 1], [ 27, 9, 3, 1], [125, 25, 5, 1]]) >>> np.vander(x, increasing=True) array([[ 1, 1, 1, 1], [ 1, 2, 4, 8], [ 1, 3, 9, 27], [ 1, 5, 25, 125]])
The determinant of a square Vandermonde matrix is the product of the differences between the values of the input vector:
>>> np.linalg.det(np.vander(x)) 48.000000000000043 # may vary >>> (5-3)*(5-2)*(5-1)*(3-2)*(3-1)*(2-1) 48