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

6 months ago

## 100 numpy exercises

##### How to run this notebook

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.

##### How to get help

If you are stuck, you can ask for help on the community forum: https://jovian.ml/forum/t/100-numpy-exercises-hints-discussions-help/10561 . You can get help with errors, ask for hints, and share your solutions with others.

If you're new to Numpy, check out this notebook for a quick tutorial: https://jovian.ml/aakashns/python-numerical-computing-with-numpy

Remember to save a snapshot of your work from time to time using to your Jovian.ml account.

import jovian
jovian.commit(project='100-numpy-exercises')

# Ucomment the next line if you need install numpy

##### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)

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

##### 17. What is the result of the following expression? (★☆☆)
0 * np.nan
np.nan == np.nan
np.inf > np.nan
np.nan - np.nan
np.nan in set([np.nan])
0.3 == 3 * 0.1

##### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?

import jovian
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "aakashns/numpy-100-exercises" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/aakashns/numpy-100-exercises

##### 26. What is the output of the following script? (★☆☆)
# Author: Jake VanderPlas

print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))

Z**Z
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<Z>Z

##### 28. What are the result of the following expressions?
np.array(0) / np.array(0)
np.array(0) // np.array(0)
np.array([np.nan]).astype(int).astype(float)

##### 30. How to find common values between two arrays? (★☆☆)

import jovian
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "aakashns/numpy-100-exercises" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/aakashns/numpy-100-exercises

##### 32. Is the following expressions true? (★☆☆)
np.sqrt(-1) == np.emath.sqrt(-1)

##### 40. Create a random vector of size 10 and sort it (★★☆)

import jovian
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "aakashns/numpy-100-exercises" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/aakashns/numpy-100-exercises

##### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)

import jovian
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "aakashns/numpy-100-exercises" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/aakashns/numpy-100-exercises

1, 2, 3, 4, 5
6,  ,  , 7, 8
,  , 9,10,11

##### 60. How to tell if a given 2D array has null columns? (★★☆)

import jovian
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "aakashns/numpy-100-exercises" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/aakashns/numpy-100-exercises

##### 70. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★)

import jovian
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "aakashns/numpy-100-exercises" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/aakashns/numpy-100-exercises

##### 80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a fill value when necessary) (★★★)

import jovian
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "aakashns/numpy-100-exercises" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/aakashns/numpy-100-exercises

##### 90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)

import jovian
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "aakashns/numpy-100-exercises" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/aakashns/numpy-100-exercises