cloud

# Python Numpy

Sometimes it is very difficult to install NumPy, SciPy, and matplotlib. If you are unsure about the installation process, you might want to install Anaconda Python distribution. Python has an integer type, a float type, and a complex type; however, this is not enough for scientific computing and, for this reason, NumPy has a lot more data types with varying precision, dependent on memory requirements.

TypeDescription
boolBoolean (True or False) stored as a bit
inti Platform integer (normally either int32 or int64)
int8Byte (-128 to 127)
int16Integer (-32768 to 32767)
int32 Integer (-2 ** 31 to 2 ** 31 -1)
int64Integer (-2 ** 63 to 2 ** 63 -1)
uint8Unsigned integer (0 to 255)
uint16 Unsigned integer (0 to 65535)
uint32Unsigned integer (0 to 2 ** 32 - 1)
uint64Unsigned integer (0 to 2 ** 64 - 1)
float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
float64or float Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
complex64Complex number, represented by two 32-bit floats (real and imaginary components)complex128 or complex Complex number, represented by two 64-bit floats (real and imaginary components)

Many functions have a data type argument, which is often optional:
>>> import numpy as np
>>> np.arange(7,dtype='uint16')
array([0, 1, 2, 3, 4, 5, 6], dtype=uint16)

Let's walk quickly through some basic NumPy examples.

### Example 1:

>>> import numpy
>>> numpy.version.full_version
'1.11.1'

### Example 2:

>>> import numpy as np
>>> a = np.array([0,1,2,3,4,5])
>>> a
array([0, 1, 2, 3, 4, 5])
Remember, use [0,1,.....] not (0,1,......).

To get the dimension and shape of array

### Example 3:

>>> import numpy as np
>>> a = np.array([0,1,2,3,4,5])
>>> b = np.array([[0,1,2],[3,4,5],[6,7,8]])
>>> a
array([0, 1, 2, 3, 4, 5])
>>> b
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> a.ndim
1
>>> b.ndim
2
>>>
>>> a.shape
(6L,)
>>>
>>> b.shape
(3L, 3L)
>>>

### Example 4:

We can now transform this array to a two-dimensional matrix:
>>> import numpy as np
>>> a = np.array([0,1,2,3,4,5])
>>> c = a.reshape((3,2))
>>> c
array([[0, 1],
[2, 3],
[4, 5]])
>>>
>>> c.ndim
2
>>> c.shape
(3L, 2L)
>>>

Let us see the funny thing, and optimization of numpy packages.
>>> import numpy as np
>>>>>> c = 89
>>> c
array([[ 0, 1],
[89, 3],
[ 4, 5]])
>>> a
array([ 0, 1, 89, 3, 4, 5])
>>>

In this case, we have modified value 2 to 77 in c, and at the same time the same change reflected in a as well. Just to sure that whenever you need a true copy, you can always perform:
>>> import numpy as np
>>> c = a.reshape((3,2)).copy()
>>> c
array([[ 0, 1],
[89, 3],
[ 4, 5]])
>>> c = 90
>>> c
array([[ 0, 90],
[89, 3],
[ 4, 5]])
>>> a
array([ 0, 1, 89, 3, 4, 5])
>>>
Note that here, c and a are totally independent copies.

### Example 5:

The operation performed on numpy array are propagated to the individual elements.
>>> import numpy as np
>>> d = np.array([1,2,3,4,5])
>>> d*2
array([ 2, 4, 6, 8, 10])
>>>
>>> d**2
array([ 1, 4, 9, 16, 25])
>>>
>>> d
array([1, 2, 3, 4, 5])
>>>