{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![](NotebookHeader.jpg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Course : Python for Data Science\n", "\n", "## Module 3 Lesson 1 : Numpy Arrays" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "marks = [20,30,40, 550, 600]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "newmarks = marks*5" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[20,\n", " 30,\n", " 40,\n", " 550,\n", " 600,\n", " 20,\n", " 30,\n", " 40,\n", " 550,\n", " 600,\n", " 20,\n", " 30,\n", " 40,\n", " 550,\n", " 600,\n", " 20,\n", " 30,\n", " 40,\n", " 550,\n", " 600,\n", " 20,\n", " 30,\n", " 40,\n", " 550,\n", " 600]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "newmarks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a numpy array object" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "np_marks = np.array(marks)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 20, 30, 40, 550, 600])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np_marks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vectorized Operations" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 100, 150, 200, 2750, 3000])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np_marks*5" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np_marks.ndim" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5,)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np_marks.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Two dimensional array" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "np_marks = np.array([[10,20,30], [100,200,300]], np.int32)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 3)" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(np_marks)\n", "np_marks.shape" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([20, 40, 60])" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np_marks[0]*2" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "200" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np_marks[1]" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "300" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np_marks[1][2]" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([list([10, 200, 10, 200]), list([100, 200, 300, 100, 200, 300])],\n", " dtype=object)" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np_marks_2 = np_marks*2\n", "np_marks_2" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "np_marks = np.array(([10,20,30,40,50], [100,200, 300, 400, 500]))" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 10, 20, 30],\n", " [100, 200, 300]])" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np_marks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use of conditions for slicing / retriving subset" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "cond = np_marks > 20" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 30, 100, 200, 300])" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np_marks[cond]" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "marks = [20,30,40, 550, 600]" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "np_marks = np.array(marks)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[False False True]\n", " [ True True True]]\n" ] } ], "source": [ "print(cond)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "cond2 = [True, False, True, False, True]" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 20, 40, 600])" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np_marks[cond2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Applying statistical functions" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30.0" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "marks = [20,30,40]\n", "np_marks = np.array(marks)\n", "np_marks.mean()" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10 20 30]\n", "[300 400 500]\n" ] } ], "source": [ "t1 = [10,20, 30]\n", "t2 = [300,400,500]\n", "t1 = np.array(t1)\n", "t2 = np.array(t2)\n", "print(t1)\n", "print(t2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Combining two numpy arrays" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 10, 20, 300, 400, 500])" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.concatenate([t1, t2])" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 10, 20, 30],\n", " [300, 400, 500]])" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.vstack([t1,t2]) " ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 10, 20, 30, 300, 400, 500])" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.hstack([t1,t2]) " ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10. , 12.5, 15. , 17.5, 20. ])" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.linspace(10, 20, 5) # Generating five points (spaced equaly like on a number line) between 10 and 20\n", "x" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10, 12, 14, 16, 18])" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = np.arange(10,20,2)\n", "y" ] } ], "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" } }, "nbformat": 4, "nbformat_minor": 4 }