class matrix(N.ndarray):
matrix(data, dtype=None, copy=True)
Note
It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.
Returns a matrix from an array-like object, or from a string of data. A matrix is a specialized 2-D array that retains its 2-D nature through operations. It has certain special operators, such as * (matrix multiplication) and ** (matrix power).
data
is a string, it is interpreted as a matrix with commas
or spaces separating columns, and semicolons separating rows.data
is already an ndarray
, then this flag determines
whether the data is copied (the default), or whether a view is
constructed.array
>>> a = np.matrix('1 2; 3 4') >>> a matrix([[1, 2], [3, 4]])
>>> np.matrix([[1, 2], [3, 4]]) matrix([[1, 2], [3, 4]])
Method | __array_finalize__ |
Undocumented |
Method | __getitem__ |
Undocumented |
Method | __imul__ |
Undocumented |
Method | __ipow__ |
Undocumented |
Method | __mul__ |
Undocumented |
Method | __new__ |
Undocumented |
Method | __pow__ |
Undocumented |
Method | __rmul__ |
Undocumented |
Method | __rpow__ |
Undocumented |
Method | _align |
A convenience function for operations that need to preserve axis orientation. |
Method | _collapse |
A convenience function for operations that want to collapse to a scalar like _align, but are using keepdims=True |
Method | all |
Test whether all matrix elements along a given axis evaluate to True. |
Method | any |
Test whether any array element along a given axis evaluates to True. |
Method | argmax |
Indexes of the maximum values along an axis. |
Method | argmin |
Indexes of the minimum values along an axis. |
Method | flatten |
Return a flattened copy of the matrix. |
Method | max |
Return the maximum value along an axis. |
Method | mean |
Returns the average of the matrix elements along the given axis. |
Method | min |
Return the minimum value along an axis. |
Method | prod |
Return the product of the array elements over the given axis. |
Method | ptp |
Peak-to-peak (maximum - minimum) value along the given axis. |
Method | ravel |
Return a flattened matrix. |
Method | squeeze |
Return a possibly reshaped matrix. |
Method | std |
Return the standard deviation of the array elements along the given axis. |
Method | sum |
Returns the sum of the matrix elements, along the given axis. |
Method | tolist |
Return the matrix as a (possibly nested) list. |
Method | var |
Returns the variance of the matrix elements, along the given axis. |
Class Variable | __array_priority__ |
Undocumented |
Instance Variable | _getitem |
Undocumented |
Instance Variable | shape |
Undocumented |
Property | A |
Return self as an ndarray object. |
Property | A1 |
Return self as a flattened ndarray . |
Property | H |
Returns the (complex) conjugate transpose of self . |
Property | I |
Returns the (multiplicative) inverse of invertible self . |
Property | T |
Returns the transpose of the matrix. |
Test whether all matrix elements along a given axis evaluate to True.
See numpy.all
for complete descriptions
numpy.all
This is the same as ndarray.all
, but it returns a matrix
object.
>>> x = np.matrix(np.arange(12).reshape((3,4))); x matrix([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> y = x[0]; y matrix([[0, 1, 2, 3]]) >>> (x == y) matrix([[ True, True, True, True], [False, False, False, False], [False, False, False, False]]) >>> (x == y).all() False >>> (x == y).all(0) matrix([[False, False, False, False]]) >>> (x == y).all(1) matrix([[ True], [False], [False]])
Test whether any array element along a given axis evaluates to True.
Refer to numpy.any
for full documentation.
- any : bool, ndarray
- Returns a single bool if
axis
is None; otherwise, returnsndarray
Indexes of the maximum values along an axis.
Return the indexes of the first occurrences of the maximum values along the specified axis. If axis is None, the index is for the flattened matrix.
See numpy.argmax
for complete descriptions
numpy.argmax
This is the same as ndarray.argmax
, but returns a matrix
object
where ndarray.argmax
would return an ndarray
.
>>> x = np.matrix(np.arange(12).reshape((3,4))); x matrix([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> x.argmax() 11 >>> x.argmax(0) matrix([[2, 2, 2, 2]]) >>> x.argmax(1) matrix([[3], [3], [3]])
Indexes of the minimum values along an axis.
Return the indexes of the first occurrences of the minimum values along the specified axis. If axis is None, the index is for the flattened matrix.
See numpy.argmin
for complete descriptions.
numpy.argmin
This is the same as ndarray.argmin
, but returns a matrix
object
where ndarray.argmin
would return an ndarray
.
>>> x = -np.matrix(np.arange(12).reshape((3,4))); x matrix([[ 0, -1, -2, -3], [ -4, -5, -6, -7], [ -8, -9, -10, -11]]) >>> x.argmin() 11 >>> x.argmin(0) matrix([[2, 2, 2, 2]]) >>> x.argmin(1) matrix([[3], [3], [3]])
Return a flattened copy of the matrix.
All N
elements of the matrix are placed into a single row.
m
is Fortran contiguous in
memory, row-major order otherwise. 'K' means to flatten m
in
the order the elements occur in memory. The default is 'C'.(1, N)
matrix where N
is the number of elements in the original matrix.ravel : Return a flattened array. flat : A 1-D flat iterator over the matrix.
>>> m = np.matrix([[1,2], [3,4]]) >>> m.flatten() matrix([[1, 2, 3, 4]]) >>> m.flatten('F') matrix([[1, 3, 2, 4]])
Return the maximum value along an axis.
See amax
for complete descriptions
amax, ndarray.max
This is the same as ndarray.max
, but returns a matrix
object
where ndarray.max
would return an ndarray.
>>> x = np.matrix(np.arange(12).reshape((3,4))); x matrix([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> x.max() 11 >>> x.max(0) matrix([[ 8, 9, 10, 11]]) >>> x.max(1) matrix([[ 3], [ 7], [11]])
Returns the average of the matrix elements along the given axis.
Refer to numpy.mean
for full documentation.
numpy.mean
Same as ndarray.mean
except that, where that returns an ndarray
,
this returns a matrix
object.
>>> x = np.matrix(np.arange(12).reshape((3, 4))) >>> x matrix([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> x.mean() 5.5 >>> x.mean(0) matrix([[4., 5., 6., 7.]]) >>> x.mean(1) matrix([[ 1.5], [ 5.5], [ 9.5]])
Return the minimum value along an axis.
See amin
for complete descriptions.
amin, ndarray.min
This is the same as ndarray.min
, but returns a matrix
object
where ndarray.min
would return an ndarray.
>>> x = -np.matrix(np.arange(12).reshape((3,4))); x matrix([[ 0, -1, -2, -3], [ -4, -5, -6, -7], [ -8, -9, -10, -11]]) >>> x.min() -11 >>> x.min(0) matrix([[ -8, -9, -10, -11]]) >>> x.min(1) matrix([[ -3], [ -7], [-11]])
Return the product of the array elements over the given axis.
Refer to prod
for full documentation.
prod, ndarray.prod
Same as ndarray.prod
, except, where that returns an ndarray
, this
returns a matrix
object instead.
>>> x = np.matrix(np.arange(12).reshape((3,4))); x matrix([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> x.prod() 0 >>> x.prod(0) matrix([[ 0, 45, 120, 231]]) >>> x.prod(1) matrix([[ 0], [ 840], [7920]])
Peak-to-peak (maximum - minimum) value along the given axis.
Refer to numpy.ptp
for full documentation.
numpy.ptp
Same as ndarray.ptp
, except, where that would return an ndarray
object,
this returns a matrix
object.
>>> x = np.matrix(np.arange(12).reshape((3,4))); x matrix([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> x.ptp() 11 >>> x.ptp(0) matrix([[8, 8, 8, 8]]) >>> x.ptp(1) matrix([[3], [3], [3]])
Return a flattened matrix.
Refer to numpy.ravel
for more documentation.
m
are read using this index order. 'C' means to
index the elements in C-like order, with the last axis index
changing fastest, back to the first axis index changing slowest.
'F' means to index the elements in Fortran-like index order, with
the first index changing fastest, and the last index changing
slowest. Note that the 'C' and 'F' options take no account of the
memory layout of the underlying array, and only refer to the order
of axis indexing. 'A' means to read the elements in Fortran-like
index order if m
is Fortran contiguous in memory, C-like order
otherwise. 'K' means to read the elements in the order they occur
in memory, except for reversing the data when strides are negative.
By default, 'C' index order is used.(1, N)
where N
is the number of elements in the original matrix.
A copy is made only if necessary.matrix.flatten : returns a similar output matrix but always a copy matrix.flat : a flat iterator on the array. numpy.ravel : related function which returns an ndarray
Return a possibly reshaped matrix.
Refer to numpy.squeeze
for more documentation.
numpy.squeeze : related function
If m
has a single column then that column is returned
as the single row of a matrix. Otherwise m
is returned.
The returned matrix is always either m
itself or a view into m
.
Supplying an axis keyword argument will not affect the returned matrix
but it may cause an error to be raised.
>>> c = np.matrix([[1], [2]]) >>> c matrix([[1], [2]]) >>> c.squeeze() matrix([[1, 2]]) >>> r = c.T >>> r matrix([[1, 2]]) >>> r.squeeze() matrix([[1, 2]]) >>> m = np.matrix([[1, 2], [3, 4]]) >>> m.squeeze() matrix([[1, 2], [3, 4]])
Return the standard deviation of the array elements along the given axis.
Refer to numpy.std
for full documentation.
numpy.std
This is the same as ndarray.std
, except that where an ndarray
would
be returned, a matrix
object is returned instead.
>>> x = np.matrix(np.arange(12).reshape((3, 4))) >>> x matrix([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> x.std() 3.4520525295346629 # may vary >>> x.std(0) matrix([[ 3.26598632, 3.26598632, 3.26598632, 3.26598632]]) # may vary >>> x.std(1) matrix([[ 1.11803399], [ 1.11803399], [ 1.11803399]])
Returns the sum of the matrix elements, along the given axis.
Refer to numpy.sum
for full documentation.
numpy.sum
This is the same as ndarray.sum
, except that where an ndarray
would
be returned, a matrix
object is returned instead.
>>> x = np.matrix([[1, 2], [4, 3]]) >>> x.sum() 10 >>> x.sum(axis=1) matrix([[3], [7]]) >>> x.sum(axis=1, dtype='float') matrix([[3.], [7.]]) >>> out = np.zeros((2, 1), dtype='float') >>> x.sum(axis=1, dtype='float', out=np.asmatrix(out)) matrix([[3.], [7.]])
Return the matrix as a (possibly nested) list.
See ndarray.tolist
for full documentation.
ndarray.tolist
>>> x = np.matrix(np.arange(12).reshape((3,4))); x matrix([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> x.tolist() [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
Returns the variance of the matrix elements, along the given axis.
Refer to numpy.var
for full documentation.
numpy.var
This is the same as ndarray.var
, except that where an ndarray
would
be returned, a matrix
object is returned instead.
>>> x = np.matrix(np.arange(12).reshape((3, 4))) >>> x matrix([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> x.var() 11.916666666666666 >>> x.var(0) matrix([[ 10.66666667, 10.66666667, 10.66666667, 10.66666667]]) # may vary >>> x.var(1) matrix([[1.25], [1.25], [1.25]])
Return self
as an ndarray
object.
Equivalent to np.asarray(self).
None
self
as an ndarray
>>> x = np.matrix(np.arange(12).reshape((3,4))); x matrix([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> x.getA() array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
Return self
as a flattened ndarray
.
Equivalent to np.asarray(x).ravel()
None
self
, 1-D, as an ndarray
>>> x = np.matrix(np.arange(12).reshape((3,4))); x matrix([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> x.getA1() array([ 0, 1, 2, ..., 9, 10, 11])
Returns the (complex) conjugate transpose of self
.
Equivalent to np.transpose(self) if self
is real-valued.
None
self
>>> x = np.matrix(np.arange(12).reshape((3,4))) >>> z = x - 1j*x; z matrix([[ 0. +0.j, 1. -1.j, 2. -2.j, 3. -3.j], [ 4. -4.j, 5. -5.j, 6. -6.j, 7. -7.j], [ 8. -8.j, 9. -9.j, 10.-10.j, 11.-11.j]]) >>> z.getH() matrix([[ 0. -0.j, 4. +4.j, 8. +8.j], [ 1. +1.j, 5. +5.j, 9. +9.j], [ 2. +2.j, 6. +6.j, 10.+10.j], [ 3. +3.j, 7. +7.j, 11.+11.j]])
Returns the (multiplicative) inverse of invertible self
.
None
self
is non-singular, ret
is such that ret * self ==
self * ret == np.matrix(np.eye(self[0,:].size)) all return
True.self
is singular.linalg.inv
>>> m = np.matrix('[1, 2; 3, 4]'); m matrix([[1, 2], [3, 4]]) >>> m.getI() matrix([[-2. , 1. ], [ 1.5, -0.5]]) >>> m.getI() * m matrix([[ 1., 0.], # may vary [ 0., 1.]])
Returns the transpose of the matrix.
Does not conjugate! For the complex conjugate transpose, use .H.
None
transpose, getH
>>> m = np.matrix('[1, 2; 3, 4]') >>> m matrix([[1, 2], [3, 4]]) >>> m.getT() matrix([[1, 3], [2, 4]])