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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.