We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3
Lab Session #3
Exploring NumPy Module in Python
Aim: To Learn NumPy module in python and explore the various functions available in it.
Problem Definition: Develop Python Programs to do the following:
1. Create a Numpy array filled with all ones. 2. Find the number of occurrences of a sequence in a NumPy array 3. Interchange two axes of an array 4. Trim the leading and/or trailing zeros from a 1-D array 5. Reverse a numpy array 6. make a NumPy array read-only. 7. Replace NumPy array elements that doesn’t satisfy the given condition 8. Remove columns in Numpy array that contains non-numeric values 9. Create a Numpy array with random values 10. Get the n-largest values of an array using NumPy? 11. get the floor, ceiling and truncated values of the elements of a numpy array 12. Evaluate Einstein’s summation convention of two multidimensional NumPy arrays 13. Compute the variance of the NumPy array 14. Compute the standard deviation of the NumPy array 15. Compute pearson product-moment correlation coefficients of two given NumPy arrays 16. Calculate the mean across dimension in a 2D NumPy array 17. Define a polynomial function. Add, subtract, multiply and divide one polynomial to another using NumPy 18. save a NumPy array to a text file? 19. Load data from a text file 20. Plot line graph from NumPy array 21. Create Histogram using NumPy Theory: NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. It is open-source software. It contains various features including these important ones: ● A powerful N-dimensional array object ● Sophisticated (broadcasting) functions ● Tools for integrating C/C++ and Fortran code ● Useful linear algebra, Fourier transform, and random number capabilities Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined using Numpy which allows NumPy to seamlessly and speedily integrate with a wide variety of databases. Installation: Mac and Linux users can install NumPy via pip command: pip install numpy Windows does not have any package manager analogous to that in linux or mac. Please download the pre-built windows installer for NumPy from here (according to your system configuration and Python version). And then install the packages manually. NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In NumPy dimensions are called axes. The number of axes is rank. NumPy’s array class is called ndarray. It is also known by the alias array.
Array creation: There are various ways to create arrays in NumPy.
● For example, you can create an array from a regular Python list or tuple using the array function. The type of the resulting array is deduced from the type of the elements in the sequences. ● Often, the elements of an array are originally unknown, but its size is known. Hence, NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive operation. For example: np.zeros, np.ones, np.full, np.empty, etc. ● To create sequences of numbers, NumPy provides a function analogous to range that returns arrays instead of lists. ● arange: returns evenly spaced values within a given interval. step size is specified. ● linspace: returns evenly spaced values within a given interval. num no. of elements are returned. ● Reshaping array: We can use reshape method to reshape an array. Consider an array with shape (a1, a2, a3, …, aN). We can reshape and convert it into another array with shape (b1, b2, b3, …, bM). The only required condition is: a1 x a2 x a3 … x aN = b1 x b2 x b3 … x bM . (i.e original size of array remains unchanged.) ● Flatten array: We can use flatten method to get a copy of array collapsed into one dimension. It accepts order argument. Default value is ‘C’ (for row-major order). Use ‘F’ for column major order. Array Indexing: Knowing the basics of array indexing is important for analysing and manipulating the array object. NumPy offers many ways to do array indexing. ● Slicing: Just like lists in python, NumPy arrays can be sliced. As arrays can be multidimensional, you need to specify a slice for each dimension of the array. ● Integer array indexing: In this method, lists are passed for indexing for each dimension. One to one mapping of corresponding elements is done to construct a new arbitrary array. ● Boolean array indexing: This method is used when we want to pick elements from array which satisfy some condition. Basic operations: Plethora of built-in arithmetic functions are provided in NumPy. ● Operations on single array: We can use overloaded arithmetic operators to do element-wise operation on array to create a new array. In case of +=, -=, *= operators, the existing array is modified. ● Unary operators: Many unary operations are provided as a method of ndarray class. This includes sum, min, max, etc. These functions can also be applied row-wise or column-wise by setting an axis parameter. ● Binary operators: These operations apply on array elementwise and a new array is created. You can use all basic arithmetic operators like +, -, /, , etc. In case of +=, -=, = operators, the existing array is modified. And many more.
The Neurobiology of Attachment Focused Therapy Enhancing Connection & Trust in the Treatment of Children & Adolescents (Norton Series on Interpersonal Neurobiology) ISBN 0393711048, 9780393711042 Complete Volume Download