 abhishek-p/numpy-array-operations - Jovian
Learn data science and machine learning by building real-world projects on Jovian

Assignment 2 - Numpy Array Operations

This assignment is part of the course "Data Analysis with Python: Zero to Pandas". The objective of this assignment is to develop a solid understanding of Numpy array operations. In this assignment you will:

1. Pick 5 interesting Numpy array functions by going through the documentation: https://numpy.org/doc/stable/reference/routines.html
2. Run and modify this Jupyter notebook to illustrate their usage (some explanation and 3 examples for each function). Use your imagination to come up with interesting and unique examples.
3. Upload this notebook to your Jovian profile using jovian.commit and make a submission here: https://jovian.ml/learn/data-analysis-with-python-zero-to-pandas/assignment/assignment-2-numpy-array-operations
5. (Optional) Check out the notebooks shared by other participants and give feedback & appreciation.

The recommended way to run this notebook is to click the "Run" button at the top of this page, and select "Run on Binder". This will run the notebook on mybinder.org, a free online service for running Jupyter notebooks.

Try to give your notebook a catchy title & subtitle e.g. "All about Numpy array operations", "5 Numpy functions you didn't know you needed", "A beginner's guide to broadcasting in Numpy", "Interesting ways to create Numpy arrays", "Trigonometic functions in Numpy", "How to use Python for Linear Algebra" etc.

NOTE: Remove this block of explanation text before submitting or sharing your notebook online - to make it more presentable.

Title Here

Subtitle Here

Write a short introduction about Numpy and list the chosen functions.

• function 1
• function 2
• function 3
• function 4
• function 5

The recommended way to run this notebook is to click the "Run" button at the top of this page, and select "Run on Binder". This will run the notebook on mybinder.org, a free online service for running Jupyter notebooks.

import jovian
jovian.commit(project='numpy-array-operations')
[jovian] Attempting to save notebook.. [jovian] Updating notebook "abhishek-p/numpy-array-operations" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/abhishek-p/numpy-array-operations

Let's begin by importing Numpy and listing out the functions covered in this notebook.

import numpy as np
# List of functions explained
function1 = np.sort
function2 = np.argmax
function3 = np.count_nonzero
function4 = np.around
function5 = np.linalg.solve

Function 1 - np.sort

With explanations

# Example 1 - working (change this)
arr1 = [[1, 2],
[3, 1]]
np.sort(arr1)
array([[1, 2],
[1, 3]])

Explanation about example by default it sort along axis=-1 which is last axis(along columns)

# Example 2 - working
arr2 = [[5, 6, 7],
[2, 9, 4]]
np.sort(arr2,axis=0)
array([[2, 6, 4],
[5, 9, 7]])

Explanation about above example axis 0 means it will sort the array along rows(first axis)

# Example 3
arr3=[[9,8,3],
[12,3,0]]
np.sort(arr3,axis=1)
array([[ 3,  8,  9],
[ 0,  3, 12]])

Explanation about above example axis 1 means it will sort the array along columns(second axis or int this case lsat axis )

np.sort(arr3,axis=None)
array([ 0,  3,  3,  8,  9, 12])

axis=None so it gives a sorted flattened array

# Example 4 - breaking (to illustrate when it breaks)

np.concatenate(arr3, axis=2)
--------------------------------------------------------------------------- AxisError Traceback (most recent call last) <ipython-input-10-1bf74f1e5536> in <module> 1 # Example 4 - breaking (to illustrate when it breaks) 2 ----> 3 np.concatenate(arr3, axis=2) <__array_function__ internals> in concatenate(*args, **kwargs) AxisError: axis 2 is out of bounds for array of dimension 1

Explanation about above example (why it breaks and how to fix it) as the array has only two dimensions axis can have only two values either 0 or 1. you can also see the original documentationhttps://numpy.org/doc/stable/reference/generated/numpy.sort.html#numpy.sort

