In [7]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['rand']
`%matplotlib` prevents importing * from pylab and numpy

In [2]:
from IPython.display import Latex

Basic Array Operations


In [3]:
import numpy as np

arr = np.arange(1e7)

In [4]:
arr


Out[4]:
array([  0.00000000e+00,   1.00000000e+00,   2.00000000e+00, ...,
         9.99999700e+06,   9.99999800e+06,   9.99999900e+06])

In [5]:
larr = arr.tolist()
print type(larr)


<type 'list'>

In [6]:
larr[:10]


Out[6]:
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

In [7]:
larr[:2] * 3 # Do not support array multiplication (broadcasting).


Out[7]:
[0.0, 1.0, 0.0, 1.0, 0.0, 1.0]

In [8]:
arr * 3 # ndarray support broadcasting.


Out[8]:
array([  0.00000000e+00,   3.00000000e+00,   6.00000000e+00, ...,
         2.99999910e+07,   2.99999940e+07,   2.99999970e+07])

In [9]:
arr = np.zeros((3, 3, 3))
arr


Out[9]:
array([[[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]]])

In [10]:
print "\n".join(np.matrix.__doc__.split("\n")[:7])
print 
mat = np.matrix(np.zeros((3, 2)))
print mat


    matrix(data, dtype=None, copy=True)

    Returns a matrix from an array-like object, or from a string of data.
    A matrix is a specialized 2-D array that retains its 2-D nature
    through operations.  It has certain special operators, such as ``*``
    (matrix multiplication) and ``**`` (matrix power).

[[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]

In [11]:
# Converting a list to ndarray (using np.array: array-like object --> numpy array)
alist = [1, 2, 3]
arr = np.array(alist)
arr


Out[11]:
array([1, 2, 3])

In [12]:
# Instanciating an numpy array using np.ndarray. (shape --> numpy array)
array1 = np.ndarray(1000)
print array1.shape

array2 = np.ndarray((1000, 1))
print array2.shape


(1000,)
(1000, 1)

In [13]:
array1[:10]


Out[13]:
array([  1.28822975e-231,   1.28822975e-231,   3.00384435e-273,
         3.38503263e+125,   7.17065513e-310,   3.56014368e-306,
        -2.05747454e-289,   4.22633667e-308,   2.37989208e-244,
         4.96393995e+173])

In [14]:
print np.ndarray.__doc__


ndarray(shape, dtype=float, buffer=None, offset=0,
            strides=None, order=None)

    An array object represents a multidimensional, homogeneous array
    of fixed-size items.  An associated data-type object describes the
    format of each element in the array (its byte-order, how many bytes it
    occupies in memory, whether it is an integer, a floating point number,
    or something else, etc.)

    Arrays should be constructed using `array`, `zeros` or `empty` (refer
    to the See Also section below).  The parameters given here refer to
    a low-level method (`ndarray(...)`) for instantiating an array.

    For more information, refer to the `numpy` module and examine the
    the methods and attributes of an array.

    Parameters
    ----------
    (for the __new__ method; see Notes below)

    shape : tuple of ints
        Shape of created array.
    dtype : data-type, optional
        Any object that can be interpreted as a numpy data type.
    buffer : object exposing buffer interface, optional
        Used to fill the array with data.
    offset : int, optional
        Offset of array data in buffer.
    strides : tuple of ints, optional
        Strides of data in memory.
    order : {'C', 'F'}, optional
        Row-major or column-major order.

    Attributes
    ----------
    T : ndarray
        Transpose of the array.
    data : buffer
        The array's elements, in memory.
    dtype : dtype object
        Describes the format of the elements in the array.
    flags : dict
        Dictionary containing information related to memory use, e.g.,
        'C_CONTIGUOUS', 'OWNDATA', 'WRITEABLE', etc.
    flat : numpy.flatiter object
        Flattened version of the array as an iterator.  The iterator
        allows assignments, e.g., ``x.flat = 3`` (See `ndarray.flat` for
        assignment examples; TODO).
    imag : ndarray
        Imaginary part of the array.
    real : ndarray
        Real part of the array.
    size : int
        Number of elements in the array.
    itemsize : int
        The memory use of each array element in bytes.
    nbytes : int
        The total number of bytes required to store the array data,
        i.e., ``itemsize * size``.
    ndim : int
        The array's number of dimensions.
    shape : tuple of ints
        Shape of the array.
    strides : tuple of ints
        The step-size required to move from one element to the next in
        memory. For example, a contiguous ``(3, 4)`` array of type
        ``int16`` in C-order has strides ``(8, 2)``.  This implies that
        to move from element to element in memory requires jumps of 2 bytes.
        To move from row-to-row, one needs to jump 8 bytes at a time
        (``2 * 4``).
    ctypes : ctypes object
        Class containing properties of the array needed for interaction
        with ctypes.
    base : ndarray
        If the array is a view into another array, that array is its `base`
        (unless that array is also a view).  The `base` array is where the
        array data is actually stored.

    See Also
    --------
    array : Construct an array.
    zeros : Create an array, each element of which is zero.
    empty : Create an array, but leave its allocated memory unchanged (i.e.,
            it contains "garbage").
    dtype : Create a data-type.

    Notes
    -----
    There are two modes of creating an array using ``__new__``:

    1. If `buffer` is None, then only `shape`, `dtype`, and `order`
       are used.
    2. If `buffer` is an object exposing the buffer interface, then
       all keywords are interpreted.

    No ``__init__`` method is needed because the array is fully initialized
    after the ``__new__`` method.

    Examples
    --------
    These examples illustrate the low-level `ndarray` constructor.  Refer
    to the `See Also` section above for easier ways of constructing an
    ndarray.

    First mode, `buffer` is None:

    >>> np.ndarray(shape=(2,2), dtype=float, order='F')
    array([[ -1.13698227e+002,   4.25087011e-303],
           [  2.88528414e-306,   3.27025015e-309]])         #random

    Second mode:

    >>> np.ndarray((2,), buffer=np.array([1,2,3]),
    ...            offset=np.int_().itemsize,
    ...            dtype=int) # offset = 1*itemsize, i.e. skip first element
    array([2, 3])

In [15]:
np.linspace(0, 10, 10000) # zero to 10 with 10000 steps.


Out[15]:
array([  0.00000000e+00,   1.00010001e-03,   2.00020002e-03, ...,
         9.99799980e+00,   9.99899990e+00,   1.00000000e+01])

In [16]:
cube = np.zeros((5, 5, 5))
cube.dtype


Out[16]:
dtype('float64')

In [17]:
cube2 = cube.astype(int) + 1
cube2[0:2]


Out[17]:
array([[[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]],

       [[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]]])

In [18]:
cube2.dtype


Out[18]:
dtype('int64')

In [19]:
int_arr = np.zeros((1, 10), dtype=int)

In [20]:
int_arr.tolist()


Out[20]:
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

In [21]:
int_arr2 = np.zeros(10, dtype=int)

In [22]:
int_arr2.tolist()


Out[22]:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

In [23]:
arr1d = np.zeros((1000, 1))

In [24]:
arr3d = arr1d.reshape((10, 10, 10))

In [25]:
arr3d[9]


Out[25]:
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

In [26]:
arr = arr3d.ravel() # Flat a multidimensional array to 1-D array.
arr.shape


Out[26]:
(1000,)

Record Array


In [27]:
recarr = np.zeros((2,), dtype = ('i4, f4, a10'))
toadd = [(1, 2, 'Hello'), (2, 3., 'World')]

In [28]:
recarr[:] = toadd

In [29]:
recarr


Out[29]:
array([(1, 2.0, 'Hello'), (2, 3.0, 'World')], 
      dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', 'S10')])

In [30]:
recarr.dtype.names = ("Int", 'Float', "String")
recarr


Out[30]:
array([(1, 2.0, 'Hello'), (2, 3.0, 'World')], 
      dtype=[('Int', '<i4'), ('Float', '<f4'), ('String', 'S10')])

In [31]:
recarr["Int"]


Out[31]:
array([1, 2], dtype=int32)

In [32]:
recarr["String"]


Out[32]:
array(['Hello', 'World'], 
      dtype='|S10')

Indexing


In [33]:
arr = np.arange(100).reshape((10, 10))

In [34]:
print arr[1, 9]
print arr[2, 3]


19
23

In [35]:
arr[1, :]


Out[35]:
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])

In [36]:
arr[2:4, 0:3]


Out[36]:
array([[20, 21, 22],
       [30, 31, 32]])

In [37]:
ind = np.where(arr >= 30)
ind


Out[37]:
(array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5,
        5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7,
        7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9,
        9]),
 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
        3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
        6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8,
        9]))

In [38]:
arr[ind].reshape((np.unique(ind[0]).shape[0], np.unique(ind[1]).shape[0]))


Out[38]:
array([[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
       [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
       [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
       [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
       [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
       [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])

In [39]:
np.unique(ind[0]).shape[0]


Out[39]:
7

In [40]:
img1 = np.zeros((20, 20)) + 3
img1[4:-4, 4:-4] = 6
img1[7:-7, 7:-7] = 9

In [41]:
img1


Out[41]:
array([[ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,
         6.,  6.,  6.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,
         6.,  6.,  6.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,
         6.,  6.,  6.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  6.,  6.,  6.,  9.,  9.,  9.,  9.,  9.,  9.,
         6.,  6.,  6.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  6.,  6.,  6.,  9.,  9.,  9.,  9.,  9.,  9.,
         6.,  6.,  6.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  6.,  6.,  6.,  9.,  9.,  9.,  9.,  9.,  9.,
         6.,  6.,  6.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  6.,  6.,  6.,  9.,  9.,  9.,  9.,  9.,  9.,
         6.,  6.,  6.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  6.,  6.,  6.,  9.,  9.,  9.,  9.,  9.,  9.,
         6.,  6.,  6.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  6.,  6.,  6.,  9.,  9.,  9.,  9.,  9.,  9.,
         6.,  6.,  6.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,
         6.,  6.,  6.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,
         6.,  6.,  6.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,
         6.,  6.,  6.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.]])

In [42]:
imshow(img1)


Out[42]:
<matplotlib.image.AxesImage at 0x122e9e510>

In [43]:
compound_ind = (img1 > 3) & (img1 < 7)

In [44]:
import copy

In [45]:
%time img2 = copy.deepcopy(img1)


CPU times: user 30 µs, sys: 9 µs, total: 39 µs
Wall time: 35 µs

In [46]:
%time img3 = np.copy(img1)


CPU times: user 29 µs, sys: 8 µs, total: 37 µs
Wall time: 38.9 µs

In [47]:
img2 = np.copy(img1)

In [48]:
img2[compound_ind] = 0
imshow(img2)


Out[48]:
<matplotlib.image.AxesImage at 0x122fac990>

In [49]:
img2


Out[49]:
array([[ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  0.,  0.,  0.,  9.,  9.,  9.,  9.,  9.,  9.,
         0.,  0.,  0.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  0.,  0.,  0.,  9.,  9.,  9.,  9.,  9.,  9.,
         0.,  0.,  0.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  0.,  0.,  0.,  9.,  9.,  9.,  9.,  9.,  9.,
         0.,  0.,  0.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  0.,  0.,  0.,  9.,  9.,  9.,  9.,  9.,  9.,
         0.,  0.,  0.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  0.,  0.,  0.,  9.,  9.,  9.,  9.,  9.,  9.,
         0.,  0.,  0.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  0.,  0.,  0.,  9.,  9.,  9.,  9.,  9.,  9.,
         0.,  0.,  0.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,
         3.,  3.,  3.,  3.,  3.,  3.,  3.]])

In [50]:
ind3 = img1 == 9
ind4 = compound_ind & ind3

In [51]:
img3 = np.copy(img1)

In [52]:
img3[ind3] = 1

In [53]:
imshow(img3)


Out[53]:
<matplotlib.image.AxesImage at 0x1230d1550>

In [11]:
import numpy.random as rand

In [12]:
print rand.randn.__doc__


        randn(d0, d1, ..., dn)

        Return a sample (or samples) from the "standard normal" distribution.

        If positive, int_like or int-convertible arguments are provided,
        `randn` generates an array of shape ``(d0, d1, ..., dn)``, filled
        with random floats sampled from a univariate "normal" (Gaussian)
        distribution of mean 0 and variance 1 (if any of the :math:`d_i` are
        floats, they are first converted to integers by truncation). A single
        float randomly sampled from the distribution is returned if no
        argument is provided.

        This is a convenience function.  If you want an interface that takes a
        tuple as the first argument, use `numpy.random.standard_normal` instead.

        Parameters
        ----------
        d0, d1, ..., dn : int, optional
            The dimensions of the returned array, should be all positive.
            If no argument is given a single Python float is returned.

        Returns
        -------
        Z : ndarray or float
            A ``(d0, d1, ..., dn)``-shaped array of floating-point samples from
            the standard normal distribution, or a single such float if
            no parameters were supplied.

        See Also
        --------
        random.standard_normal : Similar, but takes a tuple as its argument.

        Notes
        -----
        For random samples from :math:`N(\mu, \sigma^2)`, use:

        ``sigma * np.random.randn(...) + mu``

        Examples
        --------
        >>> np.random.randn()
        2.1923875335537315 #random

        Two-by-four array of samples from N(3, 6.25):

        >>> 2.5 * np.random.randn(2, 4) + 3
        array([[-4.49401501,  4.00950034, -1.81814867,  7.29718677],  #random
               [ 0.39924804,  4.68456316,  4.99394529,  4.84057254]]) #random

        

In [32]:
a = rand.randn(100)

In [33]:
ind = a > 0.2
b = a[ind]

In [34]:
b = b**2 - 10

In [35]:
a[ind] = b
imshow(a.reshape((10, 10)))


Out[35]:
<matplotlib.image.AxesImage at 0x106b0b750>

Text Files


In [60]:
%%file test.txt
XR21 32.789 1
XR22 33.091 2


Overwriting test.txt

In [61]:
table = np.loadtxt('test.txt', dtype = {'names': ('ID', 'Result', 'Type'), 'formats':('S4', 'f4', 'i2')})

In [62]:
table


Out[62]:
array([('XR21', 32.78900146484375, 1), ('XR22', 33.090999603271484, 2)], 
      dtype=[('ID', 'S4'), ('Result', '<f4'), ('Type', '<i2')])

Binary Files


In [63]:
data = np.empty((1000, 1000))

In [64]:
np.save("test.npy", data) # Save as binary data for a np.array

In [65]:
np.savez("test.npz", data) # Save as compressed binary data.

In [66]:
!tree .


.
├── LICENSE.rst
├── Numpy_Learn2.ipynb
├── Numpy_SciPy_Learn1.ipynb
├── README.md
├── SciPy_Learn2.ipynb
├── binning.png
├── polynomials.png
├── python_examples
│   ├── numpy_211_exs.py
│   ├── numpy_212_exs.py
│   ├── numpy_213_exs.py
│   ├── numpy_21_ex1.py
│   ├── numpy_22_ex1.py
│   ├── numpy_22_ex2.py
│   ├── numpy_231_ex1.py
│   ├── numpy_231_ex2.py
│   ├── numpy_231_ex3.py
│   ├── numpy_232_exs.py
│   ├── numpy_241_ex1.py
│   ├── numpy_241_ex2.py
│   ├── numpy_241_ex3.py
│   ├── numpy_241_ex4.py
│   ├── numpy_241_exs.py
│   ├── numpy_243_ex1.py
│   ├── numpy_243_exs2.py
│   ├── numpy_244_exs.py
│   ├── scikits_411_ex1.py
│   ├── scikits_412_ex1.py
│   ├── scikits_412_ex2.py
│   ├── scikits_421_ex1.py
│   ├── scikits_422_ex1.py
│   ├── scipy_311_ex1.py
│   ├── scipy_311_ex2.py
│   ├── scipy_311_ex3.py
│   ├── scipy_312_ex1.py
│   ├── scipy_312_ex2.py
│   ├── scipy_32_ex1.py
│   ├── scipy_32_ex2.py
│   ├── scipy_32_ex3.py
│   ├── scipy_32_ex4.py
│   ├── scipy_331_ex1.py
│   ├── scipy_332_ex1.py
│   ├── scipy_341_ex1.py
│   ├── scipy_341_ex2.py
│   ├── scipy_341_ex3.py
│   ├── scipy_342_ex1.py
│   ├── scipy_34_ex1.py
│   ├── scipy_351_ex1.py
│   ├── scipy_352_ex1.py
│   ├── scipy_352_ex2.py
│   ├── scipy_36_ex1.py
│   ├── scipy_36_ex2.py
│   ├── scipy_36_space_download.py
│   └── scipy_37_ex1.py
├── test.npy
├── test.npz
└── test.txt

1 directory, 56 files

In [67]:
restored_data = np.load("test.npy")

In [68]:
restored_data


Out[68]:
array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ..., 
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]])

In [69]:
arr = rand.randint(0, 100, 10)

In [70]:
arr.sort() # sort method will modify the original array

In [71]:
arr


Out[71]:
array([39, 48, 61, 64, 71, 72, 72, 82, 84, 98])

In [72]:
arr2 = np.random.rand(100)

In [73]:
arr2[5] = np.nan

In [74]:
print (np.max(arr2))


nan

In [75]:
print (np.nanmax(arr2))


0.97145792849

In [75]:


In [76]:
array = np.random.rand(10000).reshape((100, 100))

In [77]:
rind = np.random.randint(2, size=(100, 100)) == 1

In [78]:
array[rind] = np.nan

In [79]:
array


Out[79]:
array([[ 0.57090172,         nan,  0.90072508, ...,  0.77279258,
                nan,         nan],
       [        nan,         nan,         nan, ...,  0.07743289,
         0.55706942,  0.66799649],
       [        nan,  0.15451591,  0.01014619, ...,         nan,
         0.60096717,  0.52515727],
       ..., 
       [ 0.12928502,  0.45773371,  0.05671592, ...,  0.25737227,
         0.86250029,         nan],
       [ 0.21773123,         nan,         nan, ...,         nan,
                nan,  0.6018319 ],
       [ 0.3261015 ,         nan,  0.44136562, ...,         nan,
         0.39676067,         nan]])

In [80]:
not_nan = ~np.isnan(array)

In [81]:
not_nan


Out[81]:
array([[ True, False,  True, ...,  True, False, False],
       [False, False, False, ...,  True,  True,  True],
       [False,  True,  True, ..., False,  True,  True],
       ..., 
       [ True,  True,  True, ...,  True,  True, False],
       [ True, False, False, ..., False, False,  True],
       [ True, False,  True, ..., False,  True, False]], dtype=bool)

In [82]:
print np.std(array[not_nan])


0.290903397815

Linear Algebra

Solving:

$$ \left[ \begin{array}{ccc} 3 & 6 & -5 \\ 1 & -3 & 2 \\ 5 & -1 & 4 \\ \end{array} \right] \left[ \begin{array}{c} x \\ y \\ z \\ \end{array} \right] = \left [ \begin{array}{c} 12 \\ -2 \\ 10 \end{array} \right ] $$

In [83]:
A = np.matrix([[3, 6, -5], [1, -3, 2], [5, -1, 4]])

In [84]:
B = np.matrix([[12], [-2], [10]])

In [85]:
X = A**(-1) * B

In [86]:
X


Out[86]:
matrix([[ 1.75],
        [ 1.75],
        [ 0.75]])

In [87]:
A * X


Out[87]:
matrix([[ 12.],
        [ -2.],
        [ 10.]])

Other way to solve above linear system with numpy (but more efficient).


In [88]:
A = np.array([[3, 6, -5], [1, -3, 2], [5, -1, 4]])

B = np.array([[12], [-2], [10]])

In [89]:
X = np.linalg.inv(A).dot(B)

In [90]:
X


Out[90]:
array([[ 1.75],
       [ 1.75],
       [ 0.75]])

In [90]:


In [91]:
a = np.random.rand(10)
print a
np.random.permutation(a) # premutation will not affect a


[ 0.57330679  0.32483134  0.39162488  0.88094255  0.31214181  0.34087541
  0.21404871  0.71094909  0.71366721  0.54751866]
Out[91]:
array([ 0.21404871,  0.39162488,  0.32483134,  0.88094255,  0.71094909,
        0.34087541,  0.71366721,  0.54751866,  0.57330679,  0.31214181])

In [92]:
a


Out[92]:
array([ 0.57330679,  0.32483134,  0.39162488,  0.88094255,  0.31214181,
        0.34087541,  0.21404871,  0.71094909,  0.71366721,  0.54751866])

In [93]:
np.random.shuffle(a) # shuffle will affect a.

In [94]:
a


Out[94]:
array([ 0.71094909,  0.32483134,  0.39162488,  0.88094255,  0.31214181,
        0.54751866,  0.21404871,  0.34087541,  0.57330679,  0.71366721])

In [95]:
def temp_and_rain(lat, min_temp = 10, max_temp = 10, points = 1000):
    lat = np.abs(lat)
    temp = np.random.normal(scale = 5, size = lat.shape) + lat/2
    temp = np.abs(temp)
    
    tobe_ornot = np.random.randint(0, high = 2)
    print tobe_ornot
    
    if tobe_ornot:
        rain = np.random.normal(scale = 3, size=lat.shape) + temp * 2
    else:
        rain = np.random.uniform(high = 100, size = lat.shape)
        
    return temp, rain

In [96]:
lat = np.random.uniform(low = -90, high = 90, size = 100)
temp, rain = temp_and_rain(lat)


0

In [97]:
result = np.corrcoef(temp, rain)

In [98]:
result


Out[98]:
array([[ 1.        , -0.11384718],
       [-0.11384718,  1.        ]])

In [99]:
temp, rain = temp_and_rain(lat)


0

In [100]:
result = np.corrcoef(temp, rain)
result


Out[100]:
array([[ 1.        ,  0.04578089],
       [ 0.04578089,  1.        ]])

In [101]:
print np.histogram2d.__doc__


    Compute the bi-dimensional histogram of two data samples.

    Parameters
    ----------
    x : array_like, shape (N,)
        An array containing the x coordinates of the points to be
        histogrammed.
    y : array_like, shape (N,)
        An array containing the y coordinates of the points to be
        histogrammed.
    bins : int or [int, int] or array_like or [array, array], optional
        The bin specification:

          * If int, the number of bins for the two dimensions (nx=ny=bins).
          * If [int, int], the number of bins in each dimension
            (nx, ny = bins).
          * If array_like, the bin edges for the two dimensions
            (x_edges=y_edges=bins).
          * If [array, array], the bin edges in each dimension
            (x_edges, y_edges = bins).

    range : array_like, shape(2,2), optional
        The leftmost and rightmost edges of the bins along each dimension
        (if not specified explicitly in the `bins` parameters):
        ``[[xmin, xmax], [ymin, ymax]]``. All values outside of this range
        will be considered outliers and not tallied in the histogram.
    normed : bool, optional
        If False, returns the number of samples in each bin. If True,
        returns the bin density ``bin_count / sample_count / bin_area``.
    weights : array_like, shape(N,), optional
        An array of values ``w_i`` weighing each sample ``(x_i, y_i)``.
        Weights are normalized to 1 if `normed` is True. If `normed` is
        False, the values of the returned histogram are equal to the sum of
        the weights belonging to the samples falling into each bin.

    Returns
    -------
    H : ndarray, shape(nx, ny)
        The bi-dimensional histogram of samples `x` and `y`. Values in `x`
        are histogrammed along the first dimension and values in `y` are
        histogrammed along the second dimension.
    xedges : ndarray, shape(nx,)
        The bin edges along the first dimension.
    yedges : ndarray, shape(ny,)
        The bin edges along the second dimension.

    See Also
    --------
    histogram : 1D histogram
    histogramdd : Multidimensional histogram

    Notes
    -----
    When `normed` is True, then the returned histogram is the sample
    density, defined such that the sum over bins of the product
    ``bin_value * bin_area`` is 1.

    Please note that the histogram does not follow the Cartesian convention
    where `x` values are on the abscissa and `y` values on the ordinate
    axis.  Rather, `x` is histogrammed along the first dimension of the
    array (vertical), and `y` along the second dimension of the array
    (horizontal).  This ensures compatibility with `histogramdd`.

    Examples
    --------
    >>> import matplotlib as mpl
    >>> import matplotlib.pyplot as plt

    Construct a 2D-histogram with variable bin width. First define the bin
    edges:

    >>> xedges = [0, 1, 1.5, 3, 5]
    >>> yedges = [0, 2, 3, 4, 6]

    Next we create a histogram H with random bin content:

    >>> x = np.random.normal(3, 1, 100)
    >>> y = np.random.normal(1, 1, 100)
    >>> H, xedges, yedges = np.histogram2d(y, x, bins=(xedges, yedges))

    Or we fill the histogram H with a determined bin content:

    >>> H = np.ones((4, 4)).cumsum().reshape(4, 4)
    >>> print H[::-1]  # This shows the bin content in the order as plotted
    [[ 13.  14.  15.  16.]
     [  9.  10.  11.  12.]
     [  5.   6.   7.   8.]
     [  1.   2.   3.   4.]]

    Imshow can only do an equidistant representation of bins:

    >>> fig = plt.figure(figsize=(7, 3))
    >>> ax = fig.add_subplot(131)
    >>> ax.set_title('imshow: equidistant')
    >>> im = plt.imshow(H, interpolation='nearest', origin='low',
                    extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])

    pcolormesh can display exact bin edges:

    >>> ax = fig.add_subplot(132)
    >>> ax.set_title('pcolormesh: exact bin edges')
    >>> X, Y = np.meshgrid(xedges, yedges)
    >>> ax.pcolormesh(X, Y, H)
    >>> ax.set_aspect('equal')

    NonUniformImage displays exact bin edges with interpolation:

    >>> ax = fig.add_subplot(133)
    >>> ax.set_title('NonUniformImage: interpolated')
    >>> im = mpl.image.NonUniformImage(ax, interpolation='bilinear')
    >>> xcenters = xedges[:-1] + 0.5 * (xedges[1:] - xedges[:-1])
    >>> ycenters = yedges[:-1] + 0.5 * (yedges[1:] - yedges[:-1])
    >>> im.set_data(xcenters, ycenters, H)
    >>> ax.images.append(im)
    >>> ax.set_xlim(xedges[0], xedges[-1])
    >>> ax.set_ylim(yedges[0], yedges[-1])
    >>> ax.set_aspect('equal')
    >>> plt.show()

    

In [102]:
import matplotlib.pyplot as plt

# Generate 2 by 1000000 array by normal dist and assign rows to x, y respectively.
x, y = np.random.randn(2, 1000000)

img, xedges, yedges = np.histogram2d(x, y, bins = (20, 20))

# For imshow. (Define the extend of x and y axis)
extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]]

