Controlling memory size

You may think that it is a waste of memory to use an int64 data type if the range of your values is so limited.

In fact, conscious of data-intensive situations, you can calculate how much memory space your Array_1 object is taking:

In: import numpy as np    Array_1.nbytesOut: 24  
Please note that on 32-bit platforms (or when using a 32-bit Python version on a 64-bit platform), the result is 12.

In order to save memory, you can specify the type that best suits your array beforehand:

In: Array_1 = np.array(list_of_ints, dtype= 'int8')  

Now, your simple array occupies just a fourth of the previous memory space. It may seem an obvious and overly simplistic example, but when dealing with millions of rows and columns, defining ...

Get Python Data Science Essentials - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.