Learn data science and machine learning by building real-world projects on Jovian

abhishek-s/numpy-array-operations

a month 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.

nansum nanmin nanmmax nanargmin nanargmax

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.

In [1]:
``!pip install jovian --upgrade -q``
In [2]:
``import jovian``
In [3]:
``jovian.commit(project='numpy-array-operations')``
```[jovian] Attempting to save notebook.. [jovian] Updating notebook "abhishek-s/numpy-array-operations" on https://jovian.ai [jovian] Uploading notebook.. [jovian] Committed successfully! https://jovian.ai/abhishek-s/numpy-array-operations ```
Out[3]:
``'https://jovian.ai/abhishek-s/numpy-array-operations'``

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

In [4]:
``import numpy as np``
In [5]:
``````arr1d = np.array([np.nan,2,3,4,5,np.nan], dtype=np.float) # Create 1d array

print(f'Data type of first item ({arr1d[0]}) is {arr1d[0].dtype}\n')

arr2d = np.array([1,1,2,3,np.nan,5,6,np.nan,8], dtype=np.float).reshape(3,3) # Create 2d array

print(f'arr1d = {arr1d}\n\narr2d = {arr2d}')``````
```Data type of first item (nan) is float64 arr1d = [nan 2. 3. 4. 5. nan] arr2d = [[ 1. 1. 2.] [ 3. nan 5.] [ 6. nan 8.]] ```

Function 1 - np.nansum

In [6]:
``````# Example 1
npsum1d = np.sum(arr1d)
npnansum1d = np.nansum(arr1d)

print(f'Result of np.sum(arr1d): {npsum1d}\n\
Result of np.nansum(arr1d): {npnansum1d}')``````
```Result of np.sum(arr1d): nan Result of np.nansum(arr1d): 14.0 ```

In [7]:
``````# Example 2 - working
# Example 3
npsum2d = np.sum(arr2d)
npnansum2d = np.nansum(arr2d) # Returns sum of the flattened array, axis = None, , excluding NaNs

print(f'Result of np.sum(arr2d) flattened: {npsum2d}\n\
Result of np.nansum(arr2d) flattened: {npnansum2d}')``````
```Result of np.sum(arr2d) flattened: nan Result of np.nansum(arr2d) flattened: 26.0 ```

In [8]:
``````# Example 3
npsum2d = np.sum(arr2d, axis=0) # Returns sum of array elements by rows, , excluding NaNs
npnansum2d = np.nansum(arr2d, axis=0)

print(f'Result of np.sum(arr2d) by rows: {npsum2d}\n\
Result of np.nansum(arr2d) by rows: {npnansum2d}')``````
```Result of np.sum(arr2d) by rows: [10. nan 15.] Result of np.nansum(arr2d) by rows: [10. 1. 15.] ```

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

In [9]:
``jovian.commit()``
```[jovian] Attempting to save notebook.. [jovian] Updating notebook "abhishek-s/numpy-array-operations" on https://jovian.ai [jovian] Uploading notebook.. [jovian] Committed successfully! https://jovian.ai/abhishek-s/numpy-array-operations ```
Out[9]:
``'https://jovian.ai/abhishek-s/numpy-array-operations'``

Function 2 - nanmin

In [10]:
``````# Example 1 - working
# Example 1
npmin1d = np.min(arr1d)
npnanmin1d = np.nanmin(arr1d)

print(f'Result of np.min(arr1d): {npmin1d}\n\
Result of np.nanmin(arr1d): {npnanmin1d}')``````
```Result of np.min(arr1d): nan Result of np.nanmin(arr1d): 2.0 ```

In [11]:
``````# Example 2 - working
# Example 2
npmin2d = np.min(arr2d)
npnanmin2d = np.nanmin(arr2d)

print(f'Result of np.min(arr2d) flattened: {npmin2d}\n\
Result of np.nanmin(arr2d) flattened: {npnanmin2d}')``````
```Result of np.min(arr2d) flattened: nan Result of np.nanmin(arr2d) flattened: 1.0 ```

In [12]:
``````# Example 3 - breaking (to illustrate when it breaks)
# Example 3
npmin2d = np.min(arr2d, axis=1) # Returns min of array elements by columns, , excluding NaNs
npnanmin2d = np.nanmin(arr2d, axis = 1)

print(f'Result of np.min(arr2d) by columns: {npmin2d}\n\
Result of np.nanmin(arr2d) by columns: {npnanmin2d}')``````
```Result of np.min(arr2d) by columns: [ 1. nan nan] Result of np.nanmin(arr2d) by columns: [1. 3. 6.] ```

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

In [13]:
``jovian.commit()``
```[jovian] Attempting to save notebook.. [jovian] Updating notebook "abhishek-s/numpy-array-operations" on https://jovian.ai [jovian] Uploading notebook.. [jovian] Committed successfully! https://jovian.ai/abhishek-s/numpy-array-operations ```
Out[13]:
``'https://jovian.ai/abhishek-s/numpy-array-operations'``

Function 3 - nanmax

In [ ]:
``````# Example 1 - working

``````

In [ ]:
``````# Example 2 - working
???
``````

In [ ]:
``````# Example 3 - breaking (to illustrate when it breaks)
???
``````

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

In [25]:
``jovian.commit()``
```[jovian] Attempting to save notebook.. [jovian] Updating notebook "aakashns/numpy-array-operations" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/aakashns/numpy-array-operations ```
Out[25]:
``'https://jovian.ml/aakashns/numpy-array-operations'``

Function 3 - nanmax

In [14]:
``````# Example 1 - working
# Example 1
npmax1d = np.max(arr1d)
npnanmax1d = np.nanmax(arr1d)

print(f'Result of np.max(arr1d): {npmax1d}\n\
Result of np.nanmax(arr1d): {npnanmax1d}')``````
```Result of np.max(arr1d): nan Result of np.nanmax(arr1d): 5.0 ```

In [15]:
``````# Example 2 - working
# Example 2
npmax2d = np.max(arr2d)
npnanmax2d = np.nanmax(arr2d)

print(f'Result of np.max(arr2d) flattened: {npmax2d}\n\
Result of np.nanmax(arr2d) flattened: {npnanmax2d}')``````
```Result of np.max(arr2d) flattened: nan Result of np.nanmax(arr2d) flattened: 8.0 ```

In [16]:
``````# Example 3 - breaking (to illustrate when it breaks)
# Example 2
npmax2d = np.max(arr2d, axis=1)
npnanmax2d = np.nanmax(arr2d, axis=1)

print(f'Result of np.max(arr2d) by columns: {npmax2d}\n\
Result of np.nanmax(arr2d) by columns: {npnanmax2d}')``````
```Result of np.max(arr2d) by columns: [ 2. nan nan] Result of np.nanmax(arr2d) by columns: [2. 5. 8.] ```

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

In [17]:
``jovian.commit()``
```[jovian] Attempting to save notebook.. [jovian] Updating notebook "abhishek-s/numpy-array-operations" on https://jovian.ai [jovian] Uploading notebook.. [jovian] Committed successfully! https://jovian.ai/abhishek-s/numpy-array-operations ```
Out[17]:
``'https://jovian.ai/abhishek-s/numpy-array-operations'``

Function 4 - nanargmin

In [18]:
``````# Example 1 - working
# Example 1
npargmin1d = np.argmin(arr1d)
npargnanmin1d = np.nanargmin(arr1d)

print(f'Result of np.argmin(arr1d): {npargmin1d}\n\
Result of np.nanargmin(arr1d): {npargnanmin1d}')``````
```Result of np.argmin(arr1d): 0 Result of np.nanargmin(arr1d): 1 ```

In [19]:
``````# Example 2 - working
# Example 2
npargmin2d = np.argmin(arr2d)
npargnanmin2d = np.nanargmin(arr2d)

print(f'Result of np.argmin(arr2d) flattened: {npargmin2d}\n\
Result of np.nanargmin(arr2d) flattened: {npargnanmin2d}')``````
```Result of np.argmin(arr2d) flattened: 4 Result of np.nanargmin(arr2d) flattened: 0 ```

In [20]:
``````# Example 3 - breaking (to illustrate when it breaks)
# Example 2
npargmin2d = np.argmin(arr2d, axis=0)
npargnanmin2d = np.nanargmin(arr2d, axis=0)

print(f'Result of np.argmin(arr2d) by rows: {npargmin2d}\n\
Result of np.nanargmin(arr2d) by rows: {npargnanmin2d}')``````
```Result of np.argmin(arr2d) by rows: [0 1 0] Result of np.nanargmin(arr2d) by rows: [0 0 0] ```

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

In [21]:
``jovian.commit()``
```[jovian] Attempting to save notebook.. [jovian] Updating notebook "abhishek-s/numpy-array-operations" on https://jovian.ai [jovian] Uploading notebook.. [jovian] Committed successfully! https://jovian.ai/abhishek-s/numpy-array-operations ```
Out[21]:
``'https://jovian.ai/abhishek-s/numpy-array-operations'``

Function 5 - nanargmax

In [22]:
``````# Example 1 - working# Example 1
npargmax1d = np.argmax(arr1d)
npargnanmax1d = np.nanargmax(arr1d)

print(f'Result of np.argmax(arr1d): {npargmax1d}\n\
Result of np.nanargmax(arr1d): {npargnanmax1d}')``````
```Result of np.argmax(arr1d): 0 Result of np.nanargmax(arr1d): 4 ```

In [23]:
``````# Example 2 - working
# Example 2
npargmax2d = np.argmax(arr2d)
npargnanmax2d = np.nanargmax(arr2d)

print(f'Result of np.argmax(arr2d) flattend: {npargmax2d}\n\
Result of np.nanargmax(arr2d) flattened: {npargnanmax2d}')``````
```Result of np.argmax(arr2d) flattend: 4 Result of np.nanargmax(arr2d) flattened: 8 ```

In [24]:
``````# Example 3 - breaking (to illustrate when it breaks)
# Example 3
npargmax2d = np.argmax(arr2d, axis=1)
npargnanmax2d = np.nanargmax(arr2d, axis=1)

print(f'Result of np.argmax(arr2d) by columns: {npargmax2d}\n\
Result of np.nanargmax(arr2d) by columns: {npargnanmax2d}')

``````
```Result of np.argmax(arr2d) by columns: [2 1 1] Result of np.nanargmax(arr2d) by columns: [2 2 2] ```

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

In [25]:
``jovian.commit()``
```[jovian] Attempting to save notebook.. [jovian] Updating notebook "abhishek-s/numpy-array-operations" on https://jovian.ai [jovian] Uploading notebook.. [jovian] Committed successfully! https://jovian.ai/abhishek-s/numpy-array-operations ```
Out[25]:
``'https://jovian.ai/abhishek-s/numpy-array-operations'``

Conclusion

Summarize what was covered in this notebook, and where to go next

``jovian.commit()``
`` ``