Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

NumPy Techniques and Practical Examples (Summary)

You’ve now gained insight into some interesting use cases for NumPy. Although NumPy is essentially a Python library that allows you to work with multidimensional array objects, you now have a better understanding of how its features can be applied in several different scenarios.

In this video course, you’ve learned how to:

  • Construct NumPy array objects from external data files
  • Find matching data between different NumPy arrays
  • Extract named data and construct basic charts
  • Transform your own function into a vectorized function

Here are the additional resources mentioned in this summary lesson:

Download

Course Slides (.pdf)

1.3 MB
Download

Sample Code (.zip)

608.6 KB

00:00 In the previous lesson, I completed the fourth example by showing you how to create custom vectorized functions and how you might even be able to avoid them using NumPy’s where() function.

00:10 This is the last lesson and it will summarize the course and point you at others you might enjoy.

00:16 NumPy is a powerful numerics library in Python that is optimized for speed. It can be useful for a wide variety of programming problems. This course covered four real-world examples, which were creating multidimensional arrays from CSV files, reconciling data using structured arrays, charting hierarchical data, and writing custom vectorized functions.

00:41 In the first example, you learned how NumPy arrays are statically sized and how you can use the np.zeros() call to create a pre-populated array, and then you overwrote parts of it by using the np.loadtxt() call to fetch data from CSV files.

00:55 Along the way, you also learned how to use the Path object’s .glob() method to get an interable of files based on a file name pattern.

01:03 Remember, the order these come back in is operating system specific, and you might get away with a single-digit wildcard question mark, but if you use an asterisk and your file names have numbers in them, you might not get the order you expect. To be safe, you should sort the results from glob() and the natsort library provides a sorting method that treats the numeric part of file names like numbers rather than substrings.

01:28 NumPy arrays can be more than just matrix-based containers. You can also add structural information through the data type mechanism.

01:35 Remember though, NumPy has its own data types. These are mostly compatible with Python, but the important part of that sentence is mostly. When adding structural information to an array, you can pass in NumPy data types or a string that represents the type you want.

01:51 For example, U12 is a Unicode string, 12 characters in length. Once you have structural information in your arrays, you can take advantage of NumPy’s data reconciliation features in the recfunctions module.

02:03 This includes the ability to do SQL-like joins where you merge data from multiple arrays into a single new one based on common items like ID numbers.

02:14 The recfunctions module also contains the find_duplicates() function that will find duplicate rows in an array. Alternatively, if you’d rather get rid of duplicates instead of just find them, you can use NumPy’s unique() function.

02:28 In the third example, you saw how to use Matplotlib to create a bar chart. Matplotlib isn’t the most Pythonic thing and in its most common form you use global functions to build a global single chart.

02:39 It takes a little getting used to, but it works. Matplotlib plays nicely with NumPy and you can pass columns to it for graphing. In several of the examples, I showed you how you can do math on an array and have that math apply to every item in the array.

02:54 You can also do math between arrays. This means you can perform both scalar on vector operations and operations between vectors. If what you want to do is more complicated than straight math operations, you can create a function that operates like a vector operation through NumPy’s np.vectorize() call.

03:12 It is also handily available as a decorator. Vectorized functions are useful, but as they’re pure Python, they tend to be slower. If all you need to do is conditional operations, the NumPy where() function will likely be faster for you.

03:28 This course is based on a written tutorial. In addition to having everything that I’ve covered here, it also contains exercises to test your knowledge.

03:37 Or if you’d prefer, there’s also a companion quiz that goes with the tutorials. If you want to try out what you’ve learned, see the article or the quiz. If you like what you can do with NumPy, especially the structured data pieces, you might also find pandas useful.

03:52 It takes the structured mechanism to the next level. Here, you can get it as a tutorial or a video course. And a more recent addition to the Python space is a competitor to pandas called Polars.

04:03 Check out this tutorial to learn about that. Finally, if you liked the example-based structure of this course, you might also like this one. This teaches you a bit of astronomy using pandas and Matplotlib, and there’s even some NumPy buried in there as well.

04:19 Thanks for your attention. I hope you enjoyed the course.

Become a Member to join the conversation.