{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:31:12.616119Z", "start_time": "2020-08-02T21:31:12.357816Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Splitting of Array\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:31:22.244242Z", "start_time": "2020-08-02T21:31:22.236270Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11],\n", " [12, 13, 14, 15]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.arange(16).reshape((4, 4))\n", "A" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:31:34.452753Z", "start_time": "2020-08-02T21:31:34.442462Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1],\n", " [ 4, 5],\n", " [ 8, 9],\n", " [12, 13]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Horizontal split\n", "upper, lower = np.hsplit(A, 2)\n", "upper" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:32:12.681989Z", "start_time": "2020-08-02T21:32:12.677320Z" } }, "outputs": [], "source": [ "# Vertical split\n", "left, right = np.vsplit(A, 2)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Min and Max" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:32:24.436924Z", "start_time": "2020-08-02T21:32:24.426248Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[4, 9, 3, 9],\n", " [9, 5, 4, 8],\n", " [5, 4, 9, 0]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = np.random.randint(10, size = (3,4))\n", "B " ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:32:28.931219Z", "start_time": "2020-08-02T21:32:28.920770Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B.min()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:32:54.870487Z", "start_time": "2020-08-02T21:32:54.860497Z" } }, "outputs": [ { "data": { "text/plain": [ "array([4, 4, 3, 0])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Minimum of columns\n", "B.min(axis = 0)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:32:58.795951Z", "start_time": "2020-08-02T21:32:58.790480Z" } }, "outputs": [ { "data": { "text/plain": [ "array([3, 4, 0])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Minimum of rows\n", "B.min(axis = 1)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:33:15.501827Z", "start_time": "2020-08-02T21:33:15.498793Z" } }, "outputs": [ { "data": { "text/plain": [ "69" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sum of the whole array\n", "B.sum()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:33:27.751650Z", "start_time": "2020-08-02T21:33:27.747969Z" } }, "outputs": [ { "data": { "text/plain": [ "5.75" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Mean of the whole array\n", "B.mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Boolean" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:34:10.885016Z", "start_time": "2020-08-02T21:34:10.878790Z" } }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# how many families whose number of members above 5\n", "np.count_nonzero(B<5)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:33:57.787195Z", "start_time": "2020-08-02T21:33:57.781249Z" } }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Or this\n", "np.sum(B<5)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:34:38.304392Z", "start_time": "2020-08-02T21:34:38.301089Z" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# find if any of the family has the number of members above 8\n", "np.any(B>8)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:34:58.563717Z", "start_time": "2020-08-02T21:34:58.558046Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# find if all families have the number of members above 2\n", "np.all(B>2)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:35:32.158431Z", "start_time": "2020-08-02T21:35:32.152146Z" } }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#find how many families in the range 3 and 5\n", "np.sum((B >= 3) & (B <= 5))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:36:28.443892Z", "start_time": "2020-08-02T21:36:28.437348Z" } }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# find the number of families that are not less than 3 or above 5\n", "np.sum(~(B < 3) | (B > 5))" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:36:41.577225Z", "start_time": "2020-08-02T21:36:41.567953Z" } }, "outputs": [ { "data": { "text/plain": [ "array([9, 9, 9, 8, 9])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Peek at what numbers are above 5 in our data\n", "B[B > 5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Sorting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Sort indices" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:37:01.923811Z", "start_time": "2020-08-02T21:37:01.919363Z" } }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 6])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([3, 1, 2, 4, 6])\n", "np.sort(x)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:37:22.719715Z", "start_time": "2020-08-02T21:37:22.716397Z" } }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 0, 3, 4])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sort indices\n", "indices = np.argsort(x)\n", "indices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sort Columns or Rows" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:37:54.387012Z", "start_time": "2020-08-02T21:37:54.384456Z" } }, "outputs": [], "source": [ "A = np.random.randint(0, 10, (4, 6))" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:38:02.946875Z", "start_time": "2020-08-02T21:38:02.943432Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[3, 0, 5, 3, 2, 2],\n", " [4, 1, 6, 5, 3, 3],\n", " [6, 2, 8, 7, 6, 9],\n", " [7, 6, 9, 8, 7, 9]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# By columns\n", "np.sort(A, axis=0)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "ExecuteTime": { "end_time": "2020-08-02T21:38:11.695002Z", "start_time": "2020-08-02T21:38:11.684975Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 3, 5, 6, 7, 9],\n", " [2, 3, 6, 7, 8, 9],\n", " [3, 4, 5, 6, 7, 9],\n", " [0, 2, 2, 3, 6, 8]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# By rows\n", "np.sort(A, axis=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.8.3" }, "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": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }