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

Sign up to execute **numpy-array-operations** and 150,000+ data science projects. Build your own projects and share them online!

Updated a month ago

Assignment 2 - Numpy Array OperationsThis 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:

- Pick 5 interesting Numpy array functions by going through the documentation: https://numpy.org/doc/stable/reference/routines.html
- 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.
- 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- (Optional) Share your notebook online (on Twitter, LinkedIn, Facebook) and on the community forum thread: https://jovian.ml/forum/t/assignment-2-numpy-array-operations-share-your-work/10575 .
- (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.

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
```

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.]]
```

Add some explanation about the function in your own words

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
```

Explanation about example

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
```

Explanation about example

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)

Some closing comments about when to use this function.

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
```

Add some explanations

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
```

Explanation about example

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
```

Explanation about example

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)

Some closing comments about when to use this function.

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
```

Add some explanations

In [ ]:

```
# Example 1 - working
```

Explanation about example

In [ ]:

```
# Example 2 - working
???
```

Explanation about example

In [ ]:

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

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

Some closing comments about when to use this function.

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
```

Add some explanations

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
```

Explanation about example

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
```

Explanation about example

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)

Some closing comments about when to use this function.

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
```

Add some explanations

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
```

Explanation about example

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
```

Explanation about example

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)

Some closing comments about when to use this function.

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
```

Add some explanations

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
```

Explanation about example

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
```

Explanation about example

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)

Some closing comments about when to use this function.

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
```

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

Provide links to your references and other interesting articles about Numpy arrays:

- Numpy official tutorial : https://numpy.org/doc/stable/user/quickstart.html
- ...

In [ ]:

`jovian.commit()`

In [ ]:

` `