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

Importing and Exporting Text Files

Learn how to load numerical data from text files and save NumPy arrays for future use. Explore np.loadtxt(), np.savetxt(), delimiter settings, headers, comments, and practical examples of reading and writing numerical datasets.

1. Learning Objectives

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

  • Understand text-based data storage.
  • Load numerical data using np.loadtxt().
  • Save NumPy arrays using np.savetxt().
  • Work with spaces, commas, and other delimiters.
  • Handle header rows and comment lines.
  • Control numeric output formatting.
  • Understand common file path and data-format errors.
  • Apply text file input and output to practical datasets.

2. Introduction to Text File Input and Output

Text files store information as readable characters. Numerical datasets are often saved in text formats such as TXT and delimiter-separated files such as CSV.

NumPy provides functions for reading and writing numerical data. The most commonly used functions in this lesson are np.loadtxt() and np.savetxt().

Function Purpose
np.loadtxt() Reads numerical data from a text file.
np.savetxt() Writes numerical array data to a text file.
Text File 10 20 30 40 50 60 loadtxt NumPy Array [[10, 20, 30], [40, 50, 60]] savetxt Output File Saved data .txt / .csv Load text data into NumPy and export arrays to text files.

NumPy supports reading numerical text data and exporting array values.

3. Creating a Sample Text File

Before loading data, create a text file containing numerical values. For this example, create a file named numbers.txt with the following contents.

10 20 30 40 50 60 70 80 90

Each line contains three values separated by spaces. NumPy can read this format directly because whitespace is the default delimiter for np.loadtxt().

Important: Save the file in the directory from which your Python program can access it, or provide the correct file path.

4. Loading Data Using np.loadtxt()

The np.loadtxt() function reads numerical values from a text file and returns a NumPy array.

import numpy as np data = np.loadtxt("numbers.txt") print(data) print("Shape:", data.shape)

Output:

[[10. 20. 30.] [40. 50. 60.] [70. 80. 90.]] Shape: (3, 3)

The text file is converted into a two-dimensional NumPy array. By default, np.loadtxt() typically loads numerical values as floating-point numbers.

Loading Values as Integers

import numpy as np data = np.loadtxt("numbers.txt", dtype=int) print(data) print("Data type:", data.dtype)

Output:

[[10 20 30] [40 50 60] [70 80 90]] Data type: int64

The exact integer dtype may depend on your platform. The dtype=int argument requests integer values.

Tip: Use dtype=int when the file contains whole numbers and integer representation is appropriate. Use a floating-point dtype when decimal values are needed.

5. Loading Comma-Separated Data

Numerical text files sometimes use commas instead of spaces. In that case, provide the delimiter argument to np.loadtxt().

Create a file named sales.csv with these contents:

100,200,300 150,250,350 180,280,380

Reading CSV-Style Numerical Data

import numpy as np sales = np.loadtxt("sales.csv", delimiter=",") print(sales)

Output:

[[100. 200. 300.] [150. 250. 350.] [180. 280. 380.]]

The delimiter tells NumPy how values are separated in the file. If a comma-separated file is read without the correct delimiter, loading may fail because a line can be interpreted as one invalid numerical field.

Remember: Use delimiter="," for comma-separated numerical data and the default whitespace handling for space-separated data.

6. Handling Headers and Comment Lines

Text files may contain a header row describing the columns. If the header contains text, skip it when loading purely numerical data.

Example file: student_marks.csv

Python,NumPy,Statistics 80,85,90 75,92,88 90,87,95

Skipping the Header

import numpy as np marks = np.loadtxt( "student_marks.csv", delimiter=",", skiprows=1 ) print(marks)

Output:

[[80. 85. 90.] [75. 92. 88.] [90. 87. 95.]]

The skiprows=1 argument tells NumPy to skip the first line before reading the numerical values.

Handling Comment Lines

