Python for Scientific Computation

Python

Python : A powerfull yet friendly programming language that can be used in almost every field.
# the hello world program in python
print('Hello World!')

Doc

Popular Scientific Python Packages

NumPy : The foundation of scientific python that brings to Python the high performance of scientific computation, as well as the MATLAB experience.
import numpy as np
x = np.arange(0, 2*np.pi, 0.1)
y = np.sin(x)

Overview Doc Video

SciPy : Extends the power of NumPy with more tools, similar to MATLAB's toolboxes.
import numpy as np
from scipy.fftpack import fft, ifft
x = np.random.randn(100)
y = fft(x)
yinv = ifft(y)

Doc

Matplotlib : Visualizes scientific data and produces publication quality figures, with a MATLAB-like interface.
import numpy as np
import matplotlib.pyplot as plt

N = 50 
x = np.random.rand(N) y = np.random.rand(N) colors = np.random.rand(N) area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radiuses plt.scatter(x, y, s=area, c=colors, alpha=0.5) plt.show()

Gallery Doc Video

Basemap : The matplotlib basemap toolkit is a library for plotting 2D data on maps in Python. It is similar in functionality to the matlab mapping toolbox, the IDL mapping facilities, GrADS, or the Generic Mapping Tools. (Deprecation Notice: Basemap is deprecated in favor of the Cartopy project.)
from mpl_toolkits.basemap import Basemap
m = Basemap(projection='kav7',lon_0=0,resolution=None)
map.drawcoastlines(linewidth=0.25)
map.drawcountries(linewidth=0.25)
map.fillcontinents(color='coral',lake_color='aqua')

Gallery Doc

Cartopy : Cartopy is a Python package designed for geospatial data processing in order to produce maps and other geospatial data analyses.
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()

ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61

plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
         color='blue', linewidth=2, marker='o',
         transform=ccrs.Geodetic(),
         )

plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
         color='gray', linestyle='--',
         transform=ccrs.PlateCarree(),
         )

plt.text(ny_lon - 3, ny_lat - 12, 'New York',
         horizontalalignment='right',
         transform=ccrs.Geodetic())

plt.text(delhi_lon + 3, delhi_lat - 12, 'Delhi',
         horizontalalignment='left',
         transform=ccrs.Geodetic())
        

Projection list Doc

IPython : Provides a rich architecture for interactive computing.
Jupyter : A web application for interactive data science and scientific computing
Pandas : The art of data analysis, especially for time series and table data
import pandas as pd
s = pd.Series([1,3,5,np.nan,6,8])
dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))

Doc Video

Xarray : Brings to NumPy N-D arrays the Pandas-like labeled axes, and the NetCDF-like but in memory data structure.
import xarray as xr
ds = xr.open_dataset('test.nc')

Doc Video

Numba : Makes Python run fast.
Seaborn : A Python visualization library based on matplotlib. It provides a high-level interface for drawing attractive statistical graphics.
import seaborn as sns
sns.set_style('darkgrid')
sns.set_context('talk')
g = sns.jointplot("total_bill", "tip", data=tips, kind="reg",xlim=(0, 60), ylim=(0, 12), color="r", size=7)

Gallery Doc Video

Scikit-learn : Machine Learning in Python.

Keras : Keras is a deep learning API written in Python, running on top of the machine learning platform TensorFlow.

PyTorch : PyTorch is an optimized tensor library for deep learning using GPUs and CPUs.

Install Python Packages

Conda : The easiest way to install Python packages.

First, install Miniconda: Instruction

After Miniconda is installed, install the scientific Python packages using command conda install:

conda install numpy scipy matplotlib basemap ipython jupyter netcdf4 h5py pandas xarray dask bottleneck

You can also use conda install to install other python packages with similar syntax:

conda install PACKAGENAME

Doc

A talk on the stack of scientific Python.