Python Numpy Tutorial #1
Numpy is the major library for scientific computing in Python.NumPy enriches the programming language Python with powerful data structures for efficient computation of multi-dimensional arrays and matrices. If you are familiar with Octave or Matlab, you will find this tutorial easy.
NB: Numpy can be imported using
import numpy as np
Arrays: You can create an array as follows:
a = np.array([1,2,3,4]) #creates a 1x 4 array b = np.array([[1,2,3],[1,2,3]]) #creates 2x3 array
Along with the array, some important parameters taken by np.array() are:
1. dtype: It helps you to pass the desired datatype of the array. The permitted datatypes are -> int8, int16, int32, int64, float16, float32, float64, complex64 and complex128. Int is for integers, float can store real numbers and complex can store complex numbers. The number represents the bit of data stored, for example, int16 represents a 16-bit int. You can pass the data type by :
a = np.array([1,2,3,4] dtype = complex)
You can create custom datatype using:
import numpy as np student = np.dtype([('age','int32' ),('money','float32')]) a = np.array([('32','500.16'), ('45', '4000.25')], dtype = student) print a
The output would be:
[(32, 500.16) (45, 4000.25)]
You can view the type of your array using :
a.dtype
where a is the array name.
2. ndmin: Specifies minimum dimension of the array. Use it as :
a = np.array([1,2,3], ndmin = 2)
It would create the array as :
[[1 2 3]]
It also allows you to create some special array such as:
a = np.zeros((2,2)) # Create an array of all zeros b = np.ones((1,2)) # Create an array of all ones c = np.full((2,2), 7) # Create a constant array d = np.eye(2) # Create a 2x2 identity matrix e = np.random.random((2,2)) # Create an array filled with random values
The ndmin of an array can view using:
a.ndmin
Array Indexing: Arrays can be indexed in several ways :
1. Individual elements: Individual elements can be indexed using :
import numpy as np a = np.array([[1,2,3], [5,6,7], [9,10,11]]) a[0, 1] #it will print the element at index 1 of 0th coloumn
2. Slicing :
import numpy as np a = np.array([[1,2,3], [5,6,7], [9,10,11]]) b = a[:2, 1:3] #it slices/ creates a subarray the array as #[[2 3] [6 7]] #taking the elements from index 1 to 3 of coloums 0 to 2
3. Individual rows and columns can be accessed as :
import numpy as np a = np.array([[1,2,3], [5,6,7], [9,10,11]]) a[0,:] #it will display the 0th row a[:,0] #it will display the 0th column
4. Two or more rows and columns can be accessed as:
import numpy as np a = np.array([[1,2,3], [5,6,7], [9,10,11]]) a[0:3,:] #it will display from 0th row to 3rd row a[:, 1:3] #it will display the 0th column to 3rd coloumn
.
5. Array indexing with conditions :
import numpy as np a = np.array([[1,2,3], [5,6,7], [9,10,11]]) a[a>5] #returns a 1d array containing the values from a which are greater than a #the condition specified can be >, <, >=, <=, =
The first part of the tutorial finishes her. Read the next part of the tutorial from here.
Comments
Post a Comment