The objectives of this assignment are to:
- Practice Python programming and use of NumPy arrays.
- Understand the importance of testing code for correctness and reliability.
- Implement an
Indexclass that maps string or object sequences to numeric vectors and matrices, a common preprocessing step in neural network training.
| File | Description |
|---|---|
notebooks/stv_nn.ipynb |
Main notebook containing the full implementation of the Index class. |
notebooks/string-to-vector.ipynb |
Notebook used to run pytest validation and record test results. |
tests/test_nn.py |
Provided test file used to validate correctness using pytest. |
stv_nn.py |
The exported version of the main notebook used for testing. |
stv_nn.html |
Exported HTML version of the notebook for grading and review. |
README.md |
This file, describing objectives, setup, and instructions. |
requirements.txt |
Python dependencies for running tests. |
The Index class provides a numeric mapping for sequences of objects (typically strings). It supports:
- Creating object-to-index and index-to-object mappings.
- Converting between object sequences and numeric arrays.
- Generating binary (one-hot) encodings and decoding them back to objects.
- Handling padding and unknown objects gracefully.
Implemented Methods:
__init__– Assign indexes to unique vocabulary items starting from a given value.objects_to_indexes– Convert a sequence of objects to a numeric index vector.objects_to_index_matrix– Convert sequences of objects to an indexed matrix with padding.objects_to_binary_vector– Produce a binary (one-hot) vector representation.objects_to_binary_matrix– Produce a binary matrix for multiple sequences.indexes_to_objects– Convert indexes back to their original objects.index_matrix_to_objects– Convert index matrices back to object sequences.binary_vector_to_objects– Retrieve objects from a binary vector.binary_matrix_to_objects– Retrieve objects from a binary matrix.
This assignment reuses the shared virtual environment created for previous coursework.
# Activate the shared environment
source ~/venvs/ml-env/bin/activate
# Navigate to the assignment folder
cd ~/projects/info527-neural-networks-assignment1
# Install dependencies
pip install -r requirements.txtrequirements.txt
numpy
pytest
-
Run all cells in
stv_nn.ipynbto complete the implementation. -
Export the notebook as:
stv_nn.ipynbstv_nn.htmlstv_nn.py
-
Place the exported
.pyfile in the same directory astest_nn.py. -
Run pytest to verify correctness:
pytest tests/test_nn.pyExpected Output:
============================= test session starts ==============================
collected 8 items
test_nn.py ........ [100%]
============================== 8 passed in 1.XXs ===============================
Submit the following for grading:
stv_nn.ipynbstv_nn.htmlstv_nn.pystring-to-vector.ipynb(with pytest results)
Grading is based solely on the correctness of the code written in stv_nn.ipynb.
| Method | Description | Marks |
|---|---|---|
__init__ |
Index initialization | 3 |
objects_to_indexes |
Convert objects to index vector | 1 |
objects_to_index_matrix |
Convert sequences to indexed matrix | 3 |
objects_to_binary_vector |
One-hot vector encoding | 2 |
objects_to_binary_matrix |
One-hot matrix encoding | 1 |
indexes_to_objects |
Decode index vector | 1 |
index_matrix_to_objects |
Decode index matrix | 1 |
binary_vector_to_objects |
Decode binary vector | 2 |
binary_matrix_to_objects |
Decode binary matrix | 1 |
Total: 15 marks
This repository was completed as part of INFO 527: Neural Networks, under the M.S. in Information Science and Machine Learning program (2023–2025).