Numpy...why so serious?

What can we really do with Numpy? Why should we use it at all ?

Start with : import numpy

1. We can create arrays .


method: numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)


>>> a = numpy.array(range(10))
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a.dtype
dtype('int32')

In the above array, there are 10 columns and 1 row.

Note : The concept of rows and columns applies when you have a 2D array. However, the array numpy.array([1,2,3,4]) is a 1D array and so has only one dimension, therefore shape rightly returns a single valued iterable.

Refer this link

2. We can create/convert array of a particular type (immediate conversion of your list to a desired type)



>>> s = numpy.array(range(10), dtype=str)
>>> s
array(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
      dtype='|S1')

---------------------------------------------------------------------------------------

>>> s = numpy.array(range(10), dtype=float)
>>> s
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])

3. We can change the dimensions of the array anytime easily.



>>> s = numpy.array(range(10), dtype=float)
>>> s
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])
>>> s.shape
(10L,)
>>> s.shape = (2,5)
>>> s
array([[ 0.,  1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.,  9.]])
>>> s.shape = (5,2)
>>> s
array([[ 0.,  1.],
       [ 2.,  3.],
       [ 4.,  5.],
       [ 6.,  7.],
       [ 8.,  9.]])
>>> s.shape
(5L, 2L)

You can also use the reshape method. This does not change the array but just returns the changed output. Whereas, obj.shape will change the shape permanently.

4. We can transpose the array. 


Well. We can actually create a copy . Not save in-place. numpy.transpose or obj.transpose


>>> s
array([[ 0.,  1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.,  9.]])
>>> s.transpose()
array([[ 0.,  5.],
       [ 1.,  6.],
       [ 2.,  7.],
       [ 3.,  8.],
       [ 4.,  9.]])
>>> s
array([[ 0.,  1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.,  9.]])

5. We can flatten any array with  and get back a single dimension (one column) array. 



>>> s
array([[ 0.,  1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.,  9.]])
>>> s.flatten()
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])
>>> s
array([[ 0.,  1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.,  9.]])

Note : This will again not save the array in-place.

6. We can concatenate two or more arrays using the method 'numpy.concatenate((arr1, arr2, .....))'.



>>> s = numpy.array(range(5))
>>> s1 = numpy.array(range(6,11))
>>> numpy.concatenate((s,s1))
array([ 0,  1,  2,  3,  4,  6,  7,  8,  9, 10])

Above are example of single dimension arrays. If we have arrays of multi-dimension, we can also mention the axis.

Note: By default, axis=0 . It means, all the elements of each array will be appended to create one single same dimension array.


>>> arr1
array([[1, 2, 3],
       [4, 5, 6]])
>>> arr2
array([[11, 12, 13],
       [14, 15, 16]])

>>> numpy.concatenate((arr1, arr1))
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3],
       [4, 5, 6]])

#This shows that axis=0 is the default
>>> numpy.concatenate((arr1, arr2), axis=0)
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [11, 12, 13],
       [14, 15, 16]])

#This shows that axis=1 will add corresponding index items together
>>> numpy.concatenate((arr1, arr2), axis=1)
array([[ 1,  2,  3, 11, 12, 13], # [1 2 3] + [11 12 13]
       [ 4,  5,  6, 14, 15, 16]]) # [4 5 6] + [14 15 16]

#Note that mentioning other axis will raise
#an error since 0 and 1 are possible axis
#values for a two dimensional array.

>>> numpy.concatenate((arr1, arr2), axis=2)

Traceback (most recent call last):
  File "<pyshell#131>", line 1, in <module>
    numpy.concatenate((arr1, arr2), axis=2)
AxisError: axis 2 is out of bounds for array of dimension 2

Things to remember at this junction.


You can anytime get a normal (python) list from a numpy object by calling the associated method obj.tolist()



>>> narr = numpy.array(range(5))
>>> narr
array([0, 1, 2, 3, 4])
>>> narr.tolist()
[0, 1, 2, 3, 4]

In case you have floats in your array and you want to round all members, the associated method with the array object narr "obj.round(decimals=0, out=None)" can be used .


>>> narr
array([  1.5,   1. ,   4.5,   6. ,  20.5,  45.7,   1. ,   1.5])
>>> narr.round()
array([  2.,   1.,   4.,   6.,  20.,  46.,   1.,   2.])


7. We can create an array with only zeros or ones.



>>> numpy.zeros(4)
array([ 0.,  0.,  0.,  0.])

>>> numpy.zeros(9)
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
# Above are examples of 1-D arrays zero filled

>>> numpy.zeros((2,3))
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
#Above is an example of 2-D array with 2 rows and 3 columns

#Below are the same examples for ones

>>> numpy.ones((2,3))
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
>>> numpy.ones((4))
array([ 1.,  1.,  1.,  1.])
>>> numpy.ones(4)
array([ 1.,  1.,  1.,  1.])

8. We can create identity matrices while choosing the eye of the matrix. 



>>> numpy.identity(1)
array([[ 1.]])
>>> numpy.identity(2)
array([[ 1.,  0.],
       [ 0.,  1.]])
>>> numpy.identity(3)
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> numpy.identity(4)
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])

>>> numpy.eye(4, k=1)
array([[ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.]])

>>> numpy.eye(4, k=-1)
array([[ 0.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.]])

>>> numpy.eye(4, 3, k=-1)
array([[ 0.,  0.,  0.],
       [ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])


9.  Numpy Help

Numpy has its own way of providing help info for its functions.

numpy.info(numpy.ndarray)

Syntax:


numpy.info(object=None, maxwidth=76, output=<idlelib.PyShell.PseudoOutputFile object>, toplevel='numpy')

As you can see, it allows certain arguments . maxwidth can set the width of the print output.You can choose the output too (Default is stdout).  

Popular posts from this blog

Why should you visit Kashmir sooner?

CNTLM in Office

How to run a Python program over the internet?