📜 ⬆️ ⬇️

Python for mathematical calculations

image The python ecosystem is evolving rapidly. This is no longer just a general purpose language. With it, you can successfully develop web applications, system utilities and much more. In this article, we will focus on another application, namely, scientific calculations.


We will try to find in the language functions that we usually require from math packages. Consider the strengths and weaknesses of the idea of ​​using python instead of MATLAB, Maple, Mathcad, Mathematica.


Development environment


Python code can be placed in a file with the .py extension and sent to the interpreter for execution. This is a classic approach that is usually diluted using a development environment, such as pyCharm. However, for python ( and not only ), there is another way of interacting with the interpreter - interactive jupyter notepads that preserve the intermediate state of the program between the execution of various blocks of code that can be executed in any order. This way of interaction is borrowed from Mathematica notebooks, later an analogue appeared in MATLAB (Live script).



Thus, all work with the python-code is transferred to the browser. The resulting notebook can be opened using nbviewer.jupyter.org , github (and gist ) are able to independently display the contents of such files (convert).


The jupyter browser nature implies its flaws: lack of a debugger and problems with printing a large amount of information (browser window hanging). The latter problem is solved by an extension that limits the maximum number of characters that can be displayed as a result of executing a single cell.


Data visualization


For data visualization, the matplotlib library is usually used, whose commands are very similar to MATLAB. In Stanford, a library was developed that extends the capabilities of matplotlib - seaborn (unusual graphs for statistics).



Consider an example of constructing a histogram for the generated data sample.


import numpy as np import matplotlib.mlab as mlab import matplotlib.pyplot as plt # example data mu = 100 # mean of distribution sigma = 15 # standard deviation of distribution x = mu + sigma * np.random.randn(10000) num_bins = 50 # the histogram of the data n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5) # add a 'best fit' line y = mlab.normpdf(bins, mu, sigma) plt.plot(bins, y, 'r--') plt.xlabel('Smarts') plt.ylabel('Probability') plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$') # Tweak spacing to prevent clipping of ylabel plt.subplots_adjust(left=0.15) plt.show() 


We see that the syntax of matplotlib is very similar to the syntax of MATLAB. It is also worth noting that latex is used in the graph header.


Computational Mathematics


For linear algebra in python, it is customary to use numpy , the vectors and matrices of which are typed, in contrast to the built-in language of lists. For scientific calculations using the library scipy .


Especially for users of MATLAB written guide on the transition from MATLAB to numpy .


 import scipy.integrate as integrate import scipy.special as special result = integrate.quad(lambda x: special.jv(2.5,x), 0, 4.5) 

In this example, the value of a definite integral of the Bessel function on the interval [0.0.45] is calculated numerically using the QUADPACK (Fortran) library.


Character calculations


You can use the sympy library to use symbolic computations. However, code written with sympy is inferior in beauty to code written in Mathematica, which is specialized in symbolic calculations.


 # python from sympy import Symbol, solve x = Symbol("x") solve(x**2 - 1) 


By functionality, Sympy is inferior to Mathematica, however, taking into account your needs, it may turn out that for you their capabilities are approximately equal. A more detailed comparison can be found in the sympy repository wiki .


We speed up the code


To speed up your code by converting to C ++, it can be implemented using theano library. Payment for such acceleration becomes syntax, now you need to write theano-oriented functions and specify the types of all variables.


 import theano import theano.tensor as T x = T.dmatrix('x') s = 1 / (1 + T.exp(-x)) logistic = theano.function([x], s) logistic([[0, 1], [-1, -2]]) 

Some libraries for convolutional neural networks, such as Lasagne and Keras , use theano for their calculations. It is also worth adding that theano supports acceleration due to computing on the GPU.


Machine learning


The most popular machine learning library for python is scikit-learn , which contains all the basic algorithms of machine learning, as well as quality metrics, tools for validating algorithms, tools for pre-processing data.



 from sklearn import svm from sklearn import datasets clf = svm.SVC() iris = datasets.load_iris() X, y = iris.data, iris.target clf.fit(X, y) clf.predict(X) 

Pandas is commonly used to load data from tabular data formats (excel, csv). Downloaded data are presented in memory in the form of DataFrames, to which you can apply various operations: both lower case (row processing) and group (filters, groupings). An overview of the main functions of pandas can be found in the presentation " Pandas: an overview of the main functions " (Author: Alexander Dyakonov, Professor at Moscow State University).


Not everything is so smooth ...


However, not everything is so smooth in python. For example, two versions of language 2. and 3. are now getting along . Both of them are developed in parallel, but the syntax of version 2 is not fully compatible with the syntax of version 3.


You may have another problem if you are not the linux owner, in this case you may have difficulties installing some libraries, some libraries will not be completely compatible, for example tensorflow .


Libraries in question

PS: All the python libraries mentioned in this article are open source and distributed free of charge. To download them, you can use the pip command or simply download the Anaconda assembly, which contains all the main libraries.


')

Source: https://habr.com/ru/post/312268/


All Articles