Some closing comments about when to use this function.It's simple to use whenever you want to sort the element of the array.

jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "abhishek-p/numpy-array-operations" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/abhishek-p/numpy-array-operations

Function 2 - np.argmax

With explanations

# Example 1 - working
arr3=[[9,8,3],
[12,3,0],
[12,34,56]]
np.argmax(arr3,axis=1)
array([0, 0, 2])

Explanation about example-argmax function take an array as a parameter and if axis is specified it returns the indices of the max values along that axis like in the above example axis = 1 so it give indices of all the maximum values along each row

# Example 2 - working
arr3=[[9,8,3],
[12,3,0],
[12,34,56]]
np.argmax(arr3)
8

if no axis arg provided by default it provided the index of the maximum value in the flattened array.

# Example 3
arr3=[[9,8,3],
[12,3,0],
[12,34,56]]
np.argmax(arr3,axis=0)
array([1, 2, 2])

Explanation about example- In the above example axis = 1 so it give indices of all the maximum values along each column.

# Example 4 - breaking (to illustrate when it breaks)
np.argmax(arr3,axis=2)
--------------------------------------------------------------------------- AxisError Traceback (most recent call last) <ipython-input-15-130ecc5176dc> in <module> 1 # Example 4 - breaking (to illustrate when it breaks) ----> 2 np.argmax(arr3,axis=2) <__array_function__ internals> in argmax(*args, **kwargs) /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/core/fromnumeric.py in argmax(a, axis, out) 1186 1187 """ -> 1188 return _wrapfunc(a, 'argmax', axis=axis, out=out) 1189 1190 /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds) 53 bound = getattr(obj, method, None) 54 if bound is None: ---> 55 return _wrapit(obj, method, *args, **kwds) 56 57 try: /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/core/fromnumeric.py in _wrapit(obj, method, *args, **kwds) 42 except AttributeError: 43 wrap = None ---> 44 result = getattr(asarray(obj), method)(*args, **kwds) 45 if wrap: 46 if not isinstance(result, mu.ndarray): AxisError: axis 2 is out of bounds for array of dimension 2

Explanation about example (why it breaks and how to fix it)as the array has only two dimensions axis can have only two values either 0 or 1.you can also see the expalation in the original documentationhttps://numpy.org/doc/stable/reference/generated/numpy.argmax.html#numpy.argmax

Some closing comments about when to use this function. whenever you have a large size arrays, the max value along the columns or rows or in the entire array can be identified using argmax function.it returns the indices so you can pass it as function arguements in your code.

jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "abhishek-p/numpy-array-operations" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/abhishek-p/numpy-array-operations

Function 3 - np.count_nonzero

With explanations

# Example 1 - working
arr3=[[9,8,3],
[12,3,0],
[12,34,56]]
np.count_nonzero(arr3)
8

Explanation about example-it returns the no. of non-zero values in the flattened array.

# Example 2 - working
arr3=[[9,8,3],
[12,3,0],
[12,34,56]]
np.count_nonzero(arr3,axis=1)
array([3, 2, 3])

Explanation about example-if we specify the axis also it gives the no. of non-zero values along the rows or columns depending upon the value of axis is 0 or 1.

#Example 3
arr3=[[9,8,3],
[12,3,0],
[12,34,56]]
np.count_nonzero(arr3,axis=1,keepdims=True)
array([,
,
])

as you see in the above example it takes another arguement known as keepdims which have boolean values (True or False) or it is optional.If this is set to True, the axis that are counted are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. If it is given value False,the results is same as if we are not giving keepdims arguement.

# Example 4 - breaking (to illustrate when it breaks)
arr3=[[9,8,3],
[12,3,0],
[12,34,56]]
np.count_nonzero(arr3,axis=4,keepdims=9)
--------------------------------------------------------------------------- AxisError Traceback (most recent call last) <ipython-input-20-a5edb90c526d> in <module> 3 [12,3,0], 4 [12,34,56]] ----> 5 np.count_nonzero(arr3,axis=4,keepdims=9) <__array_function__ internals> in count_nonzero(*args, **kwargs) /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/core/numeric.py in count_nonzero(a, axis, keepdims) 460 a_bool = a.astype(np.bool_, copy=False) 461 --> 462 return a_bool.sum(axis=axis, dtype=np.intp, keepdims=keepdims) 463 464 /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/core/_methods.py in _sum(a, axis, dtype, out, keepdims, initial, where) 45 def _sum(a, axis=None, dtype=None, out=None, keepdims=False, 46 initial=_NoValue, where=True): ---> 47 return umr_sum(a, axis, dtype, out, keepdims, initial, where) 48 49 def _prod(a, axis=None, dtype=None, out=None, keepdims=False, AxisError: axis 4 is out of bounds for array of dimension 2

Explanation about example (why it breaks and how to fix it)-the value of axis depends upon the dimensions of arrays and keepdims parameters only takes boolean value.you can also use the original documentationhttps://numpy.org/doc/stable/reference/generated/numpy.around.html?highlight=around#numpy.around

Some closing comments about when to use this function.when you want to calculate no. of non-zero values in an array.we can use it to count no. elements in an array satisfying a given condition by applying count_nonzero method on an array which is an output(which contains 0s and 1s) of a logical or conditional(<,>,==,<=,>=) statements between two arrays.

jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "abhishek-p/numpy-array-operations" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/abhishek-p/numpy-array-operations

Function 4- np.around

With explanations

# Example 1 - working
arr=[[4.333,9.20932909,7.29832],
[67.28,90.2398923,83.2929]]
np.around(arr,decimals=6)
array([[ 4.333   ,  9.209329,  7.29832 ],
[67.28    , 90.239892, 83.2929  ]])

Explanation about example- around takes an array as an arguement and also takes decimals as a arguement which decides the Number of decimal places to round to.it gives the rounded off array.

# Example 2 - working
arr=[[4.333,9.20932909,7.29832],
[67.28,90.2398923,83.2929]]
np.around(arr)
array([[ 4.,  9.,  7.],
[67., 90., 83.]])

Explanation about example- by default it takes decimals=0 to round off.

#example 3
arr=[[4.333,9.20932909,7.29832],
[67.28,90.2398923,83.2929]]
np.around(arr,decimals=2,out=np.array([[0.,0,0],[0,0,0]]))
array([[ 4.33,  9.21,  7.3 ],
[67.28, 90.24, 83.29]])

another arguement out can be used to which have Alternative output array in which to place the result. It must have the same shape as the expected output, but the type of the output values will be cast if necessary.

# Example 4 - breaking (to illustrate when it breaks)
arr=[[4.333,9.20932909,7.29832],
[67.28,90.2398923,83.2929]]
np.around(arr,decimals=2,out=np.array([[0,0],[0,0.5]]))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-25-074052358b0c> in <module> 2 arr=[[4.333,9.20932909,7.29832], 3 [67.28,90.2398923,83.2929]] ----> 4 np.around(arr,decimals=2,out=np.array([[0,0],[0,0.5]])) <__array_function__ internals> in around(*args, **kwargs) /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/core/fromnumeric.py in around(a, decimals, out) 3260 3261 """ -> 3262 return _wrapfunc(a, 'round', decimals=decimals, out=out) 3263 3264 /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds) 53 bound = getattr(obj, method, None) 54 if bound is None: ---> 55 return _wrapit(obj, method, *args, **kwds) 56 57 try: /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/core/fromnumeric.py in _wrapit(obj, method, *args, **kwds) 42 except AttributeError: 43 wrap = None ---> 44 result = getattr(asarray(obj), method)(*args, **kwds) 45 if wrap: 46 if not isinstance(result, mu.ndarray): ValueError: invalid output shape

Explanation about example (why it breaks and how to fix it)-the shape of output array which is to be replaced by the result should be of same size of as input array .

# Example 5 - breaking (to illustrate when it breaks)
arr=[[4.333,9.20932909,7.29832],
[67.28,90.2398923,83.2929]]
np.around(arr,decimals=2,out=np.array([[0,0,7],[0,5,9]]))
--------------------------------------------------------------------------- UFuncTypeError Traceback (most recent call last) <ipython-input-26-09199401385e> in <module> 2 arr=[[4.333,9.20932909,7.29832], 3 [67.28,90.2398923,83.2929]] ----> 4 np.around(arr,decimals=2,out=np.array([[0,0,7],[0,5,9]])) <__array_function__ internals> in around(*args, **kwargs) /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/core/fromnumeric.py in around(a, decimals, out) 3260 3261 """ -> 3262 return _wrapfunc(a, 'round', decimals=decimals, out=out) 3263 3264 /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds) 53 bound = getattr(obj, method, None) 54 if bound is None: ---> 55 return _wrapit(obj, method, *args, **kwds) 56 57 try: /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/core/fromnumeric.py in _wrapit(obj, method, *args, **kwds) 42 except AttributeError: 43 wrap = None ---> 44 result = getattr(asarray(obj), method)(*args, **kwds) 45 if wrap: 46 if not isinstance(result, mu.ndarray): UFuncTypeError: Cannot cast ufunc 'multiply' output from dtype('float64') to dtype('int64') with casting rule 'same_kind'

data type of output array(which is to be replaced by result) should match with dat type of input array.In the above case atleast one value should be float to make data type of array as float.you can also use the original documentationhttps://numpy.org/doc/stable/reference/generated/numpy.around.html?highlight=around#numpy.around

Some closing comments about when to use this function- Use this function to round off the elements of array to a particular decimal place.

jovian.commit()
[jovian] Attempting to save notebook..

Function 5-np.linalg.solve

# Example 1 - working To Solve the system of equations 3 * x0 + x1 = 9 and x0 + 2 * x1 = 8:forming co-efficient matrix as arr1 and another matrix as arr2
arr1=[[3,1]
,[1,2]]
arr2=[9,8]
np.linalg.solve(arr1,arr2)

Explanation about example-working To Solve the system of equations 3 * x0 + x1 = 9 and x0 + 2 * x1 = 8 ,forming co-efficient matrix as arr1 and another matrix as arr2 .the results display the value of x0 and x1 respectively.

# Example 2 - working
arr1=[[3,1,4]
,[1,2,6]
,[1,2,3]]
arr2=[9,8,7]
np.linalg.solve(arr1,arr2)

Explanation about example-Explanation about example-working To Solve the system of equations 3 * x0 + x1+4* x2 = 9 , x0 + 2 * x1+6 x2 = 8 and x0 + 2 x1+3 *x2 = 9,forming co-efficient matrix as arr1 and another matrix as arr2 .the results display the value of x0 , x1 and x2 respectively.

# Example 3 - breaking (to illustrate when it breaks)
arr1=[[3,1,4]
,[1,2,6]
,[1,2,3]]
arr2=[9,8,7]
np.linalg.solve(arr2,arr1)

Explanation about example (why it breaks and how to fix it)-the order of arguement to the method np.linalg.solve should be correct.The coefficient matrix should be placed first.you can also use the original documentationhttps://numpy.org/doc/stable/reference/generated/numpy.linalg.solve.html#numpy.linalg.solve

Some closing comments about when to use this function- many engineering problems needs solution of linear equations of any number of variables.we can use the above method to find all the variables.

jovian.commit()

Function 6 - ???

# Example 1 - working
???

# Example 2 - working
???

# Example 3 - breaking (to illustrate when it breaks)
???

Explanation about example (why it breaks and how to fix it)