Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JenniferNorthrup/23f3e8fd407ccaebca7cd7ac58c0363a to your computer and use it in GitHub Desktop.
Save JenniferNorthrup/23f3e8fd407ccaebca7cd7ac58c0363a to your computer and use it in GitHub Desktop.
Python_for_Data_Science_NumPy.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/JenniferNorthrup/23f3e8fd407ccaebca7cd7ac58c0363a/python_for_data_science_numpy.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "ynE9R19cPIMT"
},
"outputs": [],
"source": [
"# The first step of using numpy is to tell python to import it\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "V7NoTi0kTGVz"
},
"source": [
"### 2.1 NumPy Arrays"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cYsQgeSWalcG"
},
"source": [
"**NumPy Array**\n",
"* An array is a data structure that stores values of same data type.\n",
"* While python lists can contain values corresponding to different data types, arrays in python can only contain values corresponding to the same data type.\n",
"* However python lists fail to deliver the performance required while computing large sets of numerical data. To address this issue we use NumPy arrays.\n",
"* We can create NumPy arrays by converting a list to an array.\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "-iMbhbKUS01I"
},
"outputs": [],
"source": [
"# defining a list of different car companies or string elements\n",
"arr_str = ['Mercedes', 'BMW', 'Audi', 'Ferrari', 'Tesla']\n",
"\n",
"# defining a list of number of cylinders in car or numerical elements\n",
"arr_num = [5, 4, 6, 7, 3]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "z8kfV796S0xz",
"outputId": "6a6e9c99-a09e-4cf7-a9b0-6b2ab4737912"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Numpy Array (arr_str): ['Mercedes' 'BMW' 'Audi' 'Ferrari' 'Tesla']\n",
"Numpy Array (arr_num): [5 4 6 7 3]\n"
]
}
],
"source": [
"# connverting the list arr_str to a NumPy array\n",
"np_arr_str = np.array(arr_str)\n",
"\n",
"\n",
"# connverting the list arr_num to a NumPy array\n",
"np_arr_num = np.array(arr_num)\n",
"\n",
"# checking the output\n",
"print('Numpy Array (arr_str): ',np_arr_str)\n",
"print('Numpy Array (arr_num): ',np_arr_num)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fNUnCIRbS0vz"
},
"source": [
"The resuts look similar to a list but arr_str and arr_num have been converted to NumPy arrays. Let's check the data type to confirm this."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Cc_-eibuS0sr",
"outputId": "a224267c-64f1-4f53-bd12-5789f1292f4a"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Data type of arr_str: <class 'list'>\n",
"Data type of arr_num: <class 'list'>\n",
"Data type of np_arr_str: <class 'numpy.ndarray'>\n",
"Data type of np_arr_num: <class 'numpy.ndarray'>\n"
]
}
],
"source": [
"# printing the data type of lists\n",
"print('Data type of arr_str: ',type(arr_str))\n",
"print('Data type of arr_num: ',type(arr_num))\n",
"\n",
"# printing the data type after conversion of lists to array\n",
"print('Data type of np_arr_str: ',type(np_arr_str))\n",
"print('Data type of np_arr_num: ',type(np_arr_num))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "D5MBk_c0gorg"
},
"source": [
"* The above output confirms that both the lists were successfully converted to arrays"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vO-p9figS0qt"
},
"source": [
"**NumPy Matrix**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "63nVxGI-dEFn"
},
"source": [
"* A matrix is a two-dimensional data structure where elements are arranged into rows and columns.\n",
"* A matrix can be created by using list of lists"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "eHL0aPSOS0fT",
"outputId": "1072c6f8-af64-402f-b132-d28c55b3f19f"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[1 2 1]\n",
" [4 5 9]\n",
" [1 8 9]]\n"
]
}
],
"source": [
"# let's say we have information of different number of cylinders in a car and we want to display them in a matrix format\n",
"matrix = np.array([[1,2,1],[4,5,9],[1,8,9]])\n",
"print(matrix)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1l5-7_3T2svj",
"outputId": "187ae422-e4d8-4bd3-890b-f89d08eb139a"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Data type of matrix: <class 'numpy.ndarray'>\n"
]
}
],
"source": [
"print('Data type of matrix: ',type(matrix))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ic8DdniJAYi4"
},
"source": [
"* We see that all the NumPy objects have data type as ndarray"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "s_QCIWxNPywk"
},
"source": [
"### 2.2 NumPy Functions"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9ByN9uM8dIp9"
},
"source": [
"**There are different ways to create NumPy arrays using the functions available in NumPy library**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mJD-BCvcdjQY"
},
"source": [
"**Using np.arange() function**\n",
"* The np.arange() function returns an array with evenly spaced elements as per the interval. The interval mentioned is half-opened i.e. start is included but stop is excluded.\n",
"* It has the following paramaters:\n",
" * start : start of interval range. By default start = 0\n",
" * stop : end of interval range\n",
" * step : step size of interval. By default step size = 1"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "h6yhwhCfdhrO",
"outputId": "293afe4c-9c7d-4a39-dffc-1cb611f0c16f"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[0 1 2 3 4 5 6 7 8 9]\n",
"[0 1 2 3 4 5 6 7 8 9]\n"
]
}
],
"source": [
"arr2 = np.arange(start = 0, stop = 10) # 10 will be excluded from the output\n",
"print(arr2)\n",
"\n",
"# or\n",
"\n",
"arr2 = np.arange(0,10)\n",
"print(arr2)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "EBQxWv87gij1",
"outputId": "97824898-51ea-498e-beb7-525043d25ba9"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([ 0, 5, 10, 15])"
]
},
"metadata": {},
"execution_count": 8
}
],
"source": [
"# adding a step size of 5 to create an array\n",
"arr3 = np.arange(start = 0, stop = 20, step = 5)\n",
"arr3"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fadu1DMxge1S"
},
"source": [
"**Using np.linspace() function**\n",
"* The np.linspace() function returns numbers which are evenly distributed with respect to interval. Here the start and stop both are included. \n",
"*It has the following parameters: \n",
" * start: start of interval range. By default start = 0\n",
" * stop: end of interval range\n",
" * num : No. of samples to generate. By default num = 50"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "AvnueiSGdhb7",
"outputId": "26ba2de3-6dd5-4952-cf1c-6e9edb6b6fdf"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([0. , 0.10204082, 0.20408163, 0.30612245, 0.40816327,\n",
" 0.51020408, 0.6122449 , 0.71428571, 0.81632653, 0.91836735,\n",
" 1.02040816, 1.12244898, 1.2244898 , 1.32653061, 1.42857143,\n",
" 1.53061224, 1.63265306, 1.73469388, 1.83673469, 1.93877551,\n",
" 2.04081633, 2.14285714, 2.24489796, 2.34693878, 2.44897959,\n",
" 2.55102041, 2.65306122, 2.75510204, 2.85714286, 2.95918367,\n",
" 3.06122449, 3.16326531, 3.26530612, 3.36734694, 3.46938776,\n",
" 3.57142857, 3.67346939, 3.7755102 , 3.87755102, 3.97959184,\n",
" 4.08163265, 4.18367347, 4.28571429, 4.3877551 , 4.48979592,\n",
" 4.59183673, 4.69387755, 4.79591837, 4.89795918, 5. ])"
]
},
"metadata": {},
"execution_count": 9
}
],
"source": [
"matrix2 = np.linspace(0,5) # by default 50 evenly spaced values will be generated between 0 and 5\n",
"matrix2"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zR_AydpiBToe"
},
"source": [
"**How are these values getting generated?**\n",
"\n",
"The step size or the difference between each element will be decided by the following formula:\n",
"\n",
"**(stop - start) / (total elements - 1)**\n",
"\n",
"So, in this case:\n",
"(5 - 0) / 49 = 0.10204082\n",
"\n",
"The first value will be 0.10204082, the second value will be 0.10204082 + 0.10204082, the third value will be 0.10204082 + 0.10204082 +0.10204082, and so on."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "0VDGGmj2dhPp",
"outputId": "8d2953d8-9e77-4346-e09d-3d7c973befeb"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([10. , 11.11111111, 12.22222222, 13.33333333, 14.44444444,\n",
" 15.55555556, 16.66666667, 17.77777778, 18.88888889, 20. ])"
]
},
"metadata": {},
"execution_count": 10
}
],
"source": [
"# generating 10 evenly spaced values between 10 and 20\n",
"matrix3 = np.linspace(10,20,10)\n",
"matrix3"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8Nxn51w2dhEE"
},
"source": [
"**Similarly we can create matrices using the functions available in NumPy library**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "x9brl-h9dg2b"
},
"source": [
"**Using np.zeros()**\n",
"\n",
"* The np.zeros() is a function for creating a matrix and performing matrix operations in NumPy.\n",
"* It returns a matrix filled with zeros of the given shape.\n",
"* It has the following parameters: \n",
" * shape : Number of rows and columns in the output matrix.\n",
" * dtype: data type of the elements in the matrix, by default the value is set to `float`."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "zcfKm8ENjNQv",
"outputId": "32ea17dd-77dd-4d79-e890-29c2201d5c1d"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0]])"
]
},
"metadata": {},
"execution_count": 11
}
],
"source": [
"matrix4 = np.zeros([3,5], int)\n",
"matrix4"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8jy4YykQjICK"
},
"source": [
"**Using np.ones()**\n",
"\n",
"* The np.ones() is another function for creating a matrix and performing matrix operations in NumPy.\n",
"* It returns a matrix of given shape and type, filled with ones.\n",
"* It has the following parameters: \n",
" * shape : Number of rows and columns in the output matrix.\n",
" * dtype: data type of the elements in the matrix, by default the value is set to `float`."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JJ-hv5dcjRN1",
"outputId": "da341cd5-f20e-4eab-e906-e6baeee530d8"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1]])"
]
},
"metadata": {},
"execution_count": 12
}
],
"source": [
"matrix5 = np.ones([3,5], int)\n",
"matrix5"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ge-0i4W2jH4S"
},
"source": [
"**Using np.eye()**\n",
"* The np.eye() is a function for creating a matrix and performing matrix operations in NumPy.\n",
"* It returns a matrix with ones on the diagonal and zeros elsewhere.\n",
"* It has the following parameters:\n",
" * n: Number of rows and columns in the output matrix\n",
" * dtype: data type of the elements in the matrix, by default the value is set to `float`."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2a0_OXeWjHou",
"outputId": "86a3340b-47b5-494f-c5b2-b9c2fe60283f"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 0, 0, 0, 0],\n",
" [0, 1, 0, 0, 0],\n",
" [0, 0, 1, 0, 0],\n",
" [0, 0, 0, 1, 0],\n",
" [0, 0, 0, 0, 1]])"
]
},
"metadata": {},
"execution_count": 13
}
],
"source": [
"matrix6 = np.eye(5, dtype=int)\n",
"matrix6"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DkJp6gB1dgHl"
},
"source": [
"**We can also convert a one dimension array to a matrix. This can be done by using the np.reshape() function.**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ifxHtcyyuVsX"
},
"source": [
"* The shape of an array basically tells the number of elements and dimensions of the array. Reshaping a Numpy array simply means changing the shape of the given array.\n",
"* By reshaping an array we can add or remove dimensions or change number of elements in each dimension.\n",
"* In order to reshape a NumPy array, we use the reshape method with the given array.\n",
"* **Syntax:** array.reshape(shape)\n",
" * shape: a tuple given as input, the values in tuple will be the new shape of the array."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "iYbHCKdPk5hb",
"outputId": "df5584fa-c8f2-45ff-e291-82c3385ed8a5"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"metadata": {},
"execution_count": 14
}
],
"source": [
"# defining an array with values 0 to 9\n",
"arr4 = np.arange(0,10)\n",
"arr4"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "SE6wnu1MlbXv",
"outputId": "b4372713-4dfc-41ae-9d4f-ae3f2b9da68a"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[0, 1, 2, 3, 4],\n",
" [5, 6, 7, 8, 9]])"
]
},
"metadata": {},
"execution_count": 15
}
],
"source": [
"# reshaping the array arr4 to a 2 x 5 matrix\n",
"arr4_reshaped = arr4.reshape((2,5))\n",
"arr4_reshaped"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "KbuGcnPLFT2m",
"outputId": "051b4e03-4b5c-4c5e-d248-a08fd5b24584"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"metadata": {},
"execution_count": 16
}
],
"source": [
"arr4"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 187
},
"id": "HRqcE-TT_-dQ",
"outputId": "899675d0-9a21-4b26-ced6-edc5a4aeb3e4"
},
"outputs": [
{
"output_type": "error",
"ename": "ValueError",
"evalue": "ignored",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-49-d52ee4fd36fa>\u001b[0m in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# reshaping the array arr4 to a 2 x 6 matrix\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0marr4\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: cannot reshape array of size 10 into shape (2,6)"
]
}
],
"source": [
"# reshaping the array arr4 to a 2 x 6 matrix\n",
"arr4.reshape((2,6))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7DGMoYswCocf"
},
"source": [
"* This did not work because we have 10 elements which we are trying to fit in a 2 X 6 shape which will require 12 elements."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lNd_TddVQtWI"
},
"source": [
"**NumPy can also perform a large number of different mathematical operations and it provides different functions to do so.**\n",
"\n",
"NumPy provides:\n",
"1. Trigonometric functions\n",
"2. Exponents and Logarithmic functions\n",
"3. Functions for arithmetic operations between arrays and matrices"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "CE9wPRdYn5IB"
},
"source": [
"**Trigonometric functions**"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6iKAU71XQBXn",
"outputId": "94a33271-e982-4392-ac68-8bdacf0cfb17"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Sine Function: -0.7568024953079282\n",
"Cosine Function: -0.6536436208636119\n",
"Tan Function 1.1578212823495775\n"
]
}
],
"source": [
"print('Sine Function:',np.sin(4))\n",
"print('Cosine Function:',np.cos(4))\n",
"print('Tan Function',np.tan(4))\n",
"arr5 = np.array([1, 2, 3]) * np.array([4, 5, 6])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9QWy471mQEXT"
},
"source": [
"**Exponents and Logarithmic functions**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BBKFBoABoyRx"
},
"source": [
"* Exponents"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "aanbLXltoDgt",
"outputId": "f437d22f-8db6-4eea-fd2d-144d3b53a487"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"7.38905609893065"
]
},
"metadata": {},
"execution_count": 18
}
],
"source": [
"np.exp(2)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "9VUfgN_oomI7",
"outputId": "1f9f9fc2-4d7f-435e-b7d2-7d97398d89df"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([ 7.3890561 , 54.59815003, 403.42879349])"
]
},
"metadata": {},
"execution_count": 19
}
],
"source": [
"arr5 = np.array([2,4,6])\n",
"np.exp(arr5)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BgTCC-v6ouZn"
},
"source": [
"* Logarithms"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "7V8RWfDipHOQ",
"outputId": "4c995c1a-d5fb-4dc8-f473-8d032eb708ac"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.6931471805599453"
]
},
"metadata": {},
"execution_count": 20
}
],
"source": [
"# by default NumPy takes the base of log as e\n",
"np.log(2)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "CH5eCec2pJLo",
"outputId": "b79e6102-ffae-421f-fbcd-f91f501e3515"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([0.69314718, 1.38629436, 1.79175947])"
]
},
"metadata": {},
"execution_count": 21
}
],
"source": [
"np.log(arr5)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bgam8aXTpOVc",
"outputId": "eb14a9bb-9c2b-471a-bd2e-f3ccaa434d64"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.9030899869919435"
]
},
"metadata": {},
"execution_count": 22
}
],
"source": [
"## log with base 10\n",
"np.log10(8)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jDCujvq0pWZr"
},
"source": [
"**Arithmetic Operations on arrays**"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "SXFDL0TeKJZT",
"outputId": "cffa7ada-11ce-4d3a-a998-2d22e9dd5f86"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[1, 2, 3, 4, 5, 6]\n"
]
}
],
"source": [
"# arithmetic on lists\n",
"\n",
"l1 = [1,2,3]\n",
"l2 = [4,5,6]\n",
"print(l1+l2)\n",
"# this does not behave as you would expect!\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "MHZKZFP8ppQx",
"outputId": "de7f9781-8e79-4877-e4f4-eb40bdfa3ffe"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"arr7: [1 2 3 4 5]\n",
"arr8: [3 4 5 6 7]\n"
]
}
],
"source": [
"# we can +-*/ arrays together\n",
"\n",
"# defining two arrays\n",
"arr7 = np.arange(1,6)\n",
"print('arr7:', arr7)\n",
"\n",
"arr8 = np.arange(3,8)\n",
"print('arr8:', arr8)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "xkMkQOL8rHOw",
"outputId": "95c2d3ce-279e-4bf6-8239-7c22251b018e"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Addition: [ 4 6 8 10 12]\n",
"Subtraction: [2 2 2 2 2]\n",
"Multiplication: [ 3 8 15 24 35]\n",
"Division: [0.33333333 0.5 0.6 0.66666667 0.71428571]\n",
"Inverse: [1. 0.5 0.33333333 0.25 0.2 ]\n",
"Powers: [ 1 16 243 4096 78125]\n"
]
}
],
"source": [
"print('Addition: ',arr7+arr8)\n",
"print('Subtraction: ',arr8-arr7)\n",
"print('Multiplication:' , arr7*arr8)\n",
"print('Division:', arr7/arr8)\n",
"print('Inverse:', 1/arr7)\n",
"print('Powers:', arr7**arr8) # in python, powers are achieved using **, NOT ^!!! ^ does something completely different!"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SrE8fcrFtt7c"
},
"source": [
"**Operations on Matrices**"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "p-lZfDhUrb2b",
"outputId": "e2927bb1-a846-4a96-df8b-9536b3eaf0df"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]\n",
"[[1. 0. 0.]\n",
" [0. 1. 0.]\n",
" [0. 0. 1.]]\n"
]
}
],
"source": [
"matrix7 = np.arange(1,10).reshape(3,3)\n",
"print(matrix7)\n",
"\n",
"matrix8 = np.eye(3)\n",
"print(matrix8)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "VGBAxsUUtyts",
"outputId": "9cff6d58-8e38-4e0f-924b-2835cb11d7e2"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Addition: \n",
" [[ 2. 2. 3.]\n",
" [ 4. 6. 6.]\n",
" [ 7. 8. 10.]]\n",
"Subtraction: \n",
" [[0. 2. 3.]\n",
" [4. 4. 6.]\n",
" [7. 8. 8.]]\n",
"Multiplication: \n",
" [[1. 0. 0.]\n",
" [0. 5. 0.]\n",
" [0. 0. 9.]]\n",
"Division: \n",
" [[ 1. inf inf]\n",
" [inf 5. inf]\n",
" [inf inf 9.]]\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-27-b49c47b4c85a>:4: RuntimeWarning: divide by zero encountered in true_divide\n",
" print('Division: \\n', matrix7/matrix8)\n"
]
}
],
"source": [
"print('Addition: \\n', matrix7+matrix8)\n",
"print('Subtraction: \\n ', matrix7-matrix8)\n",
"print('Multiplication: \\n', matrix7*matrix8)\n",
"print('Division: \\n', matrix7/matrix8)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "c7jHF8Od6mHH"
},
"source": [
"* RuntimeWarning: Errors which occur during program execution(run-time) after successful compilation are called run-time errors.\n",
"* One of the most common run-time error is division by zero also known as Division error.\n",
"* Due to division by zero error, we are getting inf (infinity) values because 1/0 is not a defined operation."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3WYcA-OxyL9Y"
},
"source": [
"**Linear algebra matrix multiplication**"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "nc0PAuG3sM6A",
"outputId": "47d3f875-c3e5-4ccf-e9d0-2913f7a25449"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"First Matrix: \n",
" [[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]\n",
"Second Matrix: \n",
" [[11 12 13]\n",
" [14 15 16]\n",
" [17 18 19]]\n",
"\n",
"Multiplication: \n",
" [[ 90 96 102]\n",
" [216 231 246]\n",
" [342 366 390]]\n"
]
}
],
"source": [
"matrix9 = np.arange(1,10).reshape(3,3)\n",
"print('First Matrix: \\n',matrix9)\n",
"\n",
"matrix10 = np.arange(11,20).reshape(3,3)\n",
"print('Second Matrix: \\n',matrix10)\n",
"print('')\n",
"# taking linear algebra matrix multiplication (some may have heard this called the dot product)\n",
"print('Multiplication: \\n', matrix9 @ matrix10)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "LQVkMw7Iynfu"
},
"source": [
"**Transpose of a matrix**"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "DJp3NtsrsSM4",
"outputId": "0ba6c5be-fd5a-4cfa-b1d1-24071c37091c"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]\n"
]
}
],
"source": [
"print(matrix9)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "MDKJgUlptKet",
"outputId": "e603b8a5-0c67-4860-b40c-8a9f6beafa3f"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 4, 7],\n",
" [2, 5, 8],\n",
" [3, 6, 9]])"
]
},
"metadata": {},
"execution_count": 30
}
],
"source": [
"# taking transpose of matrix\n",
"np.transpose(matrix9)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PwwpvruatMRj",
"outputId": "b17c2a34-31a2-42ab-a91e-d0cf53188a8d"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 4, 7],\n",
" [2, 5, 8],\n",
" [3, 6, 9]])"
]
},
"metadata": {},
"execution_count": 34
}
],
"source": [
"# another way of taking a transpose\n",
"matrix9.T"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "YSl_vQk70bCS"
},
"source": [
"**Function to find minimum and maximum values**"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TmPZB7tu0a2c",
"outputId": "ed9532e9-cdd8-4046-fe5a-8bbee34e5429"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]\n"
]
}
],
"source": [
"print(matrix9)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "394LL_BZ0alr",
"outputId": "70a750a3-4de8-4c18-a34c-37f54659a39e"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Minimum value: 1\n"
]
}
],
"source": [
"print('Minimum value: ',np.min(matrix9))"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "SVs2Zx0J0ac3",
"outputId": "462dea03-0aeb-4e66-c57a-bc1060c696f8"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Maximum value: 9\n"
]
}
],
"source": [
"print('Maximum value: ',np.max(matrix9))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7NNtvTmh0aRf"
},
"source": [
"**Function to generate random samples**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "XO47Cq8L3Gb3"
},
"source": [
"**Using np.random.rand function**\n",
"\n",
"* The np.random.rand returns a random NumPy array whose element(s) are drawn randomly from the uniform distribution over [0,1). (including 0 but excluding 1).\n",
"* **Syntax** - np.random.rand(d0,d1)\n",
" * d0,d1 – It represents the dimension of the required array given as int, where d1 is optional."
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Neq3fBGH4N7F",
"outputId": "39288253-a7c6-4c58-8451-395621195c83"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[0.09506601 0.61843086 0.61155394 0.45849354 0.0153768 ]\n"
]
}
],
"source": [
"# Generating random values in an array\n",
"rand_mat = np.random.rand(5)\n",
"print(rand_mat)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "QQHFrAce1570",
"outputId": "c0443d55-24b1-4163-902b-2316afce0010"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[0.19650063 0.62160157 0.28771949 0.39957183 0.98671675]\n",
" [0.57810449 0.24550967 0.36045128 0.26311002 0.18647465]\n",
" [0.2987361 0.16783433 0.79797341 0.3401105 0.07821192]\n",
" [0.78240333 0.73789065 0.29800614 0.78794543 0.48625692]\n",
" [0.47253055 0.42825313 0.89606573 0.5187848 0.74745477]]\n"
]
}
],
"source": [
"# * Generating random values in a matrix\n",
"rand_mat = np.random.rand(5,5) # uniform random variable\n",
"print(rand_mat)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VvvK-7Sh3PfD"
},
"source": [
"**Using np.random.randn function**\n",
"\n",
"* The np.random.randn returns a random numpy array whose sample(s) are drawn randomly from the standard normal distribution (Mean as 0 and standard deviation as 1)\n",
"\n",
"* **Syntax** - np.random.randn(d0,d1)\n",
" * d0,d1 – It represents the dimension of the output, where d1 is optional."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Ll_boKdQ4Y-b",
"outputId": "f443cea8-1e65-407c-d76a-839f2cbde39e"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[-1.33254388 0.41922042 -0.30308399 0.37278855 -1.0863481 ]\n"
]
}
],
"source": [
"# Generating random values in an array\n",
"rand_mat2 = np.random.randn(5)\n",
"print(rand_mat2)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1jaiT-oZ2CyD",
"outputId": "89bb1dae-b3af-4475-8198-43d0ab9b8ce6"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[ 0.02786921 0.57655235 -1.29669096 0.04032993 0.46529421]\n",
" [ 0.89440374 0.07769757 1.75923548 0.80074266 -0.64188104]\n",
" [ 1.60873923 -0.15425246 -0.40834629 0.19645385 -0.10751853]\n",
" [-0.47531526 0.49572962 -1.23680006 -1.20654867 1.07003152]\n",
" [-0.07462871 -0.44763354 -0.33138688 -1.68072604 -1.36935295]]\n"
]
}
],
"source": [
"# Generating random values in a matrix\n",
"rand_mat2 = np.random.randn(5,5)\n",
"print(rand_mat2)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TldcCrOS5ulV",
"outputId": "7ff974c5-d701-472e-d95a-1c0c900858c2"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Mean: -0.05672008057926228\n",
"Standard Deviation: 0.8843740846344249\n"
]
}
],
"source": [
"# Let's check the mean and standard deviation of rand_mat2\n",
"print('Mean:',np.mean(rand_mat2))\n",
"print('Standard Deviation:',np.std(rand_mat2))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "66BnHAPW6Ere"
},
"source": [
"* We observe that the mean is very close to 0 and standard deviation is very close to 1."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "T_0yFxGh3U2D"
},
"source": [
"**Using np.random.randint function**\n",
"\n",
"* The np.random.randint returns a random numpy array whose element(s) are drawn randomly from low (inclusive) to the high (exclusive) range.\n",
"\n",
"* **Syntax** - np.random.randint(low, high, size)\n",
"\n",
" * low – It represents the lowest inclusive bound of the distribution from where the sample can be drawn.\n",
" * high – It represents the upper exclusive bound of the distribution from where the sample can be drawn.\n",
" * size – It represents the shape of the output."
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vLPM7a0N2EHA",
"outputId": "9a5c8bad-9fa6-4390-c7b0-7027e964774a"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[1 4 2 1 2 4 3 4 4 1]\n"
]
}
],
"source": [
"# Generating random values in an array\n",
"rand_mat3 = np.random.randint(1,5,10)\n",
"print(rand_mat3)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "u9YqKltk486n",
"outputId": "bcd1e554-502a-4643-a12d-58d51aa832fa"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[9 7 8 6 6]\n",
" [7 4 6 6 5]\n",
" [3 9 3 4 7]\n",
" [2 4 4 7 3]\n",
" [7 5 2 6 5]]\n"
]
}
],
"source": [
"# Generating random values in a matrix\n",
"rand_mat3 = np.random.randint(1,10,[5,5])\n",
"print(rand_mat3)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "J-K_ecWJ6M6n"
},
"source": [
"### 2.3 Accessing the entries of a Numpy Array"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4Tiy6KJw3a5A",
"outputId": "78c3882c-f84f-478f-c685-ce995fa8f3b6"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[-0.16183368 2.73952017 -0.36154651 0.54878698 -0.60722881 0.71611501\n",
" -1.19855612 0.09346848 0.34621684 -0.10107636]\n"
]
}
],
"source": [
"# let's generate an array with 10 random values\n",
"rand_arr = np.random.randn(10)\n",
"print(rand_arr)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gJlNtFKwaZUv"
},
"source": [
"* Accessing one element from an array"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PtSqbI-Y3awc",
"outputId": "09f24633-0ca4-49c7-fdb3-310906daae84"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"0.7161150052883691\n"
]
}
],
"source": [
"# accessing the 6 th entry of rand_arr\n",
"print(rand_arr[5])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ywt2eQx-aZUv"
},
"source": [
"* Accessing multiple elements from an array"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "HZ8Yb_3h3amG",
"outputId": "0c3cb09d-7156-410e-a50c-dc7722a1b7d4"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[-0.60722881 0.71611501 -1.19855612 0.09346848 0.34621684]\n"
]
}
],
"source": [
"# we can access multiple entries at once using\n",
"print(rand_arr[4:9])"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pNcRsFbK3aZP",
"outputId": "8e88b979-acac-4c08-9b1d-75855b8f13f2"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Index of values to access: [3 6 9]\n",
"[ 0.54878698 -1.19855612 -0.10107636]\n"
]
}
],
"source": [
"# we can also access multiple non-consecutive entries using np.arange\n",
"print('Index of values to access: ',np.arange(3,10,3))\n",
"print(rand_arr[np.arange(3,10,3)])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "m0_dOkPKc7bL"
},
"source": [
"**Accessing arrays using logical operations**"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_8v77EwOcwPM",
"outputId": "ae3c80b4-fedc-4b32-8c38-7d83b9329140"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[-0.16183368 2.73952017 -0.36154651 0.54878698 -0.60722881 0.71611501\n",
" -1.19855612 0.09346848 0.34621684 -0.10107636]\n"
]
}
],
"source": [
"print(rand_arr)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "u7QvbkGfc_Mf",
"outputId": "570c7798-02e4-47ef-ab3a-0f961a30bfbf"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([False, True, False, True, False, True, False, True, True,\n",
" False])"
]
},
"metadata": {},
"execution_count": 52
}
],
"source": [
"rand_arr>0"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PwjFRjDiczAS",
"outputId": "51dfd590-c080-4a85-d1c2-77d8dd676046"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Values greater than 0: [2.73952017 0.54878698 0.71611501 0.09346848 0.34621684]\n",
"Values less than 0: [-0.16183368 -0.36154651 -0.60722881 -1.19855612 -0.10107636]\n"
]
}
],
"source": [
"# accessing all the values of rand_arr which are greater than 0\n",
"print('Values greater than 0: ',rand_arr[rand_arr>0])\n",
"\n",
"# accessing all the values of rand_arr which are less than 0\n",
"print('Values less than 0: ',rand_arr[rand_arr<0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9j2nb_lX3aKJ"
},
"source": [
"**Accessing the entries of a Matrix**"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "rUT3MtuX3Z-o",
"outputId": "3ed8eb2f-27bc-402a-b76a-3ab83d20815b"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[-1.18391369 0.64901481 -0.68621479 -0.49487068 -0.2240231 ]\n",
" [-1.17820168 1.60253903 -1.15104259 -0.72179819 0.24312261]\n",
" [-0.83775634 1.25725097 -0.66977586 -0.14623422 -0.52323625]\n",
" [ 0.38342804 0.01875837 -0.04255999 0.15891483 -0.56209013]\n",
" [ 0.79072893 0.0212678 -0.19200103 0.97387792 -1.03155233]]\n"
]
}
],
"source": [
"# let's generate an array with 10 random values\n",
"rand_mat = np.random.randn(5,5)\n",
"print(rand_mat)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "aH5KNWYJ8AAi",
"outputId": "18f28b2d-c058-428d-afdf-6cf08a591d71"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([-1.17820168, 1.60253903, -1.15104259, -0.72179819, 0.24312261])"
]
},
"metadata": {},
"execution_count": 55
}
],
"source": [
"# accessing the second row of the rand_mat\n",
"rand_mat[1]"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "cKPauEeS3Zya",
"outputId": "d63ee4ed-c287-4c24-ec06-34e971c03b6b"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"-1.1510425924481529\n",
"-1.1510425924481529\n"
]
}
],
"source": [
"# accessing third element of the second row\n",
"print(rand_mat[1][2])\n",
"\n",
"#or\n",
"\n",
"print(rand_mat[1,2])"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "md90xMiX3ZhX",
"outputId": "28b50954-fec8-4b8a-8620-c9a728f33d69"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[ 0.64901481 -0.68621479]\n",
" [ 1.60253903 -1.15104259]]\n"
]
}
],
"source": [
"# accessing first two rows with second and third column\n",
"print(rand_mat[0:2,1:3])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZQIPaCTxdTkx"
},
"source": [
"**Accessing matrices using logical operations**"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "FPWAgqcMdTkx",
"outputId": "315328f4-758b-47b0-eee3-15764bbb6801"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[-1.18391369 0.64901481 -0.68621479 -0.49487068 -0.2240231 ]\n",
" [-1.17820168 1.60253903 -1.15104259 -0.72179819 0.24312261]\n",
" [-0.83775634 1.25725097 -0.66977586 -0.14623422 -0.52323625]\n",
" [ 0.38342804 0.01875837 -0.04255999 0.15891483 -0.56209013]\n",
" [ 0.79072893 0.0212678 -0.19200103 0.97387792 -1.03155233]]\n"
]
}
],
"source": [
"print(rand_mat)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "5pBwxeo_dTky",
"outputId": "87170d41-6a09-4b8c-b8d9-0c5f0d0487c8"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Values greater than 0: \n",
" [0.64901481 1.60253903 0.24312261 1.25725097 0.38342804 0.01875837\n",
" 0.15891483 0.79072893 0.0212678 0.97387792]\n",
"Values less than 0: \n",
" [-1.18391369 -0.68621479 -0.49487068 -0.2240231 -1.17820168 -1.15104259\n",
" -0.72179819 -0.83775634 -0.66977586 -0.14623422 -0.52323625 -0.04255999\n",
" -0.56209013 -0.19200103 -1.03155233]\n"
]
}
],
"source": [
"# accessing all the values of rand_mat which are greater than 0\n",
"print('Values greater than 0: \\n ',rand_mat[rand_mat>0])\n",
"\n",
"# accessing all the values of rand_mat which are less than 0\n",
"print('Values less than 0: \\n',rand_mat[rand_mat<0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Rbc_yo5T8mmS"
},
"source": [
"**Modifying the entries of an Array**"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2P57NUFJ8mMA",
"outputId": "04f757c0-5ee0-4a2f-fe4c-a624016dc228"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[-0.16183368 2.73952017 -0.36154651 0.54878698 -0.60722881 0.71611501\n",
" -1.19855612 0.09346848 0.34621684 -0.10107636]\n"
]
}
],
"source": [
"print(rand_arr)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "5dTfDf2d-DF7",
"outputId": "1a89292c-0f27-4ce5-fde7-5c68bcd80530"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[-0.16183368 2.73952017 -0.36154651 5. 5. 0.71611501\n",
" -1.19855612 0.09346848 0.34621684 -0.10107636]\n"
]
}
],
"source": [
"# let's change some values in an array!\n",
"# changing the values of index value 3 and index value 4 to 5\n",
"rand_arr[3:5] = 5\n",
"print(rand_arr)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "dC1yVJLT-H9H",
"outputId": "3b22f9c6-dcf9-4fea-af09-9cd00bf547ea"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[ 2. 3. -0.36154651 5. 5. 0.71611501\n",
" -1.19855612 0.09346848 0.34621684 -0.10107636]\n"
]
}
],
"source": [
"# changing the values of index value 0 and index value 1 to 2 and 3 respectively\n",
"rand_arr[0:2] = [2,3]\n",
"print(rand_arr)"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "wT3ishttMugE",
"outputId": "8625e6b7-379c-4268-b3e2-743ca40b630b"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([65. , 65. , -0.36154651, 65. , 65. ,\n",
" 65. , -1.19855612, 65. , 65. , -0.10107636])"
]
},
"metadata": {},
"execution_count": 63
}
],
"source": [
"# modify entries using logical references\n",
"rand_arr[rand_arr>0] = 65\n",
"rand_arr"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lhoMit_Da8F4"
},
"source": [
"**Modifying the entries of a Matrix**"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "fJ02uz8obsqC",
"outputId": "a582408d-780c-446f-f5bd-46dcf2e2785a"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[9 7 8 6 6]\n",
" [7 4 6 6 5]\n",
" [3 9 3 4 7]\n",
" [2 4 4 7 3]\n",
" [7 5 2 6 5]]\n"
]
}
],
"source": [
"print(rand_mat3)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "dVNbm4oKNtkr",
"outputId": "bad3c252-039d-4ddf-f168-9ad5995a397e"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Matrix before modification: \n",
" [[9 7 8 6 6]\n",
" [7 4 6 6 5]\n",
" [3 9 3 4 7]\n",
" [2 4 4 7 3]\n",
" [7 5 2 6 5]]\n",
"Matrix after modification: \n",
" [[9 7 8 6 6]\n",
" [7 4 6 0 0]\n",
" [3 9 3 0 0]\n",
" [2 4 4 7 3]\n",
" [7 5 2 6 5]]\n"
]
}
],
"source": [
"# changing the values of the 4th and 5th element of the second and third rows of the matrix to 0\n",
"print('Matrix before modification: \\n',rand_mat3)\n",
"rand_mat3[1:3,3:5] = 0\n",
"print('Matrix after modification: \\n',rand_mat3)"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "uP3butowNtvp",
"outputId": "97e9cc6e-ec2b-4310-bc8f-053d3bb959ab"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[-1.18391369 0.64901481 -0.68621479]\n",
" [-1.17820168 1.60253903 -1.15104259]]\n"
]
}
],
"source": [
"# extracting the first 2 rows and first 3 columns from the matrix\n",
"sub_mat = rand_mat[0:2,0:3]\n",
"print(sub_mat)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3DgctOvENt14",
"outputId": "08b5f346-d112-4361-a7df-7942bf7bffa4"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[3. 3. 3.]\n",
" [3. 3. 3.]]\n"
]
}
],
"source": [
"# changing all the values of the extracted matrix to 3\n",
"sub_mat[:] = 3\n",
"print(sub_mat)"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hskgV9vG9lxx",
"outputId": "1c296f65-0ee3-4158-cb31-6a65a5ca11bc"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[ 3. , 3. , 3. , -0.49487068, -0.2240231 ],\n",
" [ 3. , 3. , 3. , -0.72179819, 0.24312261],\n",
" [-0.83775634, 1.25725097, -0.66977586, -0.14623422, -0.52323625],\n",
" [ 0.38342804, 0.01875837, -0.04255999, 0.15891483, -0.56209013],\n",
" [ 0.79072893, 0.0212678 , -0.19200103, 0.97387792, -1.03155233]])"
]
},
"metadata": {},
"execution_count": 68
}
],
"source": [
"# what happened to rand_mat when we change sub_mat?\n",
"rand_mat"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PryCb2h29sHx",
"outputId": "87184be8-bdd9-4a07-b49e-8d0a372ba4df"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[-0.03965304 1.36318224 -0.04922096 0.29419758 0.26768508]\n",
" [-0.81835576 -0.89297441 0.44293089 -1.88025304 0.31368557]\n",
" [ 0.47827403 -0.78097628 0.01919465 -0.11361836 -1.07258383]\n",
" [-0.25520047 -1.27871949 1.25135614 -2.66107776 0.22880422]\n",
" [-2.22548465 -0.30585274 0.37776117 0.34526279 1.18504347]]\n",
"[[3. 3. 3.]\n",
" [3. 3. 3.]]\n",
"[[-0.03965304 1.36318224 -0.04922096 0.29419758 0.26768508]\n",
" [-0.81835576 -0.89297441 0.44293089 -1.88025304 0.31368557]\n",
" [ 0.47827403 -0.78097628 0.01919465 -0.11361836 -1.07258383]\n",
" [-0.25520047 -1.27871949 1.25135614 -2.66107776 0.22880422]\n",
" [-2.22548465 -0.30585274 0.37776117 0.34526279 1.18504347]]\n"
]
}
],
"source": [
"# to prevent this behavior we need to use the .copy() method when we assign sub_mat\n",
"# this behavior is the source of MANY errors for early python users!!!\n",
"\n",
"rand_mat = np.random.randn(5,5)\n",
"print(rand_mat)\n",
"sub_mat = rand_mat[0:2,0:3].copy()\n",
"sub_mat[:] = 3\n",
"print(sub_mat)\n",
"print(rand_mat)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Uhy7xJlW0aDT"
},
"source": [
"### 2.4 Saving and Loading a NumPy array"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qlyONcw5FeSh"
},
"source": [
"**Let's save some NumPy objects on the disk for use later!**"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "gikqL10PzrC1",
"outputId": "e5f6498e-cebd-45c0-ffa1-5bc977058137"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
]
}
],
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "G_w4dYUtFzZO",
"outputId": "8e894f8d-47bf-4fee-e8b8-bf06c00199f5"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[9 7 3 2 8]\n",
" [8 9 8 6 3]]\n",
"\n",
"[[19 16 19 16 11]\n",
" [16 15 10 13 16]]\n"
]
}
],
"source": [
"# creating a random matrices\n",
"randint_matrix1 = np.random.randint(1,10,10).reshape(2,5)\n",
"print(randint_matrix1)\n",
"print('')\n",
"randint_matrix2 = np.random.randint(10,20,10).reshape(2,5)\n",
"print(randint_matrix2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rysUk_pSFU1O"
},
"source": [
"**Using np.save() function**"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {
"id": "k9TyLFJ-tkU2"
},
"outputs": [],
"source": [
"np.save('/content/drive/MyDrive/test_file',randint_matrix1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VMfbQlc2FYNl"
},
"source": [
"**Using np.savez() function**"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {
"id": "dhtVPefIRfGP"
},
"outputs": [],
"source": [
"np.savez('/content/drive/MyDrive/test_filez',randint_matrix1=randint_matrix1,randint_matrix2=randint_matrix2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "hDbZNqdLDj35"
},
"source": [
"* The files will be saved in the directory where the Jupyter Notebook is located.\n",
"* With np.save() function, we can save an array/matrix to a NumPy .npy format.\n",
"* np.savez() function has an advantage over np.save() function because with np.savez(), we can store several arrays/matrices into a single file in uncompressed .npz format."
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {
"id": "h7mYd86cZeTq",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "77c79a3c-5fe7-49a7-e806-5a8b03944c71"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[9 7 3 2 8]\n",
" [8 9 8 6 3]]\n",
"\n",
"<numpy.lib.npyio.NpzFile object at 0x7964615afbb0>\n"
]
}
],
"source": [
"# now let's load it\n",
"loaded_arr = np.load('/content/drive/MyDrive/test_file.npy')\n",
"loaded_multi = np.load('/content/drive/MyDrive/test_filez.npz')\n",
"\n",
"print(loaded_arr)\n",
"print('')\n",
"print(loaded_multi)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "21XxghDDG69T"
},
"source": [
"* We see that .npy file has been loaded but the .npz file is returning a memory location.\n",
"* Let's see how to load the values stored in .npz file."
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {
"id": "VezEM4uPZeZk",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "43a3d283-b65b-4ef6-8ba3-9a8137675386"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1st Matrix: \n",
" [[9 7 3 2 8]\n",
" [8 9 8 6 3]]\n",
"2nd Matrix: \n",
" [[19 16 19 16 11]\n",
" [16 15 10 13 16]]\n",
"New Matrix: \n",
" [[9 7 3 2 8]\n",
" [8 9 8 6 3]]\n"
]
}
],
"source": [
"print('1st Matrix: \\n',loaded_multi['randint_matrix1'])\n",
"print('2nd Matrix: \\n',loaded_multi['randint_matrix2'])\n",
"\n",
"new_matrix = loaded_multi['randint_matrix1']\n",
"print('New Matrix: \\n',new_matrix)"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {
"id": "AyyZjATxZefd",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "025a5c91-4f3d-4f4e-e621-feacb4345c97"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[9 7 3 2 8]\n",
" [8 9 8 6 3]]\n",
"\n",
"[[9. 7. 3. 2. 8.]\n",
" [8. 9. 8. 6. 3.]]\n"
]
}
],
"source": [
"# we can also save/load text files...but only single variables\n",
"np.savetxt('/content/drive/MyDrive/text_file_name.txt',randint_matrix1,delimiter=',')\n",
"rand_mat_txt = np.loadtxt('/content/drive/MyDrive/text_file_name.txt',delimiter=',')\n",
"print(randint_matrix1)\n",
"print('')\n",
"print(rand_mat_txt)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "WiTOg-jNzE82"
},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment