12582
Comment:
|
29642
|
Deletions are marked like this. | Additions are marked like this. |
Line 2: | Line 2: |
{{{ #!html <style> xxx { background-color: red; } </style> }}} |
|
Line 28: | Line 21: |
{{{ apt-get install python-numpy python-scipy |
{{{ apt-get install python3-numpy python3-scipy |
Line 40: | Line 34: |
We distinguish between '''dense matrices''' and '''sparse matrices'''. Dense matrices store every entry in the matrix, while sparse matrices only store the non-zero entries (together with their row and column index). Dense matrices are more feature-rich, but may consume more memory space than sparse matrices (in particular if most of the entries in a matrix are zero). | {{{#!html We distinguish between <span style="font-weight: bold; background-color: #E5E5FF; border: 1pt solid #AEBDCC; padding: 2pt;">dense matrices</span> and <span style="font-weight: bold; background-color: #E5FFE5; border: 1pt solid #AEBDCC; padding: 2pt;">sparse matrices</span> (Note: The color code will be used conistently throughout this cheat sheet). }}} Dense matrices store every entry in the matrix, while sparse matrices only store the non-zero entries (together with their row and column index). Dense matrices are more feature-rich, but may consume more memory space than sparse matrices (in particular if most of the entries in a matrix are zero). |
Line 46: | Line 44: |
{{{ numpy.matrix(arg, dtype=None) |
{{{#!html <div style="background-color: #E5E5FF; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #7F7FFF; padding: 2pt 5pt; float: right;">Dense</span> <pre style="background-color: #E5E5FF; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">numpy.matrix(arg, dtype=None)</span> <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.matrix.html" class="https">Reference</a> |
Line 51: | Line 52: |
* a standard Python array; or * a string with columns separated by commas or spaces and rows separated by semicolons. |
(1) a standard Python array; or (2) a string with columns separated by commas or spaces and rows separated by semicolons. |
Line 56: | Line 57: |
---------- Examples: |
<span style="font-weight: bold;">Examples:</span> |
Line 66: | Line 67: |
}}} [[https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.matrix.html|numpy.matrix]] <<BR>><<BR>> |
</pre> </div> }}} |
Line 70: | Line 72: |
{{{ numpy.array(arg, dtype=None, ndmin=0) |
{{{#!html <div style="background-color: #E5E5FF; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #7F7FFF; padding: 2pt 5pt; float: right;">Dense</span> <pre style="background-color: #E5E5FF; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">numpy.array(arg, dtype=None, ndmin=0)</span> <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.array.html" class="https">Reference</a> |
Line 74: | Line 79: |
The data to construct the matrix from, given as * a standard array; or * a function that returns an array. |
The data to construct the matrix from, given as: (1) a standard array; or (2) a function that returns an array. |
Line 82: | Line 87: |
---------- Examples: |
<span style="font-weight: bold;">Examples:</span> |
Line 96: | Line 101: |
}}} [[https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.array.html|numpy.array]] <<BR>><<BR>> |
</pre> </div> }}} |
Line 106: | Line 112: |
{{{ scipy.sparse.csr_matrix(arg, shape=None, dtype=None) scipy.sparse.csc_matrix(arg, shape=None, dtype=None) |
{{{#!html <div style="background-color: #E5FFE5; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #009900; padding: 2pt 5pt; float: right;">Sparse</span> <pre style="background-color: #E5FFE5; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">scipy.sparse.csr_matrix(arg, shape=None, dtype=None)</span> <a href="https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.csr_matrix.html" class="https">Reference</a> <span style="font-weight: bold;">scipy.sparse.csc_matrix(arg, shape=None, dtype=None)</span> <a href="https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.csc_matrix.html" class="https">Reference</a> |
Line 122: | Line 131: |
---------- Examples: |
<span style="font-weight: bold;">Examples:</span> |
Line 139: | Line 148: |
[[1 1 0 0] | [[1 2 0 0] |
Line 148: | Line 157: |
[[1 1 0 0] | [[1 2 0 0] |
Line 152: | Line 161: |
}}} [[https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.csr_matrix.html|scipy.sparse.csr_matrix]]<<BR>> [[https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.csc_matrix.html|scipy.sparse.csc_matrix]]<<BR>><<BR>> |
</pre> </div> }}} |
Line 161: | Line 170: |
{{{ numpy.empty(shape, dtype=float) |
{{{#!html <div style="background-color: #E5E5FF; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #7F7FFF; padding: 2pt 5pt; float: right;">Dense</span> <pre style="background-color: #E5E5FF; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">numpy.empty(shape, dtype=float)</span> <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.empty.html" class="https">Reference</a> |
Line 169: | Line 181: |
---------- Examples: |
<span style="font-weight: bold;">Examples:</span> |
Line 179: | Line 191: |
}}} [[https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.empty.html|numpy.empty]] <<BR>><<BR>> |
</pre> </div> }}} |
Line 183: | Line 196: |
{{{ numpy.zeros(shape, dtype=float) |
{{{#!html <div style="background-color: #E5E5FF; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #7F7FFF; padding: 2pt 5pt; float: right;">Dense</span> <pre style="background-color: #E5E5FF; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">numpy.zeros(shape, dtype=float)</span> <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.zeros.html" class="https">Reference</a> |
Line 191: | Line 207: |
---------- Examples: |
<span style="font-weight: bold;">Examples</span> |
Line 201: | Line 217: |
}}} [[https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.zeros.html|numpy.zeros]] <<BR>><<BR>> |
</pre> </div> }}} |
Line 205: | Line 222: |
{{{ numpy.ones(shape, dtype=float) |
{{{#!html <div style="background-color: #E5E5FF; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #7F7FFF; padding: 2pt 5pt; float: right;">Dense</span> <pre style="background-color: #E5E5FF; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">numpy.ones(shape, dtype=float)</span> <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ones.html" class="https">Reference</a> |
Line 213: | Line 233: |
---------- Examples: |
<span style="font-weight: bold;">Examples:</span> |
Line 223: | Line 243: |
}}} [[https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ones.html|numpy.ones]] <<BR>><<BR>> |
</pre> </div> }}} |
Line 228: | Line 249: |
Dense: {{{ numpy.diag(arg, k=0) |
{{{#!html <div style="background-color: #E5E5FF; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #7F7FFF; padding: 2pt 5pt; float: right;">Dense</span> <pre style="background-color: #E5E5FF; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">numpy.diag(arg, k=0)</span> <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.diag.html" class="https">Reference</a> |
Line 237: | Line 260: |
---------- Examples: |
<span style="font-weight: bold;">Examples:</span> |
Line 256: | Line 279: |
}}} [[https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.diag.html|numpy.diag]] Sparse: {{{ scipy.sparse.diags(diagonals, offsets=0, dtype=None) |
</pre> </div> }}} {{{#!html <div style="background-color: #E5FFE5; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #009900; padding: 2pt 5pt; float: right;">Sparse</span> <pre style="background-color: #E5FFE5; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">scipy.sparse.diags(diagonals, offsets=0, dtype=None)</span> <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.diags.html" class="https">Reference</a> |
Line 270: | Line 296: |
---------- Examples: |
<span style="font-weight: bold;">Examples:</span> |
Line 282: | Line 308: |
}}} [[https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.diags.html|scipy.sparse.diags]] <<BR>><<BR>> |
</pre> </div> }}} |
Line 288: | Line 315: |
Dense: {{{ numpy.identity(n, dtype=float) |
{{{#!html <div style="background-color: #E5E5FF; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #7F7FFF; padding: 2pt 5pt; float: right;">Dense</span> <pre style="background-color: #E5E5FF; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">numpy.identity(n, dtype=float)</span> <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.identity.html" class="https">Reference</a> |
Line 297: | Line 326: |
---------- Examples: |
<span style="font-weight: bold;">Examples:</span> |
Line 309: | Line 338: |
}}} [[https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.identity.html|numpy.identity]] Sparse: {{{ scipy.sparse.identity(n, dtype=float, format="csr") |
</pre> </div> }}} {{{#!html <div style="background-color: #E5FFE5; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #009900; padding: 2pt 5pt; float: right;">Sparse</span> <pre style="background-color: #E5FFE5; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">scipy.sparse.identity(n, dtype=float, format="csr")</span> <a href="https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.identity.html" class="https">Reference</a> |
Line 323: | Line 355: |
---------- Examples: |
<span style="font-weight: bold;">Examples:</span> |
Line 335: | Line 367: |
}}} [[https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.identity.html|scipy.sparse.identity]]<<BR>><<BR>> |
</pre> </div> }}} |
Line 340: | Line 373: |
Dense: {{{ numpy.triu(arg, k=0) # Zero entries in the upper triangle of an array. numpy.tril(arg, k=0) # Zero entries in the lower triangle of an array. |
{{{#!html <div style="background-color: #E5E5FF; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #7F7FFF; padding: 2pt 5pt; float: right;">Dense</span> <pre style="background-color: #E5E5FF; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">numpy.triu(arg, k=0)</span> # Zero entries in the upper triangle of an array. <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.triu.html" class="https">Reference</a> <span style="font-weight: bold;">numpy.tril(arg, k=0)</span> # Zero entries in the lower triangle of an array. <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.tril.html" class="https">Reference</a> |
Line 350: | Line 385: |
---------- Examples: |
<span style="font-weight: bold;">Examples:</span> |
Line 372: | Line 407: |
}}} [[https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.triu.html|numpy.triu]] <<BR>> [[https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.tril.html|numpy.tril]] <<BR>><<BR>> Sparse: {{{ scipy.sparse.triu(arg, k=0, format="csr") # Zero entries in the upper triangle of an array. scipy.sparse.tril(arg, k=0, format="csr") # Zero entries in the lower triangle of an array. |
</pre> </div> }}} {{{#!html <div style="background-color: #E5FFE5; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #009900; padding: 2pt 5pt; float: right;">Sparse</span> <pre style="background-color: #E5FFE5; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">scipy.sparse.triu(arg, k=0, format="csr")</span> # Zero entries in the upper triangle of an array. <a href="https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.triu.html" class="https">Reference</a> <span style="font-weight: bold;">scipy.sparse.tril(arg, k=0, format="csr")</span> # Zero entries in the lower triangle of an array. <a href="https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.tril.html" class="https">Reference</a> |
Line 388: | Line 425: |
---------- Examples: |
<span style="font-weight: bold;">Examples:</span> |
Line 410: | Line 447: |
}}} [[https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.tril.html|scipy.sparse.triu]] <<BR>> [[https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.triu.html|scipy.sparse.tril]] <<BR>><<BR>> |
</pre> </div> }}} |
Line 424: | Line 461: |
TODO | === Adding a constant === The addition of a constant adds the constant to every element of a matrix (only available for dense matrices). {{{#!html <div style="background-color: #E5E5FF; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #7F7FFF; padding: 2pt 5pt; float: right;">Dense</span> <pre style="background-color: #E5E5FF; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">numpy.tril(arg, k=0)</span> # Zero entries in the lower triangle of an array. <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.tril.html" class="https">Reference</a> <span style="font-weight: bold;">A + c</span> A (matrix or array): The matrix/array. c (constant): The constant. <span style="font-weight: bold;">Examples:</span> >>> A = np.matrix([[2, 1], [3, 5]], dtype=float) >>> A + 4 [[6 5] [7 9]] </pre> </div> }}} === Multiplying by a constant === Multiplying by a constant multiplies every element of a matrix by that constant (both for sparse and dense matrices). {{{#!html <div style="background-color: #E5E5FF; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #7F7FFF; padding: 2pt 5pt; float: right;">Dense</span> <pre style="background-color: #E5E5FF; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">A * c</span> A (matrix or array): The matrix/array. c (constant): The constant. <span style="font-weight: bold;">Examples:</span> >>> A = np.matrix([[2, 1], [3, 5]], dtype=float) >>> A * 4 [[8 4] [12 20]] </pre> </div> }}} {{{#!html <div style="background-color: #E5FFE5; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <span style="background-color: #009900; padding: 2pt 5pt; float: right;">Sparse</span> <pre style="background-color: #E5FFE5; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">A * c</span> A (sparse matrix): The matrix. c (constant): The constant. <span style="font-weight: bold;">Examples:</span> >>> A = csr_matrix([[1, 0], [0, 1], [3, 2]], dtype=float) >>> A * 4 [[4 0] [0 4] [12 8]] # (transformed to a dense matrix for visualization). </pre> </div> }}} === Multiplying two matrices === There are two options on multiplying two matrices: the '''*''' operator and the '''dot()''' function. The behavior and result of both options differ depending on the type of the used matrices (resp. arrays): (1) The '''*''' operator computes * the '''normal''' matrix multiplication when '''sparse''' and/or '''dense matrices''' are used. * the '''element-wise''' matrix multiplication when '''dense arrays''' are used. (2) The '''dot()''' function computes * the normal matrix multiplication when a '''dense matrix''' is multiplied with a '''dense matrix''' or a '''sparse matrix''' is multiplied with a '''sparse matrix''' or a '''dense matrix'''; * bullshit when a '''dense matrix''' is multiplied with a '''sparse matrix'''. The result of a matrix multiplication between * a sparse matrix and a sparse matrix is a '''sparse matrix'''. * a sparse matrix and a dense matrix is a '''dense matrix'''. * a dense matrix and a dense matrix is a '''dense matrix'''. {{{#!html <div style="background-color: #EEEEEE; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <pre style="background-color: #EEEEEE; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">A * B</span> <span style="font-weight: bold;">A.dot(B)</span> <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.dot.html" class="https">Reference</a> A (dense matrix or sparse matrix): The first matrix/array. B (dense matrix or sparse matrix): The second matrix/array. <span style="font-weight: bold;">Examples:</span> >>> A = numpy.array([[1, 2], [3, 4]]) >>> M1_sparse = csr_matrix([[1, 0], [0, 1], [3, 2]], dtype=float) >>> M1_dense = A_sparse.todense() >>> M2_dense = numpy.matrix([[2, 1], [3, 4]], dtype=float) >>> M2_sparse = csr_matrix(B_dense) <span style="font-weight: bold;">## Dense array with dense array.</span> >>> A * A # <span style="font-weight: bold;">no normal</span>, but <span style="font-weight: bold;">element-wise</span> matrix multiplication. [[ 1 4] [ 9 16]] >>> A.dot(A) # normal matrix multiplication. [[ 7 10] [15 22]] <span style="font-weight: bold;">## Dense matrix with dense matrix.</span> >>> M1_dense * M2_dense # normal matrix multiplication. [[ 2., 1.], [ 3., 4.], [ 12., 11.]] # result is a <span style="font-weight: bold;">dense</span> matrix >>> M1_dense.dot(M2_dense) # same as M1_dense * M2_dense [[ 2., 1.], [ 3., 4.], [ 12., 11.]] # result is a <span style="font-weight: bold;">dense</span> matrix <span style="font-weight: bold;">## Sparse matrix with sparse matrix.</span> >>> X = M1_sparse * M2_sparse # normal matrix multiplication. >>> X.todense() [[ 2., 1.], [ 3., 4.], [ 12., 11.]] # result is a <span style="font-weight: bold;">sparse</span> matrix. >>> X = M1_sparse.dot(M2_sparse) # same as M1_sparse * M2_sparse >>> X.todense() [[ 2., 1.], [ 3., 4.], [ 12., 11.]] # result is a <span style="font-weight: bold;">sparse</span> matrix. <span style="font-weight: bold;">## Sparse matrix with dense matrix.</span> >>> M1_sparse * M2_dense # normal matrix multiplication. [[ 2., 1.], [ 3., 4.], [ 12., 11.]] # result is a <span style="font-weight: bold;">dense</span> matrix. >>> M1_sparse.dot(M2_dense) # same as M1_sparse * M2_dense [[ 2., 1.], [ 3., 4.], [ 12., 11.]] # result is a <span style="font-weight: bold;">dense</span> matrix. <span style="font-weight: bold;">## Dense matrix with sparse matrix.</span> >>> M1_dense * M2_sparse # normal matrix multiplication. [[ 2., 1.], [ 3., 4.], [ 12., 11.]] # result is a <span style="font-weight: bold;">dense</span> matrix. >>> M1_dense.dot(M2_sparse) # computes M1_dense.dot(np.array(M2_sparse)) matrix([[ <2x2 sparse matrix of type '<class 'numpy.float64'>' with 4 stored elements in Compressed Sparse Row format>, <2x2 sparse matrix of type '<class 'numpy.float64'>' with 4 stored elements in Compressed Sparse Row format>], [ <2x2 sparse matrix of type '<class 'numpy.float64'>' with 4 stored elements in Compressed Sparse Row format>, <2x2 sparse matrix of type '<class 'numpy.float64'>' with 4 stored elements in Compressed Sparse Row format>], [ <2x2 sparse matrix of type '<class 'numpy.float64'>' with 4 stored elements in Compressed Sparse Row format>, <2x2 sparse matrix of type '<class 'numpy.float64'>' with 4 stored elements in Compressed Sparse Row format>]], dtype=object) </pre> </div> }}} |
Line 430: | Line 644: |
TODO | === numpy.round() === Takes an array or matrix and rounds its values to the given number of decimals. Note that for values exactly halfway between rounded decimal values, numpy rounds to the nearest even value. {{{#!html <div style="background-color: #EEEEEE; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <pre style="background-color: #EEEEEE; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">numpy.round(A, decimals)</span> <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.round_.html" class="https">Reference</a> A (dense matrix or sparse matrix): The matrix/array. decimal (int, optional): Number of decimal places to round to (default: 0). <span style="font-weight: bold;">Examples:</span> >>> A_dense = np.array([[1.11, 2.22, 3.33], [4.44, 5.55, 6.66]]) >>> A_sparse = scipy.sparse.csr_matrix(A_dense) >>> np.round(A_dense) [[1. 2. 3.] [4. 6. 7.]] >>> np.round(A_dense, 1) [[1.1 2.2 3.3] [4.4 5.6 6.7]] >>> np.round(A_sparse, 1) (0, 0) 1.1 (0, 1) 2.2 (0, 2) 3.3 (1, 0) 4.4 (1, 1) 5.6 (1, 2) 6.7 </pre> </div> }}} === numpy.min() === Takes an array and returns its minimum value. If an axis is specified, returns the minima along the axis. {{{#!html <div style="background-color: #EEEEEE; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <pre style="background-color: #EEEEEE; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">numpy.min(A, axis)</span> <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.amin.html" class="https">Reference</a> A (dense matrix or sparse matrix): The matrix/array. axis (int or tuple of ints, optional): Axis or axes along which to operate. By default, flattened input is used. <span style="font-weight: bold;">Examples:</span> >>> A_dense = np.array([[5, 0, 1], [4, 3, 2]]) >>> A_sparse = scipy.sparse.csr_matrix(A_dense) >>> np.min(A_dense) 0 >>> np.min(A_dense, axis=0) [4, 0, 1] >>> np.min(A_dense, axis=1) [0, 2] >>> np.min(A_sparse, axis=1) (1, 0) 2 </pre> </div> }}} === numpy.argmin() === Takes an array and returns the index of the minimum value of the flattened array. If an axis is specified, returns the indices of the minimum values along the axis. {{{#!html <div style="background-color: #EEEEEE; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <pre style="background-color: #EEEEEE; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">numpy.argmin(A, axis)</span> <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.argmin.html" class="https">Reference</a> A (dense matrix or sparse matrix): The matrix/array. axis (int or tuple of ints, optional): Axis or axes along which to operate. By default, flattened input is used. <span style="font-weight: bold;">Examples:</span> >>> A_dense = np.array([[5, 0, 1], [4, 3, 2]]) >>> A_sparse = scipy.sparse.csr_matrix(A_dense) >>> np.argmin(A_dense) 1 >>> np.argmin(A_dense, axis=0) [1, 0, 0] >>> np.argmin(A_dense, axis=1) [1, 2] >>> np.argmin(A_sparse, axis=1) [[1] [2]] </pre> </div> }}} === numpy.argsort() === Takes an array A and returns an array of indices that sort A. Optionally, you can specify the axis along which a will be sorted. {{{#!html <div style="background-color: #EEEEEE; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <pre style="background-color: #EEEEEE; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">numpy.argsort(A, axis)</span> <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.argsort.html" class="https">Reference</a> A (dense matrix): The matrix/array. axis (int or tuple of ints, optional): Axis or axes along which to operate. By default, flattened input is used. <span style="font-weight: bold;">Examples:</span> >>> A_dense = np.array([[0, 4, 0], [4, 3, 2]]) >>> A_sparse = scipy.sparse.csr_matrix(A_dense) >>> np.argsort(A_dense) [[0 2 1] [2 1 0]] >>> np.argsort(A_dense, axis=0) [[0 1 0] [1 0 1]] >>> np.argsort(A_dense, axis=1) [[0 2 1] [2 1 0]] </pre> </div> }}} === numpy.where() === Takes a condition and optionally two array-like objects A and B. If A and B are specified, returns an array that contains elements from A where condition is true and elements from B elsewhere. {{{#!html <div style="background-color: #EEEEEE; padding: 5pt; border: 1pt solid #AEBDCC; margin: 0pt 0pt 25pt 0pt;"> <pre style="background-color: #EEEEEE; border: none; margin: 0; padding: 0"> <span style="font-weight: bold;">numpy.where(condition[, A, B])</span> <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.where.html" class="https">Reference</a> condition (bool): When True, yield A, otherwise yield B. A, B (array_like, optional): Values from which to choose. <span style="font-weight: bold;">Examples:</span> >>> A = numpy.array([[5, 4, 3], [2, 1, 0]]) >>> B = numpy.array([[0, 1, 2], [3, 4, 5]]) >>> np.where(A > 3, A, B) [[5 4 2] [3 4 5]] </pre> </div> }}} |
NumPy/SciPy Cheat Sheet
This cheat sheet is a quick reference for NumPy / SciPy beginners and gives an overview about the most important commands and functions of NumPy and SciPy that you might need on solving the exercise sheets about Linear Algebra in Information Retrieval. It doesn't claim to be complete and will be extended continuously. If you think that some important thing is missing or if you find any errors, please let us know.
Contents
General
What is NumPy?
A library that allows to work with arrays and matrices in Python.
What is SciPy?
Another library built upon NumPy that provides advanced Linear Algebra stuff.
Install
The routine to install NumPy and SciPy depends on your operating system.
Linux (Ubuntu, Debian)
apt-get install python3-numpy python3-scipy
Other systems (Windows, Mac, etc.)
For all other systems (Windows, Mac, etc.) see the instructions given on the offical SciPy website.
Matrix construction
We distinguish between dense matrices and sparse matrices (Note: The color code will be used conistently throughout this cheat sheet).
Dense matrices store every entry in the matrix, while sparse matrices only store the non-zero entries (together with their row and column index). Dense matrices are more feature-rich, but may consume more memory space than sparse matrices (in particular if most of the entries in a matrix are zero).
Dense matrices
In NumPy, there are two concepts of dense matrices: matrices and arrays. Matrices are strictly 2-dimensional, while arrays are n-dimensional (the term array is a bit misleading here).
Construct a matrix:
numpy.matrix(arg, dtype=None) Reference arg: The data to construct the matrix from, given as (1) a standard Python array; or (2) a string with columns separated by commas or spaces and rows separated by semicolons. dtype (str, optional): The type of the entries in the matrix (e.g., 'integer', 'float', 'string', etc.). Examples: >>> numpy.matrix("1 2; 3 4") [[1 2] [3 4]] >>> numpy.matrix([[1, 2], [3, 4]], dtype='float') [[1.0 2.0] [3.0 4.0]]
Construct an array:
numpy.array(arg, dtype=None, ndmin=0) Reference arg: The data to construct the matrix from, given as: (1) a standard array; or (2) a function that returns an array. dtype (str, optional): The type of the entries in the matrix ('integer', 'float', 'string', etc.). ndmin (int, optional): The minimum number of dimensions that the array should have. Examples: >>> numpy.array([[1, 2], [3, 4]]) [[1 2] [3 4]] >>> numpy.array([[1, 2], [3, 4]], dtype='float') [[1.0 2.0] [3.0 4.0]] >>> numpy.array([[1, 2], [3, 4]], ndmin=3) [[[1 2] [3 4]]]
Sparse matrices
There are two principle concepts of sparse matrices:
Compressed Sparse Row matrix (CSR matrix): entries are stored row by row (sorted by row index first)
Compressed Sparse Column matrix (CSC matrix): entries are stored column by column (sorted by column index first)
Construct a CSR/CSC matrix:
scipy.sparse.csr_matrix(arg, shape=None, dtype=None) Reference scipy.sparse.csc_matrix(arg, shape=None, dtype=None) Reference arg: The data to create the CSR matrix from, given as * a dense matrix; or * another sparse matrix; or * a tuple (m, n), to construct an empty matrix with shape (n, m); or * a tuple (data, (rows, cols), to construct a matrix A where A[rows[k], cols[k]] = data[k]; or * a tuple (data, indices, indptr) shape (int or sequence of ints): The dimensions of the matrix to create. dtype (str, optional): The type of the entries in the matrix ('integer', 'float', 'string', etc.). Examples: >>> scipy.sparse.csr_matrix([[1, 2, 3], [0, 0, 1], [0, 1, 3]]) [[1 2 3] [0 0 1] [0 1 3]] # (transformed to a dense matrix for visualization). >>> scipy.sparse.csc_matrix([[1, 2, 3], [0, 0, 1], [0, 1, 3]]) [[1 2 3] [0 0 1] [0 1 3]] # (transformed to a dense matrix for visualization). >>> values = [1, 2, 3] >>> rows = [0, 0, 1] >>> cols = [0, 1, 3] >>> scipy.sparse.csr_matrix((values, (rows, columns)), shape=[5, 5], dtype=int) [[1 2 0 0] [0 0 0 3] [0 0 0 0] [0 0 0 0]] # (transformed to a dense matrix for visualization). >>> values = [1, 2, 3] >>> rows = [0, 0, 1] >>> cols = [0, 1, 3] >>> scipy.sparse.csc_matrix((values, (rows, columns)), shape=[5, 5], dtype=int) [[1 2 0 0] [0 0 0 3] [0 0 0 0] [0 0 0 0]] # (transformed to a dense matrix for visualization).
Special matrices
There are some utility functions to create special matrices/arrays:
(1) Construct an empty array, without initializing the entries (an array with random entries):
numpy.empty(shape, dtype=float) Reference shape (int or sequence of ints): The dimensions of the array to create. dtype (str, optional): The type of the entries in the matrix ('integer', 'float', 'string', etc.). Examples: >>> numpy.empty(3) [6.95052181e-310 1.74512682e-316 1.58101007e-322] >>> numpy.empty([3, 2], dtype='int') [[140045355821992 140045355821992] [140045136216840 140045136244784] [140045125643544 140045153116544]]
(2) Construct an array filled with zeros:
numpy.zeros(shape, dtype=float) Reference shape (int or sequence of ints): The dimensions of the array to create. dtype (str, optional): The type of the entries in the matrix ('integer', 'float', 'string', etc.). Examples >>> numpy.zeros(3) [0.0, 0.0, 0.0] >>> numpy.zeros([3, 2], dtype='int') [[0 0] [0 0] [0 0]]
(3) Construct an array filled with ones:
numpy.ones(shape, dtype=float) Reference shape (int or sequence of ints): The dimensions of the array to create. dtype (str, optional): The type of the entries in the matrix ('integer', 'float', 'string', etc.). Examples: >>> numpy.ones(3) [1.0, 1.0, 1.0] >>> numpy.ones([3, 2], dtype='int') [[1 1] [1 1] [1 1]]
(4) Construct a diagonal array, a (usually square) array in which all entries are 0, except on the main diagonal:
numpy.diag(arg, k=0) Reference arg (1-dim array): The entries of the diagonal. k (int, optional): The diagonal in question. Use k > 0 for diagonals above the main diagonal, and k < 0 for diagonals below the main diagonal. Examples: >>> numpy.diag([1, 2, 3]) [[1 0 0] [0 2 0] [0 0 3]] >>> numpy.diag([1, 2, 3], k=1) [[0 1 0 0] [0 0 2 0] [0 0 0 3] [0 0 0 0]] >>> numpy.diag([1, 2, 3], k=-1) [[0 0 0 0] [1 0 0 0] [0 2 0 0] [0 0 3 0]]
scipy.sparse.diags(diagonals, offsets=0, dtype=None) Reference diagonals (sequence of arrays): The entries of the matrix diagonals. offsets (sequence of ints or int, optional): The diagonals in question. k = 0 is the main diagonal; k > 0 is the k-th upper diagonal; k < 0 is the k-th lower diagonal dtype (str, optional): The type of the entries in the matrix ('integer', 'float', 'string', etc.). Examples: >>> scipy.sparse.diags([1, 2, 3]) [[1.0 0.0 0.0] [0.0 2.0 0.0] [0.0 0.0 3.0]] # (transformed to a dense matrix for visualization). >>> scipy.sparse.diags([[1, 2, 3], [4, 5, 6]], offsets=[0, 1]) [[1.0 4.0 0.0] [0.0 2.0 5.0] [0.0 0.0 3.0]] # (transformed to a dense matrix for visualization).
(5) Construct an identity array, a square array in which all entries on the main diagonal are 1 and all other entries are 0:
numpy.identity(n, dtype=float) Reference n (int): The dimension of the array to create (the output is a n x n array). dtype (str, optional): The type of the entries in the matrix ('integer', 'float', 'string', etc.). Examples: >>> numpy.identity(3) [[1.0, 0.0, 0.0] [0.0, 1.0, 0.0] [0.0, 0.0, 1.0]] >>> numpy.identity(3, dtype=int) [[1, 0, 0] [0, 1, 0] [0, 0, 1]]
scipy.sparse.identity(n, dtype=float, format="csr") Reference n (int): The dimension of the array to create. dtype (str, optional): The type of the entries in the matrix ('integer', 'float', 'string', etc.). format (str, optional) The sparse format of the array, e.g. "csr" or "csc". Examples: >>> scipy.sparse.identity(3) [[1.0, 0.0, 0.0] [0.0, 1.0, 0.0] [0.0, 0.0, 1.0]] # (transformed to a dense matrix for visualization). >>> scipy.sparse.identity(3, dtype=int) [[1, 0, 0] [0, 1, 0] [0, 0, 1]] # (transformed to a dense matrix for visualization).
(6) Construct an triangular array, a square array in which all entries below (upper triangle) or above (lower triangle) the main diagonal are zero:
numpy.triu(arg, k=0) # Zero entries in the upper triangle of an array. Reference numpy.tril(arg, k=0) # Zero entries in the lower triangle of an array. Reference arg (array): The original array. k (int, optional): Diagonal above which to zero entries. k = 0 is the main diagonal, k < 0 is below it and k > 0 is above. Examples: >>> numpy.triu([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [[1 2 3] [0 5 6] [0 0 9]] >>> numpy.triu([[1, 2, 3], [4, 5, 6], [7, 8, 9]], k=1) [[0 2 3] [0 0 6] [0 0 0]] >>> numpy.tril([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [[1 0 0] [4 5 0] [7 8 9]] >>> numpy.tril([[1, 2, 3], [4, 5, 6], [7, 8, 9]], k=-1) [[0 0 0] [4 0 0] [7 8 0]]
scipy.sparse.triu(arg, k=0, format="csr") # Zero entries in the upper triangle of an array. Reference scipy.sparse.tril(arg, k=0, format="csr") # Zero entries in the lower triangle of an array. Reference arg (array): The original array. k (int, optional): Diagonal above which to zero entries. k = 0 is the main diagonal, k < 0 is below it and k > 0 is above. format (str, optional) The sparse format of the array, e.g. "csr" or "csc". Examples: >>> scipy.sparse.triu([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [[1 2 3] [0 5 6] [0 0 9]] # (transformed to a dense matrix for visualization). >>> scipy.sparse.triu([[1, 2, 3], [4, 5, 6], [7, 8, 9]], k=1) [[0 2 3] [0 0 6] [0 0 0]] # (transformed to a dense matrix for visualization). >>> scipy.sparse.tril([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [[1 0 0] [4 5 0] [7 8 9]] # (transformed to a dense matrix for visualization). >>> scipy.sparse.tril([[1, 2, 3], [4, 5, 6], [7, 8, 9]], k=-1) [[0 0 0] [4 0 0] [7 8 0]] # (transformed to a dense matrix for visualization).
Accessing elements
TODO: crazy element access magic, single elements, entire rows, sub-matrices
Matrix operations
Adding a constant
The addition of a constant adds the constant to every element of a matrix (only available for dense matrices).
numpy.tril(arg, k=0) # Zero entries in the lower triangle of an array. Reference A + c A (matrix or array): The matrix/array. c (constant): The constant. Examples: >>> A = np.matrix([[2, 1], [3, 5]], dtype=float) >>> A + 4 [[6 5] [7 9]]
Multiplying by a constant
Multiplying by a constant multiplies every element of a matrix by that constant (both for sparse and dense matrices).
A * c A (matrix or array): The matrix/array. c (constant): The constant. Examples: >>> A = np.matrix([[2, 1], [3, 5]], dtype=float) >>> A * 4 [[8 4] [12 20]]
A * c A (sparse matrix): The matrix. c (constant): The constant. Examples: >>> A = csr_matrix([[1, 0], [0, 1], [3, 2]], dtype=float) >>> A * 4 [[4 0] [0 4] [12 8]] # (transformed to a dense matrix for visualization).
Multiplying two matrices
There are two options on multiplying two matrices: the * operator and the dot() function. The behavior and result of both options differ depending on the type of the used matrices (resp. arrays):
(1) The * operator computes
the normal matrix multiplication when sparse and/or dense matrices are used.
the element-wise matrix multiplication when dense arrays are used.
(2) The dot() function computes
the normal matrix multiplication when a dense matrix is multiplied with a dense matrix or a sparse matrix is multiplied with a sparse matrix or a dense matrix;
bullshit when a dense matrix is multiplied with a sparse matrix.
The result of a matrix multiplication between
a sparse matrix and a sparse matrix is a sparse matrix.
a sparse matrix and a dense matrix is a dense matrix.
a dense matrix and a dense matrix is a dense matrix.
A * B A.dot(B) Reference A (dense matrix or sparse matrix): The first matrix/array. B (dense matrix or sparse matrix): The second matrix/array. Examples: >>> A = numpy.array([[1, 2], [3, 4]]) >>> M1_sparse = csr_matrix([[1, 0], [0, 1], [3, 2]], dtype=float) >>> M1_dense = A_sparse.todense() >>> M2_dense = numpy.matrix([[2, 1], [3, 4]], dtype=float) >>> M2_sparse = csr_matrix(B_dense) ## Dense array with dense array. >>> A * A # no normal, but element-wise matrix multiplication. [[ 1 4] [ 9 16]] >>> A.dot(A) # normal matrix multiplication. [[ 7 10] [15 22]] ## Dense matrix with dense matrix. >>> M1_dense * M2_dense # normal matrix multiplication. [[ 2., 1.], [ 3., 4.], [ 12., 11.]] # result is a dense matrix >>> M1_dense.dot(M2_dense) # same as M1_dense * M2_dense [[ 2., 1.], [ 3., 4.], [ 12., 11.]] # result is a dense matrix ## Sparse matrix with sparse matrix. >>> X = M1_sparse * M2_sparse # normal matrix multiplication. >>> X.todense() [[ 2., 1.], [ 3., 4.], [ 12., 11.]] # result is a sparse matrix. >>> X = M1_sparse.dot(M2_sparse) # same as M1_sparse * M2_sparse >>> X.todense() [[ 2., 1.], [ 3., 4.], [ 12., 11.]] # result is a sparse matrix. ## Sparse matrix with dense matrix. >>> M1_sparse * M2_dense # normal matrix multiplication. [[ 2., 1.], [ 3., 4.], [ 12., 11.]] # result is a dense matrix. >>> M1_sparse.dot(M2_dense) # same as M1_sparse * M2_dense [[ 2., 1.], [ 3., 4.], [ 12., 11.]] # result is a dense matrix. ## Dense matrix with sparse matrix. >>> M1_dense * M2_sparse # normal matrix multiplication. [[ 2., 1.], [ 3., 4.], [ 12., 11.]] # result is a dense matrix. >>> M1_dense.dot(M2_sparse) # computes M1_dense.dot(np.array(M2_sparse)) matrix([[ <2x2 sparse matrix of type '<class 'numpy.float64'>' with 4 stored elements in Compressed Sparse Row format>, <2x2 sparse matrix of type '<class 'numpy.float64'>' with 4 stored elements in Compressed Sparse Row format>], [ <2x2 sparse matrix of type '<class 'numpy.float64'>' with 4 stored elements in Compressed Sparse Row format>, <2x2 sparse matrix of type '<class 'numpy.float64'>' with 4 stored elements in Compressed Sparse Row format>], [ <2x2 sparse matrix of type '<class 'numpy.float64'>' with 4 stored elements in Compressed Sparse Row format>, <2x2 sparse matrix of type '<class 'numpy.float64'>' with 4 stored elements in Compressed Sparse Row format>]], dtype=object)
Useful methods
numpy.round()
Takes an array or matrix and rounds its values to the given number of decimals. Note that for values exactly halfway between rounded decimal values, numpy rounds to the nearest even value.
numpy.round(A, decimals) Reference A (dense matrix or sparse matrix): The matrix/array. decimal (int, optional): Number of decimal places to round to (default: 0). Examples: >>> A_dense = np.array([[1.11, 2.22, 3.33], [4.44, 5.55, 6.66]]) >>> A_sparse = scipy.sparse.csr_matrix(A_dense) >>> np.round(A_dense) [[1. 2. 3.] [4. 6. 7.]] >>> np.round(A_dense, 1) [[1.1 2.2 3.3] [4.4 5.6 6.7]] >>> np.round(A_sparse, 1) (0, 0) 1.1 (0, 1) 2.2 (0, 2) 3.3 (1, 0) 4.4 (1, 1) 5.6 (1, 2) 6.7
numpy.min()
Takes an array and returns its minimum value. If an axis is specified, returns the minima along the axis.
numpy.min(A, axis) Reference A (dense matrix or sparse matrix): The matrix/array. axis (int or tuple of ints, optional): Axis or axes along which to operate. By default, flattened input is used. Examples: >>> A_dense = np.array([[5, 0, 1], [4, 3, 2]]) >>> A_sparse = scipy.sparse.csr_matrix(A_dense) >>> np.min(A_dense) 0 >>> np.min(A_dense, axis=0) [4, 0, 1] >>> np.min(A_dense, axis=1) [0, 2] >>> np.min(A_sparse, axis=1) (1, 0) 2
numpy.argmin()
Takes an array and returns the index of the minimum value of the flattened array. If an axis is specified, returns the indices of the minimum values along the axis.
numpy.argmin(A, axis) Reference A (dense matrix or sparse matrix): The matrix/array. axis (int or tuple of ints, optional): Axis or axes along which to operate. By default, flattened input is used. Examples: >>> A_dense = np.array([[5, 0, 1], [4, 3, 2]]) >>> A_sparse = scipy.sparse.csr_matrix(A_dense) >>> np.argmin(A_dense) 1 >>> np.argmin(A_dense, axis=0) [1, 0, 0] >>> np.argmin(A_dense, axis=1) [1, 2] >>> np.argmin(A_sparse, axis=1) [[1] [2]]
numpy.argsort()
Takes an array A and returns an array of indices that sort A. Optionally, you can specify the axis along which a will be sorted.
numpy.argsort(A, axis) Reference A (dense matrix): The matrix/array. axis (int or tuple of ints, optional): Axis or axes along which to operate. By default, flattened input is used. Examples: >>> A_dense = np.array([[0, 4, 0], [4, 3, 2]]) >>> A_sparse = scipy.sparse.csr_matrix(A_dense) >>> np.argsort(A_dense) [[0 2 1] [2 1 0]] >>> np.argsort(A_dense, axis=0) [[0 1 0] [1 0 1]] >>> np.argsort(A_dense, axis=1) [[0 2 1] [2 1 0]]
numpy.where()
Takes a condition and optionally two array-like objects A and B. If A and B are specified, returns an array that contains elements from A where condition is true and elements from B elsewhere.
numpy.where(condition[, A, B]) Reference condition (bool): When True, yield A, otherwise yield B. A, B (array_like, optional): Values from which to choose. Examples: >>> A = numpy.array([[5, 4, 3], [2, 1, 0]]) >>> B = numpy.array([[0, 1, 2], [3, 4, 5]]) >>> np.where(A > 3, A, B) [[5 4 2] [3 4 5]]