In this lesson, I’m going to take you through how to use different data types with
arange(). So, moving into the terminal. And remember, you always have to
import numpy as np—or, you don’t need to do that, but you need to
import numpy, for sure in order to use
You can take a look at the
.dtype variable of the
x array, and you can see here that it’s an
'int64'. What that means is that we’re representing integers, which makes sense, because I only specified an integer as the stopping value, so there’s no reason to assume that I need floating-point values, and each value in here can be perfectly represented as an integer.
64 part means that this has a size of 64 bits. And you might say—oh, I’m sorry. I need to say
x.size is the number of elements. Oh, you might say “Oh, wow, that’s just
8. That’s weird. Why is it
8 with 64?” Well, the
.itemsize is in bytes, and the designation is in bits.
The reason for that is that NumPy,
np, has many different types. I’m going to move over to show them to you. It has many different types which designate the size of integers, and all of these have floating-point analogs as well.
01:49 And so there are many different cases where you might want to use smaller than the maximum possible number of bits to represent a number. For example, if you’re doing picture representation, you don’t often need more than 255 possible colors—red, green, blue values—because the RGB color system doesn’t actually need any of that.
02:10 You can represent all of the colors in, say, a JPEG image with just 8-bit integers to represent each pixel, rather than a whole 64 bits that you’re not going to use. So it saves space that way. Many libraries like TensorFlow, which is a popular Python machine learning library you might’ve heard of, use 32-bit integers and floats just to save space because they use such massive amounts of data in their computations.
Convenient to know, for sure. When you use the
arange() function, normally if you don’t specify the data type, then NumPy will try to infer from the data points that you give it what it should do.
Well, you’ll notice this isn’t anything close to a
1010. There are these
-15. That’s because of just how when numbers are stored in 8 bits, if you try to represent something much bigger, what you’ll actually be doing is just essentially going in a loop around the number system.
And this is something that you’ll have to read a little bit more about how binary number representation works in order to understand exactly why these particular numbers are generated, but just trust me when I say that you only want to specify a smaller size than you need—or a smaller size than the default—when you are sure that that’s what you actually need. So in this case, what I just did was a very silly thing, because clearly my
start and my
stop are both much bigger than what can be represented by an
int8. But if I did
int32 here, I wouldn’t actually have any issues. So, not a big deal.
Become a Member to join the conversation.