Understanding numpy.int16 and numpy.uint16 types (6 examples)

Updated: February 25, 2024 By: Guest Contributor Post a comment

Numpy is a powerful library in Python widely used for numerical computing. Understanding its data types is crucial for effective computation and data processing. This tutorial focuses on numpy.int16 and numpy.uint16 types, explaining their characteristics and applications through six increasingly advanced examples.

What are numpy.int16 and numpy.uint16?

numpy.int16 and numpy.uint16 are integral data types in Numpy. numpy.int16 is used for signed 16-bit integers, while numpy.uint16 denotes unsigned 16-bit integers. Here lies their primary difference – the range of values they can represent. numpy.int16 can represent values from -32768 to 32767, whereas numpy.uint16 can represent values from 0 to 65535.

Example 1: Basics of numpy.int16 and numpy.uint16

import numpy as np

# Creating arrays with numpy.int16 and numpy.uint16 types
data_int16 = np.array([32767, -32768], dtype=np.int16)
data_uint16 = np.array([65535, 0], dtype=np.uint16)

print('int16 array:', data_int16)
print('uint16 array:', data_uint16)

Output:

int16 array: [ 32767 -32768]
uint16 array: [65535     0]

This example introduces you to creating arrays using both data types and showcases their boundary values.

Example 2: Arithmetic Operations

# Exploring overflow with arithmetic operations
data_int16 = np.array([32760], dtype=np.int16)
data_int16 += 10
print('Overflowed int16:', data_int16)
data_uint16 = np.array([65535], dtype=np.uint16)
data_uint16 += 1
print('Overflowed uint16:', data_uint16)

Output:

Overflowed int16: [-32766]
Overflowed uint16: [0]

This example illustrates how arithmetic operations can lead to overflow in these data types, wrapping around their respective boundaries.

Example 3: Bitwise Operations

# Bitwise AND operation
a = np.array([15], dtype=np.int16)
b = np.array([8], dtype=np.int16)
print('Bitwise AND:', np.bitwise_and(a, b))

Output:

Bitwise AND: [8]

Bitwise operations are crucial for low-level programming. This example demonstrates a simple bitwise AND operation, highlighting the compatibility of numpy.int16 with these operations.

Example 4: Conversion Between int16 and uint16

# Conversion example
a = np.array([-1], dtype=np.int16)
converted = a.astype(np.uint16)
print('Converted to uint16:', converted)

Output:

Converted to uint16: [65535]

Here we show how to convert values between signed and unsigned 16-bit integers, pointing out the underlying value representation mechanism during conversion.

Example 5: Working with Images

Processing images with numpy often involves dealing with various bit-depths. Here’s how you can manipulate image pixel values using numpy.int16 and numpy.uint16.

# Simulating a grayscale image processing scenario
image = np.array([[0, 65535], [32767, -32768]], dtype=np.int16)
# Normalize the image
normalized_image = ((image - image.min()) / (image.max() - image.min())) * 65535
normalized_image = normalized_image.astype(np.uint16)
print('Normalized Image:\n', normalized_image)

Output:

Normalized Image:
  [[    0 65535]
  [32767     0]]

This example simulates normalizing a grayscale image’s pixel values, demonstrating the utility of both data types in image processing.

Example 6: Scientific Computing

numpy.int16 and numpy.uint16 are also used in scientific computing. This advanced example deals with data processing in a simulated physics experiment.

# Simulated data processing in physics experiment
data = np.array([120, -120, 200, -200, 300], dtype=np.int16)
# Apply a filter based on a predefined condition
filtered_data = data[data > 0]
# Evaluate the mean of the positive values
mean_positive = np.mean(filtered_data)
print('Mean of positive values:', mean_positive)

Output:

Mean of positive values: 206.66666666666666

This example showcases the use of numpy.int16 in filtering and processing datasets, a common scenario in scientific computing.

Conclusion

This tutorial explored the nuances of numpy.int16 and numpy.uint16 types through practical examples. From basic array creation, handling overflow, and bitwise operations to specialized applications such as image processing and scientific computing, these examples aimed to provide a comprehensive understanding of using these data types effectively.