Data Types in NumPy

Python tutorial · PySpark.in

Data Types in NumPy

Data types in NumPy define the type of elements stored in an array. Unlike Python lists, NumPy arrays are homogeneous, meaning all elements in an array must be of the same data type, which ensures efficient memory usage and faster computation.

1.Integer Data Type

Integer data types in NumPy are used to store whole numbers without fractional parts and may be signed or unsigned, depending on the specified type.

Example

```

import numpy as np

arr_int = np.array([1, 2, 3, 4])

print(arr_int)

print(arr_int.dtype)

```

Specific Integer Type

```

arr_int8 = np.array([10, 20, 30], dtype=np.int8)

print(arr_int8)

print(arr_int8.dtype)

```

2.Float Data Type

Floating-point data types in NumPy are used to represent real numbers with decimal values and are commonly used in scientific and mathematical computations.

Example

```

arr_float = np.array([1.5, 2.6, 3.7])

print(arr_float)

print(arr_float.dtype)

```

Type Conversion to Float

```

arr_float2 = np.array([1, 2, 3], dtype=float)

print(arr_float2)

print(arr_float2.dtype)

```

3. Boolean Data Type

Boolean data types in NumPy store logical values, where True represents 1 and False represents 0, and are commonly used in conditional operations and filtering.

Example

```

arr_bool = np.array([True, False, True])

print(arr_bool)

print(arr_bool.dtype)

```

Boolean from Condition

```

arr = np.array([10, 20, 30, 40])

result = arr > 25

print(result)

```

4. Type Conversion (Type Casting)

Type conversion in NumPy refers to the process of changing the data type of an array from one type to another using built-in methods.

Using astype():

```

arr = np.array([1, 2, 3])

print(arr.dtype)

arr_float = arr.astype(float)

print(arr_float)

print(arr_float.dtype)

```

Automatic Type Conversion

```

arr = np.array([1, 2.5, 3])

print(arr)

print(arr.dtype)

```

NumPy automatically converts to a higher precision type.

5.Custom Data Type (Structured Array)

A custom or structured data type in NumPy allows the creation of arrays with fields of different data types, similar to a structure in C programming.

Example

```

student_dtype = np.dtype([

('name', 'U10'),

('age', 'i4'),

('marks', 'f4')

])

students = np.array([

('Megha', 22, 95.5),

('Anita', 22, 90.0)

], dtype=student_dtype)

print(students)

print(students['name'])

print(students['marks'])

```

6. Overflow Behavior

Overflow behavior in NumPy occurs when a calculated value exceeds the maximum limit of the data type, causing the value to wrap around instead of raising an error.

Example

```

arr_overflow = np.array([127], dtype=np.int8)

print(arr_overflow)

arr_overflow = arr_overflow + 1

print(arr_overflow)

```

Explanation:

7. Checking Memory Usage

```

arr = np.array([1, 2, 3, 4], dtype=np.int32)

print(arr.itemsize) # bytes per element

print(arr.nbytes) # total memory used

```

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges