Data Types in NumPy

Data types in NumPy determine how your numbers are stored and handled. Picking the right data type can save memory and speed up calculations. NumPy offers various types to handle different kinds of data, like whole numbers, decimals, complex numbers, and even text.

The dtype attribute tells you the data type of your Numpy array or lets you set it when creating an array.

Example 1: Check the Data Type

import numpy as np

# Creating arrays with different data types
str_array=np.array(["apple", "banana", "kiwi"])
num_array=np.array([7,89,6,5,2,64,5])
bool_array=np.array([True, False, True, False])
float_array=np.array([1.5, 2.2, 12.5, 33.5])

# Checking the data types
print(str_array.dtype)
print(num_array.dtype)
print(bool_array.dtype)
print(float_array.dtype)

Output :

<U6
int32
bool
float64

Example 2: Specify the Data Type

import numpy as np

str_array=np.array(["apple", "banana", "kiwi"], dtype="S")
num_array=np.array([7,89,6,5,2,64,5], dtype="f")
bool_array=np.array([True, False, True, False], dtype="i")
float_array=np.array([1.5, 2.2, 12.5, 33.5], dtype="U")

print(str_array)
print(num_array)
print(bool_array)
print(float_array)

Output :

[b'apple' b'banana' b'kiwi']
[ 7. 89.  6.  5.  2. 64.  5.]
[1 0 1 0]
['1.5' '2.2' '12.5' '33.5']

Types of Data Types

NumPy offers a variety of data types to suit different needs:

Integer Types

Integers are used to represent whole numbers, both positive and negative. NumPy provides different sizes of integer data types, such as:

  • int8: 8-bit signed integer (-128 to 127)
  • int16: 16-bit signed integer (-32,768 to 32,767)
  • int32: 32-bit signed integer (-2,147,483,648 to 2,147,483,647)
  • int64: 64-bit signed integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
  • uint8: 8-bit unsigned integer (0 to 255)
  • uint16: 16-bit unsigned integer (0 to 65,535)
  • uint32: 32-bit unsigned integer (0 to 4,294,967,295)
  • uint64: 64-bit unsigned integer (0 to 18,446,744,073,709,551,615)

Example:

import numpy as np
 
# If you're counting people, you don't need int64
count = np.array([1, 2, 3], dtype=np.int32)

Floating-Point Types

Floating-point numbers are used to represent real numbers with decimal points. NumPy offers three main floating-point data types:

  • float16: 16-bit floating-point (half precision)
  • float32: 32-bit floating-point (single precision)
  • float64: 64-bit floating-point (double precision)

Example:

import numpy as np
 
# Measuring temperature with float32 is usually sufficient
temperature = np.array([25.5, 26.0, 25.8], dtype=np.float32)

Complex Number Types

Complex numbers are often used in electrical engineering, quantum physics, and signal processing, among other fields. They allow you to work with both real and imaginary components in your calculations. For complex numbers, NumPy provides:

  • complex64: Complex number with 32-bit real and imaginary parts.
  • complex128: Complex number with 64-bit real and imaginary parts.

Example:

import numpy as np
 
# Complex numbers for electrical engineering calculations
impedance = np.array([2 + 3j, 1 - 2j], dtype=np.complex128)

String Types

While the strings are of two types in NumPy:

  • str_: This type is for handling strings made up of regular characters like letters and numbers. It’s useful for simple text data.
  • unicode_: This type is for handling strings that might include characters from different languages or special symbols. It’s more versatile and can handle a wider range of characters.

Both str_ and unicode_ data types allow you to specify the maximum length of the strings. For example, if you want to create a string data type that can hold strings with a maximum length of 20 characters, you can specify it as ‘U20’.

Change the Data Type of an Array

The astype() method lets you change the data type of an array, making it easy to convert between different types as needed but this function will make a copy of the original array with the given data type.

Example:

import numpy as np

num_array = np.array([7,89,6,5,2,64,5])
bool_array = np.array([True, False, True, False])
copy_num_array = num_array.astype("U")
copy_bool_array = bool_array.astype("i")

print(copy_num_array)
print(copy_bool_array)
print(num_array)
print(bool_array)

Output:

['7' '89' '6' '5' '2' '64' '5']
[1 0 1 0]
[ 7 89  6  5  2 64  5]
[ True False  True False]

Structured Arrays in NumPy

Structured arrays in NumPy provide a way to work with heterogeneous data types within a single array. Unlike traditional NumPy arrays, which store elements of the same data type, structured arrays allow you to define custom data types, each with its fields.

Creating Structured Arrays 

To create a structured array in NumPy, you first need to define a data type that specifies the fields and their respective data types. You can define a data type using the np.dtype() function and pass a list of tuples, where each tuple represents a field with its name and data type.

Example:

import numpy as np
 
# Let's craft a data type for our party guests with 'name' (strings) and 'age' (integers)
guest_dtype = np.dtype([('name', 'U20'), ('age', int)])
 
# Then, we bring in the partygoers with their names and ages
party_data = np.array([('Alice', 25), ('Bob', 30), ('Charlie', 22)], dtype=guest_dtype)
 
# And here's the fun part: You can talk to your data using field names!
print("Guest Names:", party_data['name'])
print("Guest Ages:", party_data['age'])

Output:

Guest Names: ['Alice' 'Bob' 'Charlie']
Guest Ages: [25 30 22]

Sorting and Filtering Structured Arrays

Structured arrays in NumPy are a handy way to manage data that comes in different types, almost like organizing information in a table. Sorting and filtering are common tasks when dealing with this kind of data, and NumPy provides tools to make these tasks easier.

Sorting a Structured Array

Sorting a structured array allows you to rearrange the rows based on the values in one or more fields. NumPy provides a convenient function np.sort() for sorting structured arrays based on specified criteria. Here’s how you can do it:

Example:

# Let's sort our guest list by age, like arranging friends by their birth year
sorted_guests = np.sort(party_data, order='age')
print("Sorted Guest List by Age:")
print(sorted_guests)

Output:

Sorted Guest List by Age:
[('Charlie', 22) ('Alice', 25) ('Bob', 30)]

If you want to sort by multiple fields, provide a tuple of field names in the order parameter.

Filtering a Structured Array

NumPy provides powerful indexing capabilities for filtering structured arrays efficiently. Here’s how you can filter a structured array:

Example:

# Want to find the young crowd under 30? Piece of cake!
young_crowd = party_data[party_data['age'] < 30]
print("Young and Fun Guests:")
print(young_crowd)

Output:

Young and Fun Guests:
[('Alice', 25) ('Charlie', 22)]

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *