 abhishek-p/100-numpy-exercises - Jovian
Learn data science and machine learning by building real-world projects on Jovian

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')
[jovian] Attempting to save notebook.. [jovian] Please enter your API key ( from https://jovian.ml/ ): API KEY: ········ [jovian] Creating a new project "abhishek-p/100-numpy-exercises" [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/abhishek-p/100-numpy-exercises

# Ucomment the next line if you need install numpy
1. Import the numpy package under the name np (★☆☆)
import numpy as np
2. Print the numpy version and the configuration (★☆☆)
np.version.version
'1.19.1'
n=np.zeros(10)
4. How to find the memory size of any array (★☆☆)
n.size * n.itemsize
80
6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)
n=np.zeros(10);
n=1;
n
array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])
7. Create a vector with values ranging from 10 to 49 (★☆☆)
np.arange(10,49)
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48])
8. Reverse a vector (first element becomes last) (★☆☆)
n=np.array([1,2,3,4,5,6,7])
n[::-1]
array([7, 6, 5, 4, 3, 2, 1])
9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)
(np.arange(0,9)).reshape((3,3))
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
n=np.array([1,2,0,0,4,0] )
n.nonzero()
(array([0, 1, 4]),)

import jovian
jovian.commit(project='numpy-100-exercises')
[jovian] Attempting to save notebook.. [jovian] Creating a new project "abhishek-p/numpy-100-exercises" [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/abhishek-p/numpy-100-exercises
11. Create a 3x3 identity matrix (★☆☆)
np.eye(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
12. Create a 3x3x3 array with random values (★☆☆)
np.random.rand(3,3,3)
array([[[0.36840257, 0.31150686, 0.9276335 ],
[0.96526821, 0.79289589, 0.73614142],
[0.44406169, 0.14994376, 0.35280784]],

[[0.66975419, 0.78242702, 0.0214157 ],
[0.84521953, 0.77665423, 0.37977462],
[0.21262113, 0.43577485, 0.51677004]],

[[0.96556914, 0.00694206, 0.14305224],
[0.39907871, 0.7387387 , 0.30795233],
[0.00707716, 0.00466449, 0.05923293]]])
13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
k=np.random.rand(10,10)
k
array([[0.14931387, 0.82058391, 0.459991  , 0.80764018, 0.14690962,
0.77881274, 0.16828098, 0.55087478, 0.78414573, 0.75907851],
[0.69101799, 0.12986329, 0.0176528 , 0.97222764, 0.33081084,
0.14053793, 0.03681302, 0.53233792, 0.63126339, 0.88183518],
[0.42468815, 0.99484619, 0.49635862, 0.65537804, 0.10672969,
0.24547634, 0.37691684, 0.54243933, 0.09585046, 0.48858151],
[0.16555427, 0.84382699, 0.89172473, 0.90645   , 0.34322969,
0.98014639, 0.62552626, 0.07639031, 0.02330048, 0.83833606],
[0.30588022, 0.6526263 , 0.80756445, 0.67713463, 0.51845192,
0.79346221, 0.07624766, 0.18257856, 0.86277786, 0.98897517],
[0.6990044 , 0.89006764, 0.63992975, 0.02744584, 0.42320314,
0.51255777, 0.79736583, 0.06600319, 0.23239045, 0.86973627],
[0.39103921, 0.10003976, 0.34530513, 0.29638479, 0.477705  ,
0.57439124, 0.02015245, 0.75153273, 0.69666493, 0.73714948],
[0.91423693, 0.31753242, 0.18279211, 0.28677912, 0.28272688,
0.72359714, 0.96909524, 0.77239468, 0.04531612, 0.81346817],
[0.94797097, 0.97468478, 0.04996311, 0.40947557, 0.63364666,
0.96870186, 0.46382388, 0.7063356 , 0.38120641, 0.00247733],
[0.23027473, 0.79331469, 0.74775653, 0.61750354, 0.17973603,
0.77911122, 0.39385813, 0.57663947, 0.98574761, 0.84971466]])
print('max',np.max(k),'min',np.min(k))
max 0.994846188879808 min 0.0024773341094630474
14. Create a random vector of size 30 and find the mean value (★☆☆)
k=np.random.rand(30)

np.mean(k)
0.44171407587126876
k.sum()/30
0.44171407587126876

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
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
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-5ca840bfb533> in <module> ----> 1 0 * np.nan 2 np.nan == np.nan 3 np.inf > np.nan 4 np.nan - np.nan 5 np.nan in set([np.nan]) NameError: name 'np' is not defined

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