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

# abhyu420/numpy-array-operations

3 months ago

#### 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] Detected Colab notebook... [jovian] Please enter your API key ( from https://jovian.ai/ ): API KEY: ·········· [jovian] Uploading colab notebook to Jovian... Committed successfully! https://jovian.ai/abhyu420/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.stack() function2 = np.dot() function3 = np.rint() function4 = np.all() function5 = np.logical_and()

### Function 1 - np.concatenate (change this)

# Example 1 - working (change this)
arr1 = [2,34,5]
arr2 = [5,7,9]

np.stack((arr1,arr2))
array([[ 2, 34,  5],
[ 5,  7,  9]])

# Example 2 - working
arr1 = [[2,3,5],[4,6,8]]
arr2 = [[4,5,6],[5,8,0]]

np.stack((arr1,arr2))
array([[[2, 3, 5],
[4, 6, 8]],

[[4, 5, 6],
[5, 8, 0]]])

# Example 3 - breaking (to illustrate when it breaks)
arr1 = [[1, 2],
[3, 4.]]

arr2 = [[5, 6, 7],
[8, 9, 10]]

np.stack((arr1, arr2), axis=1)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-10-1d5459faacf8> in <module>() 6 [8, 9, 10]] 7 ----> 8 np.stack((arr1, arr2), axis=1) <__array_function__ internals> in stack(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/numpy/core/shape_base.py in stack(arrays, axis, out) 425 shapes = {arr.shape for arr in arrays} 426 if len(shapes) != 1: --> 427 raise ValueError('all input arrays must have the same shape') 428 429 result_ndim = arrays[0].ndim + 1 ValueError: all input arrays must have the same shape

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

jovian.commit()
[jovian] Detected Colab notebook... [jovian] Uploading colab notebook to Jovian... Committed successfully! https://jovian.ai/abhyu420/numpy-array-operations

### Function 2 - ???

# Example 1 - working
arr1 = [2,4,5]
arr2 = [3,5,7]

np.dot(arr1,arr2)
61

# Example 2 - working
arr1 = [4,6,8]
arr2 = [9,8,7]

np.dot(arr2,arr1)
140

# Example 3 - breaking (to illustrate when it breaks)
arr1 = [9,6,4]
arr2 = [7.9,8]

np.dot(arr1,arr2)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-14-b25f116cdd7a> in <module>() 3 arr2 = [7.9,8] 4 ----> 5 np.dot(arr1,arr2) <__array_function__ internals> in dot(*args, **kwargs) ValueError: shapes (3,) and (2,) not aligned: 3 (dim 0) != 2 (dim 0)

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

jovian.commit()
[jovian] Detected Colab notebook... [jovian] Uploading colab notebook to Jovian... Committed successfully! https://jovian.ai/abhyu420/numpy-array-operations

### Function 3 - ???

# Example 1 - working
arr1 = [[1.785, 2.35],
[3.62, 4.54]]
print("rounded numbers are ", np.rint(arr1))
rounded numbers are [[2. 2.] [4. 5.]]

# Example 2 - working
arr1 = [[1.785, 2.5],
[3.6256, 4.64]]
print("rounded numbers are ", np.rint(arr1))
rounded numbers are [[2. 2.] [4. 5.]]

# Example 3 - breaking (to illustrate when it breaks)
arr2 = [[1, "abc"],
[3, "ch"]]
print("rounded numbers", np.rint(arr2))

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

jovian.commit()

italicized text## Function 4 - ???

# Example 1 - working
arr1 =([[-1,2,3],[5,0,2]])
print(np.all(arr1))

# Example 2 - working
print(np.all(arr1,axis=0))

# Example 3 - breaking (to illustrate when it breaks)
arr2=[1,"nan",2]
print(np.all(arr2))

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

jovian.commit()

### Function 5 - ???

# Example 1 - working
arr1=[False,0,1,True]
arr2=[False,5,6,True]
output=np.logical_and(arr1,arr2)
print(output)

# Example 2 - working
arr3=[False,0,5,True]
arr4=[False,7,9,True]
print(np.logical_and(arr1,arr2))

# Example 3 - breaking (to illustrate when it breaks)
arr1=[False,0,1,True]
arr2=[False,5,6,True,45]
print(np.logical_and(arr1,arr2))

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

jovian.commit()