Exercises 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Exercise set 1 - Python and NumPy basics

Put all your exercises (Jupyter Notebooks or Python-files) in your course Git-project.
Use either code comments or Jupyter Notebook markdown (text) to document which
exercise you are doing and what a certain code section does!
You can return all of these exercises in a single Jupyter Notebook, if you wish.

1. Create these lists without NumPy:


• List of 15 ones
• List of 15 number 7's
• List of numbers from 100 to 150
• List of even numbers between 0 and 100
• List of years that are divisible by 4 between 1950 and 2020

Tip: creating a list with Python code is usually something like this:

numbers = []
for x in range(0, 5):
numbers.append(x)

2. Now, create these arrays with NumPy!


• Array of 15 zeroes
• Array of 15 ones
• Array of 15 number 7's
• Array of numbers from 100 to 150
• Array of numbers from 1900 to 2021
• Array of even numbers between 0 and 100
• Array of years that are divisible by 4 between 1950 and 2020

Note: compare the NumPy code to the plain Python code after doing the
exercises. Easier or more difficult?  (answer in code comments or as text
in Jupyter Notebook)
3. Let's try out NumPy vectors and matrices:
• Create a NumPy vector (list) containing these values:
5, 16, 9, 2, 19, 18, 11, 7
• Use argmax() to find the position index of the largest value in
the vector, and print it

• Create a 7x7 matrix, with values from 1 to 49


• Extra task: Create this matrix (list of lists) without NumPy in
Python!

• Generate 8x8 random number matrix, values between 0 and 5

• Generate 8x8 random number matrix, using standard normal


distribution values (= values are between -3 and 3)

4. Create a linearly spaced…


• array (list/vector) of 10 values between value 0 and 1
• 10x10 matrix between values 0 and 5
• Extra task: create this matrix without NumPy as well!
5. Recreate the matrices in code based on the pictures.
• Create this 3x5 matrix with NumPy:

Note: The values above have no logic, you'll have to feed basic
Python lists to NumPy for this result

• Generate this NumPy array:


• Generate this NumPy array:

Tips: arange-function (and step-parameter) + reshape is


recommended. Alternatively you can use broadcasting and division
if you like.
6. Use this matrix in your code:

dataset = np.arange(1, 37).reshape(6, 6)

• Create a slice of the matrix that contains this part of the matrix:

• Pick out this column slice from data


• Pick out this row from the data

• Pick out these three rows from data:


7. Use this matrix:

dataset = np.arange(1, 50).reshape(7, 7)

• Add 50 to all values in the array (use broadcasting)


• Get the sum of all values in the dataset
• Get sum of only odd numbers in the dataset
• Get the standard deviation of the values in the dataset
• Get the sum of all values by ROW in the dataset
• Get the sum of all values by COLUMN in the dataset (tip, use the axis
–parameter)

• Extra task: Do the total sum, row sum and column sum exercises
without NumPy in Python!

Note: Don't create a new dataset when adding 50 to all values in the array,
use the existing one.
Advanced tasks (varying challenges, some require Googling):

1. Create two files for this exercise: save_matrix.py and


read_matrix.py
o in save_matrix.py: create a 15x15 matrix, and save it to a file
(matrix.txt)
o in read_matrix.py, read matrix.txt, and convert it back to a
NumPy matrix

NumPy has loadtxt() and savetxt()-functions for these


purposes.

2. Masked arrays are a feature of NumPy that allow us to deal with


pieces data that are incorrect or missing in a dataset (for example,
faulty measurements from equipment etc.).

Create a NumPy matrix, that contains numbers between 0 to 50, and


put some faulty numbers in a 10 different spots
(for example: -100)

Use a masked array to handle the -100 values in your matrix.


Replace every -100 with 0.
3. argsort is a feature of NumPy that allows you to sort a NumPy array
based on another NumPy array.

This is useful if you have two separate arrays, but their data should
be in sync even if order of data is changed. For example, one array
contains the prices of cars, and another contains the manufacturing
years. If we wish to sort the data by manufacturing years, and keep
the prices in sync as well, argsort will be handy.

Useful link:
http://arogozhnikov.github.io/2015/09/29/NumpyTipsAndTricks1.ht
ml

Create two random datasets:


• Manufacturing years of 10 cars,
values between 2010 and 2020
• Selling price of 10 cars,
prices between 2000€ and 60000€

Use argsort to sort both datasets according to manufacturing


years.
4. Try out the following additional NumPy features, and discuss in your
code comments or in Jupyter Notebook text where they could be
useful. You can also Google and search for ideas where these could
be used.

You can write your thoughts as comments in the code as you try out
each feature.

o np.repeat()
o np.ravel()
o np.hsplit(), np.vsplit(), np.hstack, np.vstack()
o np.transpose()
o np.round()
o np.expand_dims(), np.squeeze()
o np.digitize()

You might also like