Type | Description |
---|---|

bool | Boolean (True or False) stored as a bit |

inti | Platform integer (normally either int32 or int64) |

int8 | Byte (-128 to 127) |

int16 | Integer (-32768 to 32767) |

int32 | Integer (-2 ** 31 to 2 ** 31 -1) |

int64 | Integer (-2 ** 63 to 2 ** 63 -1) |

uint8 | Unsigned integer (0 to 255) |

uint16 | Unsigned integer (0 to 65535) |

uint32 | Unsigned integer (0 to 2 ** 32 - 1) |

uint64 | Unsigned 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 |

float64 | or float Double precision float: sign bit, 11 bits exponent,
52 bits mantissa |

complex64 | Complex 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)

>>> np.arange(7,dtype='uint16')

array([0, 1, 2, 3, 4, 5, 6], dtype=uint16)

Let's walk quickly through some basic NumPy examples.

>>> import numpy

>>> numpy.version.full_version

'1.11.1'

>>> numpy.version.full_version

'1.11.1'

>>> 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,......).
>>> a = np.array([0,1,2,3,4,5])

>>> a

array([0, 1, 2, 3, 4, 5])

>>> 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)

>>>

>>> 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)

>>>

>>> 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)

>>>

>>> 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[1][0] = 89

>>> c

array([[ 0, 1],

[89, 3],

[ 4, 5]])

>>> a

array([ 0, 1, 89, 3, 4, 5])

>>>

>>>>>> c[1][0] = 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

>>> import numpy as np

>>> c = a.reshape((3,2)).copy()

>>> c

array([[ 0, 1],

[89, 3],

[ 4, 5]])

>>> c[0][1] = 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.
>>> c = a.reshape((3,2)).copy()

>>> c

array([[ 0, 1],

[89, 3],

[ 4, 5]])

>>> c[0][1] = 90

>>> c

array([[ 0, 90],

[89, 3],

[ 4, 5]])

>>> a

array([ 0, 1, 89, 3, 4, 5])

>>>

>>> 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])

>>>

>>> 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])

>>>