fig = plt.figure(figsize = (10, 5))
ax1 = fig.add_subplot(121) # 1 by 2 plot's subplot 1
ax2 = fig.add_subplot(122) # 1 by 2 plot's subplot 2

yarg = plt.cm.gist_yarg # plt.cm: color maps module

ax1.imshow(img, extent = extent, interpolation='nearest', cmap=yarg, origin='lower left')
ax2.hexbin(x, y, gridsize=20, cmap=yarg)

ax1.set_title("Rectangular binning")
ax2.set_title("Hexagonal binning")

ax1.set_xlim(-4, 4)
ax1.set_ylim(-4, 4)
ax2.set_xlim(-4, 4)
ax2.set_ylim(-4, 4)

ax1.set_aspect("equal")
ax2.set_aspect("equal")



In [104]:
fig.savefig("binning.png")

In [104]:

Polynomial


In [105]:
from numpy.polynomial import polynomial as P

In [106]:
x = np.linspace(0, 8 * np.pi, 50)
y1 = np.cos(x)
y2 = np.sin(x - np.pi)

In [107]:
print P.polyroots.__doc__


    Compute the roots of a polynomial.

    Return the roots (a.k.a. "zeros") of the polynomial

    .. math:: p(x) = \sum_i c[i] * x^i.

    Parameters
    ----------
    c : 1-D array_like
        1-D array of polynomial coefficients.

    Returns
    -------
    out : ndarray
        Array of the roots of the polynomial. If all the roots are real,
        then `out` is also real, otherwise it is complex.

    See Also
    --------
    chebroots

    Notes
    -----
    The root estimates are obtained as the eigenvalues of the companion
    matrix, Roots far from the origin of the complex plane may have large
    errors due to the numerical instability of the power series for such
    values. Roots with multiplicity greater than 1 will also show larger
    errors as the value of the series near such points is relatively
    insensitive to errors in the roots. Isolated roots near the origin can
    be improved by a few iterations of Newton's method.

    Examples
    --------
    >>> import numpy.polynomial.polynomial as poly
    >>> poly.polyroots(poly.polyfromroots((-1,0,1)))
    array([-1.,  0.,  1.])
    >>> poly.polyroots(poly.polyfromroots((-1,0,1))).dtype
    dtype('float64')
    >>> j = complex(0,1)
    >>> poly.polyroots(poly.polyfromroots((-j,0,j)))
    array([  0.00000000e+00+0.j,   0.00000000e+00+1.j,   2.77555756e-17-1.j])

    

In [108]:
coeff, stats = P.polyfit(x, y1, 20, full = True)
roots = np.real(P.polyroots(coeff))
fit = P.Polynomial(coeff)
yf1 = fit(x)

In [109]:
fig = plt.figure(figsize = (10, 5))

ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

ax1.plot(x, y1)
ax2.plot(x, yf1)

ax1.set_title("Original Data")
ax2.set_title("Fitting Date")


Out[109]:
<matplotlib.text.Text at 0x12b2b9b10>

In [110]:
print P.polyder.__doc__


    Differentiate a polynomial.

    Returns the polynomial coefficients `c` differentiated `m` times along
    `axis`.  At each iteration the result is multiplied by `scl` (the
    scaling factor is for use in a linear change of variable).  The
    argument `c` is an array of coefficients from low to high degree along
    each axis, e.g., [1,2,3] represents the polynomial ``1 + 2*x + 3*x**2``
    while [[1,2],[1,2]] represents ``1 + 1*x + 2*y + 2*x*y`` if axis=0 is
    ``x`` and axis=1 is ``y``.

    Parameters
    ----------
    c : array_like
        Array of polynomial coefficients. If c is multidimensional the
        different axis correspond to different variables with the degree
        in each axis given by the corresponding index.
    m : int, optional
        Number of derivatives taken, must be non-negative. (Default: 1)
    scl : scalar, optional
        Each differentiation is multiplied by `scl`.  The end result is
        multiplication by ``scl**m``.  This is for use in a linear change
        of variable. (Default: 1)
    axis : int, optional
        Axis over which the derivative is taken. (Default: 0).

        .. versionadded:: 1.7.0

    Returns
    -------
    der : ndarray
        Polynomial coefficients of the derivative.

    See Also
    --------
    polyint

    Examples
    --------
    >>> from numpy.polynomial import polynomial as P
    >>> c = (1,2,3,4) # 1 + 2x + 3x**2 + 4x**3
    >>> P.polyder(c) # (d/dx)(c) = 2 + 6x + 12x**2
    array([  2.,   6.,  12.])
    >>> P.polyder(c,3) # (d**3/dx**3)(c) = 24
    array([ 24.])
    >>> P.polyder(c,scl=-1) # (d/d(-x))(c) = -2 - 6x - 12x**2
    array([ -2.,  -6., -12.])
    >>> P.polyder(c,2,-1) # (d**2/d(-x)**2)(c) = 6 + 24x
    array([  6.,  24.])

    

In [111]:
## Differentiating
new_coeff = P.polyder(coeff)
dfit = P.Polynomial(new_coeff)
yf2 = dfit(x)

In [129]:
fig = plt.figure(figsize=(8, 6))

ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

ax1.set_title("Data and fitting")
ax2.set_title("Differentiation")

ax1.set_xlim(0, x.max())
ax2.set_xlim(0, x.max())
ax1.set_ylim(-1.25, 1.25)
ax2.set_ylim(-1.25, 1.25)

ax1.plot(x, yf1, label="Fitted", color="gray")
ax1.scatter(x, y1, label="Original", edgecolor="none", facecolor = "red")
ax1.scatter(roots, np.zeros(roots.shape), label="Roots", edgecolor="black", facecolor="none", s=50)
ax1.legend(loc=0)

ax2.plot(x, yf2, label="Differentiated", color="gray")
ax2.scatter(x, y2, label="Original", edgecolor = "none", facecolor="red")
ax2.legend(loc=1)


Out[129]:
<matplotlib.legend.Legend at 0x12c81f650>

In [113]:
fig.savefig("polynomials.png")

In [ ]: