Skip to content

Added NumPy notebooks #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
497 changes: 497 additions & 0 deletions NumPy/Lesson 1 - Introduction.ipynb

Large diffs are not rendered by default.

634 changes: 634 additions & 0 deletions NumPy/Lesson 2 - Dimensions.ipynb

Large diffs are not rendered by default.

1,279 changes: 1,279 additions & 0 deletions NumPy/Lesson 3 - Basic Operations.ipynb

Large diffs are not rendered by default.

1,383 changes: 1,383 additions & 0 deletions NumPy/Lesson 4 - Indexing and Slicing.ipynb

Large diffs are not rendered by default.

325 changes: 325 additions & 0 deletions NumPy/Lesson 5 - Universal Functions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,325 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Universal Functions\n",
"\n",
"NumPy provides some useful out of the box functions to perform operations on matrices. These functions are divided into 2 different segments:\n",
"1. Unary Functions\n",
"2. Binary Functions\n",
"\n",
"A Unary function operates on a single matrix by itself, while a binary function requires 2 matrices. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Unary Functions\n",
"\n",
"## np.sqrt\n",
"This function simply returns square root of all the elements in an array."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2., 3., 4.],\n",
" [5., 6., 7.]])"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"a = np.array([[4,9,16],\n",
" [25,36,49]])\n",
"\n",
"np.sqrt(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## np.exp\n",
"This function computes the exponent of each element in an array."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[5.45981500e+01, 8.10308393e+03, 8.88611052e+06],\n",
" [7.20048993e+10, 4.31123155e+15, 1.90734657e+21]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.exp(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## np.log\n",
"This function will compute the natural logarithm (base e) of each element in an array. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1.38629436, 2.19722458, 2.77258872],\n",
" [3.21887582, 3.58351894, 3.8918203 ]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.log(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly, we can also use **log10, log2, logp**."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## np.rint\n",
"This function rounds elements to their nearest integer. The function `rint` can be read as `Round to Integer`"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 2., 3.],\n",
" [3., 4., 4.]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([[1.38629436, 2.19722458, 2.77258872],\n",
" [3.21887582, 3.58351894, 3.8918203 ]])\n",
"np.rint(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](./images/ufuncs.JPG)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Binary Functions\n",
"\n",
"Lets start with 2 matrices and perform some basic arithmatic operations on them. "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"a = np.array([[1,2,3],\n",
" [4,5,6]])\n",
"\n",
"b = np.array([[2,4,8],\n",
" [16,32,64]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Addition"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 3, 6, 11],\n",
" [20, 37, 70]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a+b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Subtraction"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ -1, -2, -5],\n",
" [-12, -27, -58]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a-b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Multiplication"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2, 8, 24],\n",
" [ 64, 160, 384]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a*b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Division"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.5 , 0.5 , 0.375 ],\n",
" [0.25 , 0.15625, 0.09375]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a/b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are several binary functions that one can choose from. The table below shows some of the binary function operations available.\n",
"\n",
"> Keep in mind that the matrices have to always be of appropriate size for the binary operation to work. We cannot add a (2,2) matrix to a (3x3) matrix. If you can perform the matrix operation mathematically, then the system can also perform it for you. But it will fail on attempting an invalid matrix operation.\n",
"\n",
"\n",
"![](./images/binary_funcs.JPG)"
]
}
],
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading