SciPy – 5 – algebra lineare con SciPy

Continuo da qui, copio qui.
Finito il ripasso di NumPy oggi entra in campo SciPy 😁

Of course you first need to make sure firstly that you have Python installed. Go to this page if you still need to do this 🙂 If you’re working on Windows, make sure that you have added Python to the PATH environment variable. In addition, don’t forget to install a package manager, such as pip, which will ensure that you’re able to use Python’s open-source libraries.

Kalijn suggerisce di usare pip ma avendo installato Anaconda SciPy ce l’ho già:

After these steps, you’re ready to goy!

Uh! ecco qui, proprio come dicevo:

Tip: install the package by downloading the Anaconda Python distribution. It’s an easy way to get started quickly, as Anaconda not only includes 100 of the most popular [qui] Python, R and Scala packages for data science, but also includes several open course development environments such as Jupyter and Spyder.

Vettori e Matrici, le basi
Now that you have made sure that your workspace is prepped, you can finally get started with linear algebra in Python. In essence, this discipline is occupied with the study of vector spaces and the linear mappings that exist between them. These linear mappings can be described with matrices, which also makes it easier to calculate.

Remember that a vector space is a fundamental concept in linear algebra. It’s a space where you have a collection of objects (vectors) and where you can add or scale two vectors without the resulting vector leaving the space. Remember also that vectors are rows (or columns) of a matrix.

But how does this work in Python?

You can easily create a vector with the np.array() function. Similarly, you can give a matrix structure to every one-or two-dimensional ndarray with either the np.matrix() or np.mat() commands.

Well, not exactly. There are some differences:

  • A matrix is 2-D, while arrays are usually n-D,
  • As the functions above already implied, the matrix is a subclass of ndarray,
  • Both arrays and matrices have .T(), but only matrices have .H() and .I(),
  • Matrix multiplication works differently from element-wise array multiplication, and
  • To add to this, the ** operation has different results for matrices and arrays

When you’re working with matrices, you might sometimes have some in which most of the elements are zero. These matrices are called “sparse matrices”, while the ones that have mostly non-zero elements are called “dense matrices”.

In itself, this seems trivial, but when you’re working with SciPy for linear algebra, this can sometimes make a difference in the modules that you use to get certain things done. More concretely, you can use scipy.linalg for dense matrices, but when you’re working with sparse matrices, you might also want to consider checking up on the scipy.sparse module, which also contains its own scipy.sparse.linalg.

For sparse matrices, there are quite a number of options to create them. The code chunk below lists some:

Additionally, there are also some other functions that you might be able to use to create sparse matrices: Block Sparse Row matrices with bsr_matrix(), COOrdinate format sparse matrices with coo_matrix(), DIAgonal storage sparse matrices with dia_matrix(), and Row-based linked list sparse matrices with lil_matrix().

There are really a lot of options, but which one should you choose if you’re making a sparse matrix yourself? It’s not that hard.

Basically, it boils down to first is how you’re going to initialize it. Next, consider what you want to be doing with your sparse matrix.

More concretely, you can go through the following checklist to decide what type of sparse matrix you want to use:

  • If you plan to fill the matrix with numbers one by one, pick a coo_matrix() or dok_matrix() to create your matrix.
  • If you want to initialize the matrix with an array as the diagonal, pick dia_matrix() to initialize your matrix.
  • For sliced-based matrices, use lil_matrix().
  • If you’re constructing the matrix from blocks of smaller matrices, consider using bsr_matrix().
  • If you want to have fast access to your rows and columns, convert your matrices by using the csr_matrix() and csc_matrix() functions, respectively. The last two functions are not great to pick when you need to initialize your matrices, but when you’re multiplying, you’ll definitely notice the difference in speed.

Vai tranquillo! dice la prof 😊 A volte manca un’istruzione, come nello screenshot precedente, a volte manca un import ma si può fare (cit.). E Karlijn tockz! 🚀


Post a comment or leave a trackback: Trackback URL.



Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:


Stai commentando usando il tuo account Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger hanno fatto clic su Mi Piace per questo: