Numpy notes

From raju

numpy arrays as cells of a dataframe

    >>> import numpy as np
    >>> import pandas as pd
    >>> x = np.array([2,3,1,0]); y = np.array([1,2,0,4])
    >>> df = pd.DataFrame({'a':[x,y], 'tradeid':['usd','eur']})
    
    >>> df
                  a tradeid
    0  [2, 3, 1, 0]     usd
    1  [1, 2, 0, 4]     eur
    
    >>> df.set_index('tradeid')
                        a
    tradeid
    usd      [2, 3, 1, 0]
    eur      [1, 2, 0, 4]
    

demonstrates | how to create a numpy array

tags | numpy array as element of a dataframe

convert list to numpy array

    >>> import numpy as np
    >>> a = [2, -3, 4]
    >>> b = np.array(a)
    >>> a
    [2, -3, 4]
    >>> b
    array([ 2, -3,  4])
    >>> type(a)
    <class 'list'>
    >>> type(b)
    <class 'numpy.ndarray'>
    >>> c = np.cumsum(b)
    >>> c
    array([ 2, -1,  3], dtype=int32)
    >>> type(c)
    <class 'numpy.ndarray'>
    

tested using Python 3.6.3

2-D arrays

    In[34]: m = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
    In[35]: m
    Out[36]: 
    array([[1, 2, 3, 4],
           [5, 6, 7, 8]])
    
    In[36]: m[0]
    Out[37]: 
    array([1, 2, 3, 4])
    
    In[37]: m[1]
    Out[38]: 
    array([5, 6, 7, 8])
    
    In[38]: m[1][2]
    Out[39]: 
    7
    

iterate over 2D arrays using map

    $cat numpy_2d.py
    
    import numpy as np
    m = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]);
    n = np.array([[9, 10, 11, 12], [13, 14, 15, 16]])
    
    print "m = \n", m, "\n"
    print "n = \n", n, "\n"
    
    def a(b,c):
        print "b = ", b, ", c = ", c
    
    map(a, m, n)
    
    $python2 numpy_2d.py
    m =
    [[1 2 3 4]
     [5 6 7 8]]
    
    n =
    [[ 9 10 11 12]
     [13 14 15 16]]
    
    b =  [1 2 3 4] , c =  [ 9 10 11 12]
    b =  [5 6 7 8] , c =  [13 14 15 16]
    

random integers within a range

numpy.random.randint(low, high=None, size=None, dtype='l')

Return random integers from low (inclusive) to high (exclusive).

Ref:- https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.random.randint.html