Master NumPy From Scratch

Learn NumPy fundamentals, arrays, indexing, slicing, data types, array operations, broadcasting, mathematical functions, statistics, random numbers, linear algebra, file handling, and practical projects through structured lessons.

COURSE SYLLABUS
1. NumPy Introduction and Installation
Introduction to NumPy NumPy Installation and Setup NumPy Environment and IDE Setup First NumPy Program
2. NumPy Arrays and Array Creation
NumPy Array Fundamentals Array Creation Functions Zeros, Ones and Identity Arrays Range and Linspace Arrays
3. NumPy Array Indexing and Slicing
One-Dimensional Array Indexing Array Slicing Techniques Two-Dimensional Array Indexing Boolean and Fancy Indexing
4. NumPy Data Types and Attributes
NumPy Data Types Array Shape, Size and Dimensions Data Type Conversion and Casting Array Inspection and Attributes
5. NumPy Array Shape and Reshaping
Reshape and Flatten Arrays Transpose and Axis Operations Stacking and Splitting Arrays Expand and Squeeze Dimensions
6. NumPy Array Operations and Broadcasting
Arithmetic Array Operations Comparison and Logical Operations Broadcasting Fundamentals Universal Functions and Vectorization
7. NumPy Mathematical and Statistical Functions
Mathematical Functions Aggregate and Statistical Functions Minimum, Maximum and Percentiles Rounding and Trigonometric Functions
8. NumPy Filtering and Sorting
Array Filtering and Conditions Sorting and Searching Arrays Unique Values and Set Operations Where, Select and Nonzero Functions
9. NumPy Copies and Views
Array Copies and Views Shallow and Deep Copy Concepts Memory Layout and Contiguity Array Iteration and Memory Efficiency
10. NumPy Random Number Generation
Random Generator Fundamentals Random Integers and Sampling Probability Distributions Random Seeds and Simulation
11. NumPy Linear Algebra
Vectors and Matrices Matrix Multiplication and Dot Product Determinants and Matrix Inverse Eigenvalues and Linear Equations
12. NumPy File Input and Output
Importing and Exporting Text Files CSV Data with NumPy Binary Files and NPY Format NPZ Files and Data Persistence
13. NumPy Practical Projects
Student Marks Analysis Project Sales Data Analysis Project Random Simulation Project Image Array Processing Project
14. NumPy Practice and Assessment
NumPy Beginner Practice NumPy Intermediate Exercises NumPy Advanced Challenges Final NumPy Assessment

NumPy Data Types

Learn how NumPy stores numbers, text, Boolean values, and other kinds of data in arrays. Understand the dtype attribute, common NumPy data types, explicit type selection, and type conversion.

1. Learning Objectives

By the end of this lesson, you will be able to:

  • Understand what a data type means in NumPy.
  • Identify common NumPy integer and floating-point types.
  • Work with Boolean and string arrays.
  • Inspect an array's data type using the dtype attribute.
  • Specify a data type while creating an array.
  • Convert array elements using the astype() method.
  • Understand type conversion and possible data loss.
  • Choose suitable data types for numerical calculations.
  • Identify common data type errors.
  • Apply data type concepts to practical datasets.
Beginner tip: Every NumPy array has a data type. You can inspect it using array.dtype.

2. What Are NumPy Data Types?

A data type describes the kind of value stored in an array. For example, an array may contain integers, decimal numbers, Boolean values, or text.

NumPy arrays generally store elements using a common data type. This helps NumPy manage memory efficiently and perform numerical operations consistently.

Example: Creating Arrays with Different Data

import numpy as np integer_array = np.array([10, 20, 30]) float_array = np.array([10.5, 20.5, 30.5]) boolean_array = np.array([True, False, True]) string_array = np.array(["Red", "Green", "Blue"]) print(integer_array.dtype) print(float_array.dtype) print(boolean_array.dtype) print(string_array.dtype)

Example Output

int64 float64 bool <U5

The exact integer type can depend on the platform and the values used. NumPy uses a Unicode string type for the string array in this example.

Array Example Values Typical dtype
Integer array [10, 20, 30] int64 or another integer type
Floating-point array [10.5, 20.5] float64
Boolean array [True, False] bool
String array ["Red", "Blue"] Unicode string type
Figure 1: Different kinds of values are represented using different NumPy data types.

3. Understanding the dtype Attribute

The dtype attribute tells you which data type NumPy uses to represent the elements of an array. It is useful when checking numerical precision, understanding conversions, and troubleshooting calculations.

import numpy as np values = np.array([10, 20, 30, 40]) print(values) print(values.dtype) decimal_values = np.array([1.5, 2.5, 3.5]) print(decimal_values.dtype)

Inspecting Array Data Types

Expression Purpose
values.dtype Returns the data type of the array elements.
values.shape Returns the dimensions of the array.
values.size Returns the total number of elements.
values.ndim Returns the number of array dimensions.
Remember: dtype is an attribute, so write values.dtype, not values.dtype().

4. Integer Data Types

Integer data types store whole numbers without fractional parts. NumPy provides signed and unsigned integer types with different storage sizes.

  • int8 and uint8
  • int16 and uint16
  • int32 and uint32
  • int64 and uint64

Signed integers can represent negative and positive values. Unsigned integers represent non-negative values and provide a different range for the same number of bits.

import numpy as np small_numbers = np.array([10, 20, 30], dtype=np.int8) regular_numbers = np.array([100, 200, 300], dtype=np.int32) unsigned_numbers = np.array([0, 100, 200], dtype=np.uint8) print(small_numbers) print(small_numbers.dtype) print(regular_numbers.dtype) print(unsigned_numbers.dtype)

Integer Type Comparison

Data Type Storage Typical Use
int8 8 bits Small signed integer values.
int16 16 bits Integer data requiring a larger range than int8.
int32 32 bits General-purpose fixed-width integer data.
int64 64 bits Integer values requiring a wider range.
uint8 8 bits Non-negative values such as many image pixel values.
Important: Choose an integer type that can represent all expected values. A value outside the supported range may cause an error or unexpected behavior depending on the operation and NumPy version.

5. Floating-Point Data Types

Floating-point types represent numbers that may contain fractional parts. They are commonly used for measurements, averages, prices, percentages, and scientific calculations.

  • float16 uses 16 bits.
  • float32 uses 32 bits.
  • float64 uses 64 bits.
import numpy as np values16 = np.array([1.25, 2.50, 3.75], dtype=np.float16) values32 = np.array([1.25, 2.50, 3.75], dtype=np.float32) values64 = np.array([1.25, 2.50, 3.75], dtype=np.float64) print(values16.dtype) print(values32.dtype) print(values64.dtype) print(values64.mean())

Comparing Floating-Point Types

Type Storage Consideration
float16 16 bits Uses less memory but has limited precision and range.
float32 32 bits Often useful when balancing memory and precision.
float64 64 bits Provides greater precision than float32 in typical calculations.
Practical tip: Use a floating-point type when calculations require decimal values. Remember that floating-point numbers have finite precision and may not represent every decimal value exactly.

6. Boolean and String Data Types

Boolean Data Type

The Boolean data type, represented by bool, stores logical values: True and False. Boolean arrays are especially useful for filtering data.

import numpy as np attendance = np.array([True, False, True, True, False]) print(attendance) print(attendance.dtype) print(attendance.sum()) # Counts True values for this Boolean array

String Data Type

NumPy supports arrays containing text. When strings are created using np.array(), NumPy commonly infers a fixed-width Unicode or byte-string data type.

names = np.array(["Samadhan", "Pradnya", "Samiksha"]) print(names) print(names.dtype)
Important: NumPy's fixed-width string arrays can truncate strings assigned later if the existing dtype does not have enough capacity. For general-purpose mixed or variable-length text, consider whether a Python list or another data structure is more suitable.

7. Specifying a Data Type During Array Creation

You can specify the desired data type using the dtype parameter of np.array(). This helps control how values are represented in the array.

import numpy as np integer_values = np.array([1, 2, 3, 4], dtype=np.int32) decimal_values = np.array([1, 2, 3, 4], dtype=np.float64) boolean_values = np.array([1, 0, 1], dtype=np.bool_) print(integer_values) print(integer_values.dtype) print(decimal_values) print(decimal_values.dtype) print(boolean_values) print(boolean_values.dtype)

Example Output

[1 2 3 4] int32 [1. 2. 3. 4.] float64 [ True False True] bool

When integers are converted to floating-point values, the output can display decimal points. When values are converted to Boolean, zero becomes False and nonzero numeric values become True.

Remember: Use NumPy types such as np.int32, np.float64, and np.bool_ when you need an explicit data type.

8. Converting Data Types with astype()

The astype() method creates a new array with elements converted to the requested data type. It is commonly used to convert integers to floating-point values or floating-point values to integers.

import numpy as np numbers = np.array([10, 20, 30, 40]) decimal_numbers = numbers.astype(np.float64) print(numbers) print(numbers.dtype) print(decimal_numbers) print(decimal_numbers.dtype)

Converting Floating-Point Values to Integers

values = np.array([10.8, 20.4, 30.9, 40.1]) integer_values = values.astype(np.int32) print(integer_values) # Output: [10 20 30 40]

When converting finite floating-point values to integer types, the fractional part is discarded rather than rounded to the nearest integer.

Converting Numbers to Strings

numbers = np.array([10, 20, 30]) text_values = numbers.astype(str) print(text_values) print(text_values.dtype)
Important: Type conversion can lose information. Converting decimal values to integers removes their fractional parts. Converting large values to a narrower integer type may also change the represented values or fail, depending on the conversion and NumPy version.

9. Understanding Type Promotion

When arrays or values with different data types are combined, NumPy may choose a common data type that can represent the resulting values. This behavior is known as type promotion.

import numpy as np integers = np.array([1, 2, 3]) decimals = np.array([1.5, 2.5, 3.5]) result = integers + decimals print(result) print(result.dtype)

Example Output

[2.5 4.5 6.5] float64

In this example, the result uses a floating-point type so that the decimal values can be represented in the calculation. The exact promoted type depends on the input dtypes and operation.

values = np.array([1, 2, 3], dtype=np.int32) result = values + 0.5 print(result) print(result.dtype)
Practical tip: Check the resulting array's dtype when combining values with different types, especially in scientific calculations and data processing.

10. Data Type and Memory Usage

A NumPy array's itemsize attribute reports the number of bytes used by each element. Choosing an appropriate dtype can reduce memory usage when the required range and precision are understood.

import numpy as np small_array = np.array([10, 20, 30], dtype=np.int16) large_array = np.array([10, 20, 30], dtype=np.int64) print(small_array.itemsize) print(large_array.itemsize) print(small_array.nbytes) print(large_array.nbytes)

Understanding the Output

2 8 6 24

Each int16 element uses 2 bytes, while each int64 element uses 8 bytes. The nbytes attribute reports the total memory occupied by the array's element data, excluding other array-object overhead.

Attribute Meaning
dtype Data type used for array elements.
itemsize Size in bytes of one array element.
nbytes Total bytes occupied by the array's element data.
size Total number of array elements.

11. Common Data Type Errors

Problem Reason Solution
Unexpected decimal values NumPy inferred a floating-point type. Inspect dtype and specify a type if needed.
Lost fractional parts Floating-point values were converted to integers. Keep a floating-point type when decimals are required.
Integer range problems The selected integer type cannot represent a value. Choose a type with a suitable range.
Unexpected Boolean values Nonzero numeric values convert to True. Check the conversion rules and source values.
String truncation A fixed-width string dtype has insufficient capacity. Choose an appropriate string representation before assignment.
Unexpected calculation results Precision or type promotion affects the result. Inspect the input and output dtypes.
Debugging tip: Print the array's dtype, shape, and sample values before changing its data type.
values = np.array([10.5, 20.7, 30.9]) print("Original dtype:", values.dtype) converted = values.astype(np.int32) print("Converted dtype:", converted.dtype) print("Converted values:", converted)

12. Practical Activity: Student Marks Analysis

Activity: Inspect and Convert Marks

Samadhan is preparing student marks for analysis. Use NumPy data types to inspect integer values, convert marks to floating-point values, calculate the average, and check memory usage.

import numpy as np marks = np.array([78, 85, 92, 67, 88], dtype=np.int32) print("Marks:", marks) print("Original dtype:", marks.dtype) decimal_marks = marks.astype(np.float64) print("Decimal marks:", decimal_marks) print("Converted dtype:", decimal_marks.dtype) print("Average:", decimal_marks.mean()) print("Bytes per element:", decimal_marks.itemsize) print("Total element bytes:", decimal_marks.nbytes)

Expected Output

Marks: [78 85 92 67 88] Original dtype: int32 Decimal marks: [78. 85. 92. 67. 88.] Converted dtype: float64 Average: 82.0 Bytes per element: 8 Total element bytes: 40

The activity demonstrates how to inspect an array's data type, convert integer marks to floating-point values, calculate an average, and examine the memory used by array elements.

13. Practice Exercises

Use the following arrays to complete the exercises:

import numpy as np values = np.array([10, 20, 30, 40, 50]) prices = np.array([12.5, 18.75, 24.5, 30.25]) attendance = np.array([True, False, True, True])
  1. Print the data type of each array.
  2. Create an integer array using np.int16.
  3. Create a floating-point array using np.float32.
  4. Convert the values array to floating-point values using astype().
  5. Convert the prices array to integers and observe what happens to the fractional parts.
  6. Print the item size of the values array.
  7. Print the total element-data bytes using nbytes.
  8. Create a string array containing three names and inspect its dtype.

Challenge Exercise

Pradnya is preparing a small sales dataset. Create a NumPy array containing sales values and complete the following tasks:

  • Create an integer array and inspect its dtype.
  • Convert the array to float32.
  • Calculate the average sales value.
  • Compare the item sizes of int32 and float64 arrays.
  • Explain why converting decimal sales values to integers may lose information.
  • Choose an appropriate data type for storing whole-number quantities.
Practice goal: For every array, inspect the dtype before and after conversion. Explain how the conversion affects the values and memory usage.

Summary

In this lesson, we learned that NumPy data types define how array elements are represented. We explored integer, floating-point, Boolean, and string data types and used the dtype attribute to inspect them.

We learned how to specify a data type during array creation and how to convert values using astype(). We also explored type promotion, memory usage, and the risks of losing information during conversion. Choosing the correct data type helps maintain numerical accuracy, manage memory, and avoid unexpected results in array operations. Practice inspecting and converting arrays to become more confident with NumPy data types.