For a comprehensive introcution of NumPy, refer to:

**An Overview of NumPy Data Types**

Here are some of the data types NumPy supports:

- Integers (like 1, 2, 3)
- Floating Points (like 1.2, 3.4)
- Complex Numbers (like 1+2j)

and many more. In this article, we explain each of them.

**Basic Data Types**

**Integers (**`int`

)

`int`

)Integers are whole numbers, both positive and negative. **No decimal points.**

**NumPy Variant**:`int32`

,`int64`

**Use Case**: Counting items, indexing.

Imagine you’re counting the number of books on a shelf.

You’d have 5 books, 10 books, or 20 books, but never 5.5 books. This is where integers come into play.

- You might wonder “why not just one type for intergers?”. Well, the numbers are the bits. And the bit defines how much memory you use and the precision of the number.
`int32`

uses less memory than`int64`

, but can’t store as large of a number. So, pick based on what you need.- We’ll explain about the bits in the later section.

**Floating Points (**`float`

)

`float`

)Floating points are numbers with decimal points. Precise values.

**NumPy Variant**:`float32`

,`float64`

**Use Case**: Measurements, weights.

Let’s say you’re baking and you need 1.5 cups of sugar. That 0.5 is a fraction, and for such measurements, you’d use floating points.

**Complex Numbers (**`complex`

)

`complex`

)Complex numbers are numbers with real and imaginary parts.

**NumPy Variant**:`complex64`

,`complex128`

**Use Case**: Advanced mathematical computations, signal processing.

Complex numbers might sound… well, complex.

But think of them as coordinates on a 2D plane, where one value is the “real” part (horizontal axis) and the other is the “imaginary” part (vertical axis).

**Further Understanding of Data Types**

We find a rich set of advanced data types beyond the basic `int`

, `float`

, and `complex`

. These data types cater to a variety of computational needs. Let’s explore them with some concrete examples.

**Boolean Type (**`bool_`

)

`bool_`

)- Represents the truth values
`True`

and`False`

. - Example: Filtering data based on a condition. If you have an array of ages and you want to know which ones are above 18, the result would be an array of Boolean values.

**Integer Types**

**Signed Integers**

`int8`

, `int16`

, `int32`

, `int64`

. These can represent both positive and negative whole numbers.

- Example:
`int16`

can store values from -32,768 to 32,767.

**Unsigned Integers**

`uint8`

, `uint16`

, `uint32`

, `uint64`

. These represent only non-negative whole numbers.

- Example:
`uint8`

can store values from 0 to 255, often used in image data.

**Floating Point Numbers**

`float16`

, `float32`

, `float64`

.

- Example: If you’re measuring the weight of objects with high precision, you might use
`float64`

to capture the data, like 12.34567890123456.

**Complex Numbers**

`complex64`

, `complex128`

.

- Example: In electrical engineering, complex numbers might be used to represent impedance, such as 5 + 3j.

**Datetime**

`datetime64`

: Represents dates (year, month, day) and time (hours, minutes, seconds).

- Example: If you’re analyzing stock prices, you might have a timestamp like
`2023-05-01 10:15:00`

.

**Timedelta**

`timedelta64`

represents the difference between two dates or times.

- Example: Calculating the duration between
`2023-05-01 10:15:00`

and`2023-05-02 11:15:00`

would give a timedelta of 25 hours.

**Object**

`object`

can store a Python object.

Example: An array that contains a mix of strings, numbers, and even lists.

**Strings**

**String_**

`string_`

is a fixed-length ASCII string type.

Example: Names of students in a class, like “John” or “Alicia”.

**unicode_**

`unicode_`

: Fixed-length Unicode character string.

Example: Text in multiple languages, such as “Hello” or “こんにちは”.

**Why So Many Variants?**

You might be scratching your head, **“Why not just one type of integer or float?”** It’s all about balance.

**Balance between how much memory you use and the precision of the number.** `int32`

uses less memory than `int64`

, but can’t store as large of a number. So, pick based on what you need.

**Choosing the Correct Bit**

As explained, the bit size determines **how much memory** is allocated for each number and the range of values it can represent.

Here’s a simple guide to help you make the right choice.

**1. Memory Concerns**

- If you’re working on a device with limited memory, like a Raspberry Pi or an older computer, you might want to consider using smaller bit sizes like
`int8`

or`int16`

. They use less memory but have a smaller range.

**2. Data Range**

- If you know your data will always be within a specific range, choose the smallest bit size that can handle that range. For example, if you’re sure your data will always be between -100 and 100,
`int8`

would suffice. - However, if you’re uncertain about the maximum or minimum values, or if the range is vast, you might want to opt for
`int32`

or`int64`

.

**3. Default Choice when in Doubt**

- If you’re unsure which bit size to choose and want a safe bet, go with
. It offers a good balance between memory usage and range, suitable for most general applications.`32`

**4. Special Cases**

- For extremely large numbers or high precision calculations,
`int64`

or`float64`

might be necessary. - If you’re working with image data, often
`uint8`

(unsigned 8-bit integer) is used as pixel values range from 0 to 255.

**Creating Arrays with Specific Data Types**

It’s easy to specify a data type in NumPy.

```
import numpy as np
arr = np.array([1, 2, 3], dtype='int32')
```

Here, we’ve told NumPy, “Make this array with numbers as `int32`

.”

If you want to specify the complex numbers, just type `dtype='complex32'`

, and type `dtype='float32'`

for the float. Simple, right?

**Converting Between Data Types**

Sometimes, you might need to change the data type of an array after it’s been created.

```
import numpy as np
arr = np.array([1.1, 2.2, 3.3], dtype='float64')
int_arr = arr.astype('int32') # Outputs [1 2 3]
```

In the example above, we’ve converted a floating-point array to an integer array.

But remember, converting from `float`

to `int`

will truncate the decimal part. So, 1.1 becomes 1, 2.2 becomes 2, and so on.