Entrance Announcement
MICTE 2080
2080 Magh 07
User:Niraj/Teaching-18
Teaching lesson plan 18 Subject: Python programming
Date: 4 Feb 2024
Time: 60 minutes
Period: 3rd
Teaching Item: Array Input and Output in NumPy
Class: Bachelor
Objective:
Students will learn how to input and output array data using NumPy, understand various file formats supported for array storage, and apply file input/output operations to efficiently work with array data.
Materials Needed:
- Python interpreter with NumPy installed or IDE
- Projector
1. Introduction to Array Input and Output (10 mins)
- Define array input and output operations:
- Array input/output refers to the process of reading from and writing to external files or streams.
- It allows for storing and retrieving array data from various formats.
- Discuss the importance of array input/output in data analysis, machine learning, and scientific computing.
2. NumPy File Formats (10 mins)
- Introduce different file formats supported by NumPy for array storage:
- Text files: CSV (comma-separated values) and plain text files.
- Binary files: NumPy's native
.npy
format for efficient binary storage. - Compressed files: NumPy's
.npz
format for storing multiple arrays in a single compressed file.
- Discuss the advantages and use cases of each file format.
3. Reading and Writing Text Files (15 mins)
- Demonstrate how to read and write array data from/to text files using NumPy functions:
numpy.loadtxt()
: Read data from a text file.numpy.savetxt()
: Write data to a text file.
- Show examples of loading and saving arrays in CSV and plain text formats.
4. Reading and Writing Binary Files (15 mins)
- Explain how to read and write array data from/to binary files using NumPy functions:
numpy.load()
: Load data from a.npy
binary file.numpy.save()
: Save data to a.npy
binary file.
- Demonstrate the usage of these functions for efficient binary storage and retrieval.
5. Working with Compressed Files (10 mins)
- Introduce NumPy's compressed file format
.npz
for storing multiple arrays:numpy.load()
can also load data from a.npz
file containing multiple arrays.- Show how to save and load multiple arrays into/from a single compressed file.
- Discuss the benefits of using compressed files for efficient storage and transmission of array data.
6. Error Handling and Exception Handling (5 mins)
- Discuss error handling strategies when working with file input/output operations:
- Handle potential errors such as file not found, permission denied, or file format mismatches.
- Show how to use
try
andexcept
blocks to handle exceptions gracefully.
7. Exercise (15 mins)
- Provide a programming exercise where students:
- Write code to load array data from a text file, perform some operations, and save the modified array to a binary file.
- Experiment with different file formats and compression techniques to understand their impact on file size and performance.
8. Conclusion (5 mins)
- Recap the key points covered in the lesson:
- NumPy supports various file formats for array input and output, including text files, binary files, and compressed files.
- Text files are suitable for human-readable data exchange but may be slower for large datasets.
- Binary files offer faster storage and retrieval but are not human-readable.
- Compressed files provide a space-efficient way to store multiple arrays in a single file.
- Encourage students to practice using array input/output operations with different file formats and to explore additional file handling techniques.