NumPy can ignore comment lines when the file uses a comment marker, such as #.

# Monthly measurements 10 20 30 40 50 60
import numpy as np data = np.loadtxt("measurements.txt", comments="#") print(data)

Output:

[[10. 20. 30.] [40. 50. 60.]]
Important: skiprows counts lines at the beginning of the file. The comments argument identifies comment markers; it does not automatically remove arbitrary text headers that are not comments.

7. Saving Arrays Using np.savetxt()

The np.savetxt() function writes numerical array values to a text file. This is useful when you want to store calculated results for later use.

import numpy as np data = np.array([ [10, 20, 30], [40, 50, 60], [70, 80, 90] ]) np.savetxt("output.txt", data) print("Data saved successfully.")

Output:

Data saved successfully.

The file output.txt contains the array values. By default, np.savetxt() writes numeric values in a text-based format.

Reading the Saved File Again

import numpy as np loaded_data = np.loadtxt("output.txt") print(loaded_data)

Output:

[[10. 20. 30.] [40. 50. 60.] [70. 80. 90.]]

You can read the exported data again with np.loadtxt(), provided the file contains compatible numerical data.

8. Saving Data with a Specific Delimiter

The delimiter argument in np.savetxt() controls how values are separated in the output file.

import numpy as np data = np.array([ [10, 20, 30], [40, 50, 60] ]) np.savetxt( "output.csv", data, delimiter=",", fmt="%d" ) print("CSV file created.")

Output:

CSV file created.

The generated output.csv file contains:

10,20,30 40,50,60

The delimiter argument sets commas between values, while fmt="%d" writes values using integer formatting.

9. Formatting Values with np.savetxt()

The fmt parameter controls how values are represented in the output file. It is especially useful when exporting decimal values or when a specific number of decimal places is required.

import numpy as np data = np.array([ [12.3456, 78.9123], [45.6789, 23.4567] ]) np.savetxt( "formatted.txt", data, fmt="%.2f" ) print("Formatted data saved.")

The file contains:

12.35 78.91 45.68 23.46

The format string %.2f writes each floating-point value with two digits after the decimal point.

Format Meaning Example
%.2f Two decimal places 12.35
%.3f Three decimal places 12.346
%d Integer formatting 12
%.4e Scientific notation with four decimal places 1.2346e+01

10. Saving Headers and Comments

np.savetxt() supports header and comments arguments. These allow you to label exported columns and add explanatory information to the beginning of a file.

import numpy as np marks = np.array([ [80, 85, 90], [75, 92, 88], [90, 87, 95] ]) np.savetxt( "student_marks.csv", marks, delimiter=",", fmt="%d", header="Python,NumPy,Statistics", comments="" ) print("Student marks exported.")

The generated file contains:

Python,NumPy,Statistics 80,85,90 75,92,88 90,87,95

Here, comments="" prevents NumPy from adding the default comment prefix before the header.

Loading the Exported File

import numpy as np marks = np.loadtxt( "student_marks.csv", delimiter=",", skiprows=1, dtype=int ) print(marks)

Output:

[[80 85 90] [75 92 88] [90 87 95]]

11. Working with One-Dimensional Arrays

Text files can also store one-dimensional arrays. Each element can be written on a separate line or separated by a delimiter.

import numpy as np values = np.array([10, 20, 30, 40, 50]) np.savetxt("values.txt", values, fmt="%d") loaded_values = np.loadtxt("values.txt", dtype=int) print(loaded_values) print("Shape:", loaded_values.shape)

Output:

[10 20 30 40 50] Shape: (5,)

A single-column text file is loaded as a one-dimensional array by default when it contains multiple numerical rows.

Preserving a Two-Dimensional Column Shape

import numpy as np values = np.loadtxt("values.txt", dtype=int, ndmin=2) print(values) print("Shape:", values.shape)

Output:

[[10 20 30 40 50]] Shape: (1, 5)

The ndmin argument specifies a minimum number of dimensions. In this example, the values are returned as a two-dimensional row array.

12. Handling File Paths and Errors

File operations can fail if a file does not exist, if the path is incorrect, or if the contents cannot be interpreted as numerical data.

Common File Errors

  • FileNotFoundError: The specified file path does not exist.
  • ValueError: The file contains values that cannot be parsed as requested.
  • Incorrect delimiter: Values are separated differently from the delimiter specified.
  • Unexpected header: Text headers are included when numerical data is expected.

Example: Handling a Missing File

import numpy as np try: data = np.loadtxt("missing_file.txt") print(data) except FileNotFoundError: print("The requested file was not found.")

Handling file errors allows a program to display a useful message rather than terminating unexpectedly.

Important: During development, confirm the file location, spelling, extension, and delimiter before assuming the NumPy function is incorrect.

13. Practical Activity: Student Marks File

Activity Description

At CIIT TRANING INSTITUTE, Samadhan, Pradnya, and Samiksha have marks in Python, NumPy, and Statistics. Save the marks to a CSV file, load them again, and calculate each student's total and average.

Sample Solution

import numpy as np names = ["Samadhan", "Pradnya", "Samiksha"] marks = np.array([ [80, 85, 90], [75, 92, 88], [90, 87, 95] ]) np.savetxt( "student_marks.csv", marks, delimiter=",", fmt="%d", header="Python,NumPy,Statistics", comments="" ) loaded_marks = np.loadtxt( "student_marks.csv", delimiter=",", skiprows=1, dtype=int ) totals = np.sum(loaded_marks, axis=1) averages = np.mean(loaded_marks, axis=1) for i, name in enumerate(names): print(name) print("Total:", totals[i]) print("Average:", round(averages[i], 2))

Output:

Samadhan Total: 255 Average: 85.0 Pradnya Total: 255 Average: 85.0 Samiksha Total: 272 Average: 90.67

The program exports the marks, loads them from the CSV file, and calculates the total and average for each student.

Practical use: Text file input and output is useful for sharing numerical datasets, exporting analysis results, and preparing data for other applications.

14. Common Mistakes

Mistake 1: Forgetting the Delimiter

When reading comma-separated values, specify delimiter="," so NumPy interprets the file correctly.

Mistake 2: Loading a Header as Numerical Data

Use skiprows when the file begins with a textual header that should not be read as a numerical row.

Mistake 3: Using the Wrong Data Type

Choose an appropriate dtype for the contents of the file. Integer types cannot represent fractional values exactly.

Mistake 4: Using an Incorrect File Path

Check the current working directory or supply the correct relative or absolute path to the file.

Mistake 5: Expecting the Same Display Format

Use the fmt argument in np.savetxt() when the output requires a specific number of decimal places or integer formatting.

15. Practice Exercises

Exercise 1: Load a Text File

Create a file containing three rows of numerical data. Load it using np.loadtxt() and display the array shape.

Exercise 2: Read CSV Data

Create a comma-separated file and load it using delimiter=",". Display the resulting NumPy array.

Exercise 3: Export an Array

Create a 3 × 3 NumPy array and save it to a text file using np.savetxt().

Exercise 4: Format Decimal Values

Save an array of decimal values using fmt="%.2f". Open the resulting file and inspect the formatting.

Exercise 5: Save and Reload Student Marks

Create marks for Samadhan, Pradnya, and Samiksha. Save the marks to a CSV file with a header, reload the numerical values, and calculate row-wise averages.

Exercise 6: Handle a Missing File

Try loading a file that does not exist. Use try and except to handle FileNotFoundError.

Summary

In this lesson, you learned how to load numerical text data using np.loadtxt() and export NumPy arrays using np.savetxt().

You explored delimiters, headers, comment lines, data types, and output formatting. You also practised saving and reloading student marks from a CSV file and calculating numerical summaries.