Numpy with python
Welcome back!!!
- Numpy overview
- Creating ND-arrays
- Class and attributes of ND-arrays object
- Operation between array and scalar
- Basic, Boolean and fancy indexing
- Universal functions
- File input and output with arrays
- Broadcasting
-
NumPy is an acronym for "Numeric Python" or "Numerical Python".
- It is an open-source extension module for Python, which provides fast precompiled functions for mathematical and numerical routines
- NumPy enriches the programming language Python with powerful data structures for efficient computation of multi-dimensional arrays and matrices.
- The implementation is even aiming at huge matrices and arrays.
- The module supplies a large library of high-level mathematical functions to operate on these matrices and arrays.
import numpy as np cvalues=[25.3,32.3,42.6] print(cvalues) print(type(cvalues)) Output=[25.3, 32.3, 42.6] class 'list'> fvalues=[] for x in cvalues: fvalues.append(x*9/5+32) print(fvalues) Output=[77.54, 90.14, 108.68] C=np.array(cvalues) print(C) print(type(C)) Output=[25.3 32.3 42.6] class 'numpy.ndarray'> F=C*9/5+32 print(F) Output=[ 77.54 90.14 108.68]
- An ndarray is usually a fixed-size multi-dimensional container of items of the same type and size.
- The number of dimensions and items in an array is defined by its shape, which is a tuple of N positive integers that specify the sizes of each dimension.
a=np.array([1,2,3]) a Output=array([1, 2, 3]) a=np.array([[1,2,3],[4,5,6]]) a Output=array([[1, 2, 3], [4, 5, 6]]) a=np.array([1,2,3,4],ndmin=2) a Output=array([[1, 2, 3, 4]]) a=np.array([1,2,3,4],dtype=complex) a Output=array([1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j])
- The type of item in the array is specified by a separate data-type object, one of which is associated with each ndaarray.
- A data type object describes the type of data (integer, float, python object), size of data, and in the case of structured type, the names of fields and data type of each field.
dt=np.dtype(np.int32) dt Output=dtype('int32') dt=np.dtype('i4') dt Output=dtype('int32') student=np.dtype([('name','S20'),('age','i1'),('marks','f4')]) student Output=dtype([('name', 'S20'), ('age', 'i1'), ('marks', 'f4')])
- ndarray.ndim- Represents the number of dimensions (axes) of the ndarray.
- ndarray.shape-is a tuple of integers representing the size of the ndarray in each dimension.
- ndarray.size-is the total number of elements in the ndarray. It is equal to the product of elements of the shape.
- ndarray.itemsizereturns the size (in bytes) of each element of a NumPy array.
- numpy.flags-The ndarray object has many attributes. Its current value is returned by this function.
a=np.array([('Sunil',20,81),('Anil',18,91)],dtype = student) a Output=array([(b'Sunil', 20, 81.), (b'Anil', 18, 91.)], dtype=[('name', 'S20'), ('age', 'i1'), ('marks', 'f4')]) a=np.array([[1,2,3],[4,5,6]]) a Output=array([[1, 2, 3], [4, 5, 6]]) print(a.shape) Output=(2, 3) a.shape=(3,2) a Output=array([[1, 2], [3, 4], [5, 6]]) a=np.array([[1,2,3],[4,5,6]]) b=a.reshape(3,2) print(a) print(b) Output=[[1 2 3] [4 5 6]] [[1 2] [3 4] [5 6]] a= np.arange(24) a Output=array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]) b=a.reshape(2,4,3) b Output=array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]]) x=np.array([1,2,3,4,5],dtype=np.float32) x.itemsize Output=4 x=np.array([1,2,3,4,5]) x.flags Output= C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False print(x.flags['WRITEABLE']) Output=True
- A new ndarray object can be constructed by any of the array creation routines or using a low-level ndarray constructor.
-
numpy.empty : It creates an uninitialized array of specified shape and type.
-
numpy.zeros:Returns a new array of specified size, filled with zeros.
-
numpy.ones:Returns a new array of specified size and type, filled with ones.
* Shape : Shape of an empty array in int or sequence of int
* Dtype : Desired output data type. (Optional)
* Order :'C' for C-style row-major array, 'F' for FORTRAN style column-major array.
Array from numerical ranges:
numpy.arange : This function returns an ndarray object containing evenly spaced values within a given range. The format of the function is as follows
- Start:-The start of an interval. ff omitted, defaults to 0
- Stop:-The end of an interval (not including this number)
- Step:- Spacing between values, default is 1
- Dtype:-Data type of resulting ndarray. If not given, data type of input is used.
numpy.linspace : in this function, instead of step size, the number of evenly spaced values between the interval is specified. The usage of this function is as follows -
- Start:-The start of an interval. If omitted, defaults to 0
- Stop:-The end of an interval (not including this number)
- Num:-The number of evenly spaced samples to be generated. Default is 50
- Dtype:-Data type of resulting ndarray. if not given, data type of input is used.
- Endpoint:-True by default, hence the stop value is included in the sequence If false, it is not included.
- Retstep:- If true, returns samples and step between the consecutive numbers.
x=np.empty((3,2),dtype=int) x Output=array([[1, 2], [3, 4], [5, 6]]) x=np.zeros(5) x Output=array([0., 0., 0., 0., 0.]) x=np.zeros(5,dtype=int) x Output=array([0, 0, 0, 0, 0]) x=np.ones(5) x Output=array([1., 1., 1., 1., 1.]) x=np.ones([2,2],dtype=int) x Output=array([[1, 1], [1, 1]]) x=np.arange(5) x Output=array([0, 1, 2, 3, 4]) x=np.arange(5,dtype=float) x Output=array([0., 1., 2., 3., 4.]) x=np.linspace(10,20,5) x Output=array([10. , 12.5, 15. , 17.5, 20. ]) x=np.linspace(10,20,5,endpoint=False) x Output=array([10., 12., 14., 16., 18.]) x=np.linspace(10,20,5,retstep=True) x Output=(array([10. , 12.5, 15. , 17.5, 20. ]), 2.5)
- Arrays are important because they enable one to express batch operations on data without writing any for loops. This is usually called vectorization.
-
-
Operation between different sized arrays is called as broadcasting.
arr=np.array([[1,2,3],[4,5,6]]) arr Output=array([[1, 2, 3], [4, 5, 6]]) arr*arr Output=array([[ 1, 4, 9], [16, 25, 36]]) arr-arr Output=array([[0, 0, 0], [0, 0, 0]]) 1/arr Output=array([[1. , 0.5 , 0.33333333], [0.25 , 0.2 , 0.16666667]]) arr**0.5 Output=array([[1. , 1.41421356, 1.73205081], [2. , 2.23606798, 2.44948974]])
- Numpy array indexing is a rich topic, as there are many ways one may want to select the subset of once data or individual elements.
- One-dimensional arrays are simple, on the surface they act similarly to python lists.
-
An important first distinction from lists is that array slices are views on the original array. This mpeans that the data is not copied, and any. Modifications to the view will be reflected in the source array.
-
If one want a copy of a slice of an ndarray instead of a view, one will need to explicitly copy the array.
-
With higher dimensional arrays, you have many more options. In a two dimensional array, the elements at each index are no longer scalars but rather one-dimensional arrays.
-
ND-arrays can be sliced like python lists.
-
Higher dimensional objects give one more option as one can slice one or more axes and also mix integers.
-
One can pass multiple slices just like on can pass multiple indexes.
-
One always obtain array views of the same number of dimensions. By mixing integer indexes and slices, one get lower dimensional slices.
arr=np.arange(10) arr Output=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) print(arr[5]) Output=5 print(arr[5:8]) Output=[5 6 7] arr[5:8]=12 print(arr) Output=[ 0 1 2 3 4 12 12 12 8 9] arr_slice=arr[5:8] print(arr_slice) Output=[12 12 12] arr_slice[1]=12345 print(arr_slice) Output=[ 12 12345 12] arr2d=np.array([[1,2,3],[4,5,6],[7,8,9]]) arr2d Output=array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) arr2d=np.array([[1,2,3], [4,5,6], [7,8,9]]) arr2d Output=array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr2d[:2]) Output=[[1 2 3] [4 5 6]] print(arr2d[:,:1]) Output=[[1] [4] [7]] data=np.random.randn(7, 3) data Output=array([[ 0.91036761, 0.01140246, 0.09820392], [ 1.23032068, -2.02814858, 1.20794091], [-1.35326304, 0.46266684, 0.19864061], [-1.61946037, 0.83037172, 2.43784079], [ 0.48793765, -0.33290337, -0.66951161], [-0.18232362, -0.79675993, 1.41495878], [-0.48911061, 1.10336667, 1.37471542]]) print(data[1]) Output=[1.23032068 0. 1.20794091] print(data[[1,3,4]]) Output=[[1.23032068 0. 1.20794091] [0. 0.83037172 2.43784079] [0.48793765 0. 0. ]]
- Fancy indexing is a term adopted by numpy to describe indexing using integers arrays.
- To select out the subset of the rows in a particular order, one can simply pass a list or ndarray of integers specifying the desired order.
- Using negative indices one can select rows from the end.
- Passing multiple index arrays does slightly something different, it selects a 1D array of elements corresponding to each tuple of indices.
arr=np.arange(32).reshape(8,4) arr Output=array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23], [24, 25, 26, 27], [28, 29, 30, 31]]) print(arr[[4,3,0,6]]) Output=[[16 17 18 19] [12 13 14 15] [ 0 1 2 3] [24 25 26 27]] print(arr[[-3,-5,-7]]) Output=[[20 21 22 23] [12 13 14 15] [ 4 5 6 7]] print(arr[[1,5,7,2],[0,3,1,2]]) Output=[ 4 23 29 10]
-
A universal function, or ufune, is a function that performs element wise operations on data in ndarrays.
-
One can think of them as fast vectorized wrappers for simple functions that take one or more scalar values and produce one or more scalar results.
-
Many ufunes are simple element wise transformations, like sqrt or exp.
-
These are referred to as unary ufuncs. Others, such as add or maximum, take 2 arrays (thus, binary ufuncs) and return a single array as the result.
arr=np.arange(10) arr Output=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) np.sqrt(arr) Output=array([0. , 1. , 1.41421356, 1.73205081, 2. , 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ]) np.exp(arr) Output=array([1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01, 5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03, 2.98095799e+03, 8.10308393e+03]) x=np.random.randn(8) x Output=array([-0.72919841, -1.0676 , 0.44787507, -0.40537476, -1.16973273, 0.93954252, -0.82483436, -1.15828711]) y=np.random.randn(8) y Output=y=np.random.randn(8) y y=np.random.randn(8) y array([-0.96981463, -0.25304955, 1.53079467, 1.57485227, -0.89747387, 0.9190207 , 0.18129364, 0.08489679]) np.maximum(x,y) Output=array([-0.72919841, -0.25304955, 1.53079467, 1.57485227, -0.89747387, 0.93954252, 0.18129364, 0.08489679]) x=np.array([1.2,1.3,1.4,1.5]) y=np.array([2.1,2.3,2.4,2.5]) c=np.array([True,False,True,False]) ressult=np.where(c,x,y) ressult Output=array([1.2, 2.3, 1.4, 2.5])
THANK YOU!!!
Join the conversation