{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Matrix Decomposition Example for Machine Learning for Computational Linguistics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**(C) 2017-2024 by [Damir Cavar](http://damir.cavar.me/)**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Download:** This and various other Jupyter notebooks are available from my [GitHub repo](https://github.com/dcavar/python-tutorial-notebooks)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Version:** 1.3, January 2024" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**License:** [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/) ([CA BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Prerequisites:**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install -U numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a tutorial related to the discussion of matrix decomposition of feature sets in classification tasksin the textbook [Machine Learning: The Art and Science of Algorithms that Make Sense of Data](https://www.cs.bris.ac.uk/~flach/mlbook/) by [Peter Flach](https://www.cs.bris.ac.uk/~flach/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This tutorial was developed as part of my course material for the course Machine Learning for Computational Linguistics in the [Computational Linguistics Program](http://cl.indiana.edu/) of the [Department of Linguistics](http://www.indiana.edu/~lingdept/) at [Indiana University](https://www.indiana.edu/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrix operations" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 0 1 0]\n", " [0 2 2 2]\n", " [0 0 0 1]\n", " [1 2 3 2]\n", " [1 0 1 1]\n", " [0 2 2 3]]\n" ] } ], "source": [ "from numpy import array\n", "\n", "ratings = array([\n", " [1, 0, 1, 0],\n", " [0, 2, 2, 2],\n", " [0, 0, 0, 1],\n", " [1, 2, 3, 2],\n", " [1, 0, 1, 1],\n", " [0, 2, 2, 3]])\n", "\n", "print(ratings)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Decomposition of the matrix into sub-matrices:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 0 1 0]\n", " [0 2 2 2]\n", " [0 0 0 1]\n", " [1 2 3 2]\n", " [1 0 1 1]\n", " [0 2 2 3]]\n" ] } ], "source": [ "from numpy import dot\n", "\n", "filmsGenres = array([\n", " [1, 0, 1, 0],\n", " [0, 1, 1, 1],\n", " [0, 0, 0, 1]\n", " ])\n", "\n", "preferencesGenres = array([\n", " [1, 0, 0],\n", " [0, 1, 0],\n", " [0, 0, 1],\n", " [1, 1, 0],\n", " [1, 0, 1],\n", " [0, 1, 1]\n", " ])\n", "\n", "importanceGenres = array([\n", " [1, 0, 0],\n", " [0, 2, 0],\n", " [0, 0, 1]\n", " ])\n", "\n", "print(dot(preferencesGenres, dot(importanceGenres, filmsGenres)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" }, "latex_metadata": { "affiliation": "Indiana University, Department of Linguistics, Bloomington, IN, USA", "author": "Damir Cavar", "title": "Matrix Decomposition Example for Machine Learning for Computational Linguistics" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": false, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": false, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }