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

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

Updated 6 months 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.

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

`!pip install jovian --upgrade -q`

`import jovian`

`jovian.commit(project='numpy-array-operations')`

```
[jovian] Creating a new project "aditi/numpy-array-operations"
[jovian] Committed successfully! https://jovian.ai/aditi/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.nonzero # (change this)
function2 = np.argmax
function3 = np.partition
function4 = np.sin
function5 = np.inner
```

Add some explanation about the function in your own words

```
# Example 1 - working (change this)
x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
np.nonzero(x)
```

`(array([0, 1, 2, 2]), array([0, 1, 0, 1]))`

Return the indices of the elements that are non-zero in tuple form.

```
# Example 2 - working
x[np.nonzero(x)]
np.transpose(np.nonzero(x))
```

```
array([[0, 0],
[1, 1],
[2, 0],
[2, 1]])
```

While the nonzero values can be obtained with a[nonzero(a)], it is recommended to use x[x.astype(bool)] or x[x != 0] instead, which will correctly handle 0-d arrays.

```
# Example 3 - breaking (to illustrate when it breaks)
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
np.nonzero(a > 3)
```

`(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))`

A common use for nonzero is to find the indices of an array, where a condition is True. Given an array a, the condition a > 3 is a boolean array and since False is interpreted as 0, np.nonzero(a > 3) yields the indices of the a where the condition is true.

Some closing comments about when to use this function.

`jovian.commit()`

```
[jovian] Updating notebook "aditi/numpy-array-operations" on https://jovian.ai
[jovian] Committed successfully! https://jovian.ai/aditi/numpy-array-operations
```

Returns the indices of the maximum values along an axis.

```
# Example 1 - working
a = np.arange(6).reshape(2,3) + 10
print(a)
```

```
[[10 11 12]
[13 14 15]]
```

`np.argmax(a)`

`5`

In case of multiple occurrences of the maximum values, the indices corresponding to the first occurrence are returned.

` `

` `

```
# Example 2 - working
np.argmax(a, axis=0)
```

`array([1, 1, 1])`

return indices of axis 0

```
# Example 3 - breaking (to illustrate when it breaks)
np.argmax(a, axis=1)
```

`array([2, 2])`

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

Some closing comments about when to use this function.

`jovian.commit()`

```
[jovian] Updating notebook "aditi/numpy-array-operations" on https://jovian.ai
[jovian] Committed successfully! https://jovian.ai/aditi/numpy-array-operations
```

Add some explanations

```
# Example 1 - working
???
```

Explanation about example

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

Explanation about example

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

`jovian.commit()`

Return a partitioned copy of an array

numpy.partition(a, kth, axis=- 1, kind='introselect', order=None)

```
# Example 1 - working
a = np.array([3, 4, 2, 1])
np.partition(a, 3)
```

`array([2, 1, 3, 4])`

Creates a copy of the array with its elements rearranged in such a way that the value of the element in k-th position is in the position it would be in a sorted array. All elements smaller than the k-th element are moved before this element and all equal or greater are moved behind it. The ordering of the elements in the two partitions is undefined.

```
# Example 2 - working
a = np.array([3, 4, 2, 1])
np.partition(a, (0, 1))
```

`array([1, 2, 4, 3])`

Explanation about example

```
# Example 3 - breaking (to illustrate when it breaks)
a = np.array([3, 4, 2, 1])
np.partition(a, 2)
```

`array([1, 2, 3, 4])`

ValueError: kth(=4) out of bounds (4), when I gave kth value as 4 because it has only indices upto 3 so I should gave kth value till 3.

Some closing comments about when to use this function.

`jovian.commit()`

```
[jovian] Updating notebook "aditi/numpy-array-operations" on https://jovian.ai
[jovian] Committed successfully! https://jovian.ai/aditi/numpy-array-operations
```

Inner product of two arrays.

```
# Example 1 - working
a = np.array([1,2,3])
b = np.array([0,1,0])
np.inner(a, b)
```

`2`

Explanation about example

```
# Example 2 - working
a = np.arange(24).reshape((2,3,4))
b = np.arange(4)
np.inner(a, b)
```

```
array([[ 14, 38, 62],
[ 86, 110, 134]])
```

multidimensional array inner product

```
# Example 3 - breaking (to illustrate when it breaks)
np.inner(np.eye(2), 7)
```

```
array([[7., 0.],
[0., 7.]])
```

example where b is a scalar/

Some closing comments about when to use this function.

`jovian.commit()`

```
[jovian] Updating notebook "aditi/numpy-array-operations" on https://jovian.ai
[jovian] Committed successfully! https://jovian.ai/aditi/numpy-array-operations
```

Trigonometric sine, element-wise. numpy.sin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'sin'>

```
# Example 1 - working
np.sin(np.pi/2.)
```

`1.0`

Print sine of one angle

```
# Example 2 - working
np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180. )
```

`array([0. , 0.5 , 0.70710678, 0.8660254 , 1. ])`

Print sines of an array of angles given in degrees.

```
# Example 3 - breaking (to illustrate when it breaks)
import matplotlib.pylab as plt
x = np.linspace(-np.pi, np.pi, 201)
plt.plot(x, np.sin(x))
plt.xlabel('Angle [rad]')
plt.ylabel('sin(x)')
plt.axis('tight')
plt.show()
```

example 3 got break beacuse i was not importing matplotlib

Some closing comments about when to use this function.

`jovian.commit()`

```
[jovian] Updating notebook "aditi/numpy-array-operations" on https://jovian.ai
[jovian] Committed successfully! https://jovian.ai/aditi/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
- ...

`jovian.commit()`

```
[jovian] Updating notebook "aditi/numpy-array-operations" on https://jovian.ai
[jovian] Committed successfully! https://jovian.ai/aditi/numpy-array-operations
```

`jovian.submit(assignment="zero-to-pandas-a2")`

```
[jovian] Updating notebook "aditi/numpy-array-operations" on https://jovian.ai
[jovian] Committed successfully! https://jovian.ai/aditi/numpy-array-operations
[jovian] Submitting assignment..
[jovian] Verify your submission at https://jovian.ai/learn/data-analysis-with-python-zero-to-pandas/assignment/assignment-2-numpy-array-operations
```

`jovian.submit(assignment="zero-to-pandas-a2")`

` `