{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Copying, Sorting and Reshaping" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# import numpy \n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Copy\n", "- Copies array to new memory\n", "- **Syntax:** `np.copy(array)`" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4 5 6 7 8 9]\n" ] } ], "source": [ "# create an array `A1`\n", "A1 = np.arange(10)\n", "print(A1)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4 5 6 7 8 9]\n" ] } ], "source": [ "# copy `A1` into A2 \n", "A2 = np.copy(A1)\n", "print(A2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## View\n", "- Creates view of array elements with type(dtype)\n", "- **Syntax:** `array.view(np.dtype)`" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.0e+00 0.0e+00 0.0e+00 0.0e+00 6.0e-08 0.0e+00 0.0e+00 0.0e+00 1.2e-07\n", " 0.0e+00 0.0e+00 0.0e+00 1.8e-07 0.0e+00 0.0e+00 0.0e+00 2.4e-07 0.0e+00\n", " 0.0e+00 0.0e+00 3.0e-07 0.0e+00 0.0e+00 0.0e+00 3.6e-07 0.0e+00 0.0e+00\n", " 0.0e+00 4.2e-07 0.0e+00 0.0e+00 0.0e+00 4.8e-07 0.0e+00 0.0e+00 0.0e+00\n", " 5.4e-07 0.0e+00 0.0e+00 0.0e+00]\n" ] } ], "source": [ "# view of array A2 \n", "A3 = A2.view(np.float16)\n", "print(A3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sorting\n", "- Returns a sorted copy of an array.\n", "- **Syntax:** `array.sort()`\n", " - element-wise sorting(default) \n", " - axis = 0; row\n", " - axis = 1; column\n", "![Axis](../img/axis.png)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 9 2 3 1 5 10]\n" ] } ], "source": [ "# Unsorted array\n", "A4 = np.array([9, 2, 3,1, 5, 10])\n", "print(A4) " ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1 2 3 5 9 10]\n" ] } ], "source": [ "# Call sort function\n", "A4.sort()\n", "print(A4)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[4 1 3]\n", " [9 5 8]]\n" ] } ], "source": [ "# Row and column unsorted\n", "A5 = np.array([[4, 1, 3], [9, 5, 8]])\n", "print(A5) " ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 3 4]\n", " [5 8 9]]\n" ] } ], "source": [ "# Apply sort function on column axis=1\n", "A5.sort(axis=1)\n", "print(A5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Flatten: Flattens 2D array to 1D array\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4, 1, 3, 9, 5, 8])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 2D array\n", "A6 = np.array([[4, 1, 3], [9, 5, 8]])\n", "# 1D array \n", "A6.flatten()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Transpose: Transposes array (rows become columns and vice versa)\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[4, 1, 3],\n", " [9, 5, 8]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A7 = np.array([[4, 1, 3], [9, 5, 8]])\n", "A7" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[4, 9],\n", " [1, 5],\n", " [3, 8]])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Transpose A7 \n", "A7.T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reshape: Reshapes arr to `r` rows, `c` columns without changing data\n", "![Reshape](../img/reshape.png)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 8, 9, 10],\n", " [11, 12, 13]])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A8 = np.array([(8,9,10),(11,12,13)])\n", "A8" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 8, 9],\n", " [10, 11],\n", " [12, 13]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Reshape --> 3x4\n", "A8.reshape(3,2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Resize: Changes arr shape to `rxc` and fills new values with 0\n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 8, 9, 10],\n", " [11, 12, 13]])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A9 = np.array([(8,9,10),(11,12,13)])\n", "A9" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 8, 9],\n", " [10, 11],\n", " [12, 13]])" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Resize \n", "A9.resize(3, 2)\n", "A9" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## References\n", "- https://numpy.org/\n", "- https://www.edureka.co/blog/python-numpy-tutorial/\n", "- https://github.com/enthought/Numpy-Tutorial-SciPyConf-2019\n", "- [Python Machine Learning Cookbook](https://www.amazon.com/Python-Machine-Learning-Cookbook-Prateek/dp/1786464470)\n", "
\n", "\n", "*This notebook was created by [Jubayer Hossain](https://jhossain.me/) | Copyright © 2020, [Jubayer Hossain](https://jhossain.me/)*" ] } ], "metadata": { "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.4" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }