A7: NumPy (Practice Exercises)

Junaid Qazi, PhD
5 min readFeb 1, 2020

This article is a part of “Data Science from Scratch — Can I to I Can” series.

Click here for the previous article/lecture on “A6: NumPy (Part-2): Indexing, slicing, broadcasting, boolean masking & universal function (ufuncs).”

Hello Guys,

So, it’s time to test your knowledge on NumPy. Lets start with simple task and move on to more challenging ones!

☞ Please note, there are several ways to get the required output, so your code could be different. As long as you are getting the tasks done, it is fine at this stage.

Exercises:

(Solutions are provided at the end.)

  1. What is the major difference between “Vector" and "Matrix"?
  2. How to import NumPy library?
  3. Convert the given Python list list_1=[1,2,3,4,5] into NumPy array? Please check its data type.
  4. Generate array [0,1,2,3,4,5] using NumPy built-in function, arange().
  5. Generate an array of "5" zeros.
  6. Generate the following matrix.

7. Generate [1.,1.,1.,1.,1.] using NumPy built-in function?

8. Generate an array of “5” tens (10).

9. Use arange() to generate an array of even numbers between 50 and 100. (50 and 100 are not included)

10. Generate an array of 10 linearly spaced points between 0 and 1. Print the step-size in the output as well?

11. Perform the following tasks:

  • Generate a vector array of 25 numbers using arange()
  • write a code to convert the vector array into 2-D matrix using reshape
  • can we use shape instead of reshape?

12. Please generate the following matrix.

13. Write code to generate the output below, use "linspace()" and "print()".

14. What is the main difference between linspace() and arange()?

15. How to generate a single random number using NumPy built-in function?

16. Write a code to generate a 7x5 matrix of 35 random numbers?

17. Please generate the following matrix using NumPy’s built-in method for identity matrix.

18. Generate the matrix below and save it as "array_2d". Replicate the outputs using "array_2d", recall indexing & slicing!

19. Can you compute the sum of all the numbers in "array_2d", the matrix that you have created in Q:18?

20. Calculate sum of all the rows and columns in "array_2d", the matrix that you have created in Q:18.

21. Calculate the standard deviation of the values in "array_2d", the matrix that you have created in Q:18.

22. Create a boolean mask and filter the numbers that are not divisible by 3 in "array_2d", the matrix that you have created in Q:18.

Solutions:

1. Please explain yourself.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14. Please consult the lectures. "arange()" takes 3rd argument as step size, whereas, "linspace()" take 3rd argument as number of point we want.

15.

16. You may get different numbers in your matrix, its random!

17.

18.

19.

20.

21.

22.

Well done! I hope you guys are comfortable with NumPy now!

See you in the next lecture on A8: Pandas (Part-1): Series & DataFrame — Index & Slicing”.

Note: This complete course, including video lectures and jupyter notebooks, is available on the following links:

About Dr. Junaid Qazi:

Dr. Junaid Qazi is a Subject Matter Specialist, Data Science & Machine Learning Consultant. He is a Professional Development Coach, Mentor, Author, and Invited Speaker. He can be reached for consulting projects and/or professional development training via LinkedIn or through ScienceAcademy.ca.

--

--

Junaid Qazi, PhD

We offer professional development, corporate training, consulting, curriculum and content development in Data Science, Machine Learning and Blockchain.