AD Teaching Wiki
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

FrontPage

AD Teaching Wiki:
  • Diff for "NumpyCheatSheet"
Last updated at 2019-11-19 10:20:29
Differences between revisions 1 and 104 (spanning 103 versions)
⇤ ← Revision 1 as of 2017-01-13 17:45:49 →
Size: 4421
Editor: Patrick Brosi
Comment:
← Revision 104 as of 2017-12-14 15:22:22 → ⇥
Size: 13852
Editor: adpult
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= numpy cheat sheet = #acl All:read

= 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.

<<TableOfContents()>>
Line 5: Line 11:
=== 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 python-numpy python-scipy
}}}

=== Other systems (Windows, Mac, etc.) ===

For all other systems (Windows, Mac, etc.) see the instructions given on the offical [[https://scipy.org/install.html|SciPy website]].

------
Line 7: Line 34:
TODO (Hannah): for dense matrices (matrix vs. array) as well as sparse matrices (csr_matrix((data, indices, indptr)) {{{#!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: This 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:
{{{#!html
<div style="background-color: #E5E5FF; padding: 5pt; border: 1pt solid #AEBDCC;">
<span style="background-color: #7F7FFF; padding: 2pt 5pt; float: right;">Dense</span>
<pre style="background-color: #E5E5FF; border: none; margin: 0;">
numpy.matrix(arg, dtype=None) <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.matrix.html" class="https">Reference</a>

arg:
   The data to construct the matrix from, given as
     * a standard Python array; or
     * 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]]
</pre>
</div>
}}}

Construct an array:
{{{#!html
<div style="background-color: #E5E5FF; padding: 5pt; border: 1pt solid #AEBDCC;">
<span style="background-color: #7F7FFF; padding: 2pt 5pt; float: right;">Dense</span>
<pre style="background-color: #E5E5FF; border: none; margin: 0;">
numpy.array(arg, dtype=None, ndmin=0) <a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.array.html" class="https">Reference</a>

arg:
   The data to construct the matrix from, given as
      * a standard array; or
      * 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]]]
</pre>
}}}

=== 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:
{{{#!html
<pre style="background-color: #E5FFE5;">
scipy.sparse.csr_matrix(arg, shape=None, dtype=None)
scipy.sparse.csc_matrix(arg, shape=None, dtype=None)

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 1 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 1 0 0]
 [0 0 0 3]
 [0 0 0 0]
 [0 0 0 0]] # (transformed to a dense matrix for visualization).
</pre>
}}}
[[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>>

=== 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):
{{{#!html
<pre style="background-color: #E5E5FF;">
numpy.empty(shape, dtype=float)

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]]
</pre>
}}}
[[https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.empty.html|numpy.empty]] <<BR>><<BR>>

(2) Construct an '''array filled with zeros''':
{{{#!html
<pre style="background-color: #E5E5FF;">
numpy.zeros(shape, dtype=float)

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]]
</pre>
}}}
[[https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.zeros.html|numpy.zeros]] <<BR>><<BR>>

(3) Construct an '''array filled with ones''':
{{{#!html
<pre style="background-color: #E5E5FF;">
numpy.ones(shape, dtype=float)

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]]
</pre>
}}}
[[https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ones.html|numpy.ones]] <<BR>><<BR>>

(4) Construct a '''diagonal array''', a (usually square) array in which all entries are 0, except on the main diagonal:

{{{#!html
<pre style="background-color: #E5E5FF;">
numpy.diag(arg, k=0)

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]]
</pre>
}}}
[[https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.diag.html|numpy.diag]]

{{{#!html
<pre style="background-color: #E5FFE5;">
scipy.sparse.diags(diagonals, offsets=0, dtype=None)

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).
</pre>
}}}
[[https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.diags.html|scipy.sparse.diags]] <<BR>><<BR>>


(5) Construct an '''identity array''', a square array in which all entries on the main diagonal are 1 and all other entries are 0:

{{{#!html
<pre style="background-color: #E5E5FF;">
numpy.identity(n, dtype=float)

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]]
</pre>
}}}
[[https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.identity.html|numpy.identity]]

{{{#!html
<pre style="background-color: #E5FFE5;">
scipy.sparse.identity(n, dtype=float, format="csr")

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).
</pre>
}}}
[[https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.identity.html|scipy.sparse.identity]]<<BR>><<BR>>

(6) Construct an '''triangular array''', a square array in which all entries below (upper triangle) or above (lower triangle) the main diagonal are zero:

{{{#!html
<pre style="background-color: #E5E5FF;">
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.

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]]
</pre>
}}}
[[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>>

{{{#!html
<pre style="background-color: #E5FFE5;">
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.

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).
</pre>
}}}
[[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>>

------
Line 11: Line 437:
TODO (Hannah): crazy element access magic, single elements, entire rows, sub-matrices TODO: crazy element access magic, single elements, entire rows, sub-matrices

------
Line 15: Line 443:
TODO (Raghu): examples of dot product (dense * dense, dense * sparse, sparse * sparse), usage of both matrix.dot() and * (and how it behaves in different contexts), constant factor adding / multiplication

TODO (Claudius): Element-wise operations like taking log, sqrt. Multiplying two m*n matrices element-wise (for example, to square the entries in a matrix etc...)

== Row- or column-wise operations ==

TODO (Claudius): summing of rows or columns, sorting rows / columns etc
TODO

------
Line 25: Line 449:
TODO (Natalie): numpy.where, numpy.argsort, numpy.min, numpy.argmin, numpy.round (useful for tests)
== Special matrices ==

=== Diagonal matrix ===

Matrix (usually square) in which all entries are zero, except on the main diagonal. Use [[https://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.diag.html | numpy.diag]] to either create a diagonal matrix from a givin main diagonal, or extract the diagonal matrix from a given matrix.

{{{
>>> numpy.diag([1,2,3])
array([[1, 0, 0],
       [0, 2, 0],
       [0, 0, 3]])
}}}
{{{
>>> numpy.diag([[1, 5, 4],
                [7, 2, 4],
                [4, 7, 3]])
array([1, 2, 3])
}}}

For a sparse matrix, use [[https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.spdiags.html#scipy.sparse.spdiags|scipy.spare.spdiags]]

=== Identity matrix ===

Special diagonal ''m''*''m'' matrix where all elements on the main diagonal are 1. Sometimes denoted as '''1'''. Read as the '1' of matrix world. For example, a ''n''*''m'' matrix ''A'' multiplied with an ''m''*''m'' identity matrix yields ''A'' again. Use [[https://docs.scipy.org/doc/numpy/reference/generated/numpy.identity.html|numpy.identity(k)]] to create a ''k''*''k'' identity matrix.

{{{
>>> numpy.identity(4)
array([[ 1., 0., 0., 0.],
       [ 0., 1., 0., 0.],
       [ 0., 0., 1., 0.],
       [ 0., 0., 0., 1.]])
}}}

{{{
>>> numpy.array([[1, 2, 3],
                 [3, 4, 3]]).dot(numpy.identity(3))
array([[ 1., 2., 3.],
       [ 3., 4., 3.]])
}}}

For a sparse matrix, use [[https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.identity.html|scipy.sparse.identity]]

=== Triangular matrix ===

A (square) matrix where all elements below (upper triangle) or above (lower triangle) the main diagonal are zero.
{{{numpy.triu}}} creates the upper ({{{u}}}), {{{numpy.triu}}} the lower ({{{l}}}) triangular matrix from a given matrix.

{{{
>>> numpy.triu([[1, 5, 4],
                [7, 2, 4],
                [4, 7, 3]])
array([[1, 5, 4],
       [0, 2, 4],
       [0, 0, 3]])
}}}


{{{
>>> numpy.tril([[1, 5, 4],
                [7, 2, 4],
                [4, 7, 3]])
array([[1, 0, 0],
       [7, 2, 0],
       [4, 7, 3]])
}}}

For a sparse matrix, use [[https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.triu.html|scipy.sparse.triu]] and [[https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.tril.html|scipy.sparse.tril]]


== Matrix decomposition ==

=== Singular Value Decompostion (SVD) ===

Factorize a matrix ''A'' (''m''*''n'') into three matrices ''U'' (''m'' * ''r''), ''S'' (''r'' * ''r'') and ''V'' (''r'' * ''n'') such that ''A'' = ''U'' * ''S'' * ''V''. Here ''r'' is the [[https://en.wikipedia.org/wiki/Rank_(linear_algebra)| rank]] of ''A''.

Use [[https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.svd.html|numpy.linalg.svd]] to do a singular value decomposition for a dense matrix. Use [[https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.linalg.svds.html|scipy.sparse.linalg.svds]] for sparse matrices (computes the largest ''k'' singular values for a sparse matrix).

{{{
>>> Uk, Sk, Vk = svds(csr_matrix([[1, 2, 3], [3, 4, 5], [5, 6, 4]], dtype=float), 2)
>>> print("Uk:\n", Uk, "\nSk:\n", Sk, "\nVk:\n", Vk)
U:
 [[ 0.56475636 -0.30288472]
 [ 0.51457155 -0.59799935]
 [-0.64518709 -0.74206309]]
S:
 [ 2.13530566 11.67829513]
V:
 [[-0.52332762 -0.32001209 0.78975975]
 [-0.49726421 -0.63794803 -0.58800563]]
}}}
TODO

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

  1. NumPy/SciPy Cheat Sheet
    1. General
      1. What is NumPy?
      2. What is SciPy?
    2. Install
      1. Linux (Ubuntu, Debian)
      2. Other systems (Windows, Mac, etc.)
    3. Matrix construction
      1. Dense matrices
      2. Sparse matrices
      3. Special matrices
    4. Accessing elements
    5. Matrix operations
    6. Useful methods

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 python-numpy python-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: This 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:

Dense
numpy.matrix(arg, dtype=None)  Reference

arg:
   The data to construct the matrix from, given as
     * a standard Python array; or
     * 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:

Dense
numpy.array(arg, dtype=None, ndmin=0)  Reference

arg:
   The data to construct the matrix from, given as
      * a standard array; or
      * 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)
scipy.sparse.csc_matrix(arg, shape=None, dtype=None)

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 1 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 1 0 0]
 [0 0 0 3]
 [0 0 0 0]
 [0 0 0 0]]  # (transformed to a dense matrix for visualization).

scipy.sparse.csr_matrix
scipy.sparse.csc_matrix

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)

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]]

numpy.empty

(2) Construct an array filled with zeros:

numpy.zeros(shape, dtype=float)

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]]

numpy.zeros

(3) Construct an array filled with ones:

numpy.ones(shape, dtype=float)

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]]

numpy.ones

(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)

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]]

numpy.diag

scipy.sparse.diags(diagonals, offsets=0, dtype=None)

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).

scipy.sparse.diags

(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)

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]]

numpy.identity

scipy.sparse.identity(n, dtype=float, format="csr")

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).

scipy.sparse.identity

(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.
numpy.tril(arg, k=0)  # Zero entries in the lower triangle of an array.

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]]

numpy.triu
numpy.tril

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.

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).

scipy.sparse.triu
scipy.sparse.tril


Accessing elements

TODO: crazy element access magic, single elements, entire rows, sub-matrices


Matrix operations

TODO


Useful methods

TODO

AD Teaching Wiki: NumpyCheatSheet (last edited 2019-11-19 10:20:29 by adpult)

  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01