Tsallaka zuwa abun ciki
Justice Tech School

NumPy Exercises

Fill in the blanks to check your knowledge. Each exercise belongs to a lesson.

NumPy Introduction

Exercise

Import NumPy with its usual name.

import numpy as 

NumPy Get Started

Exercise

Install command.

pip  numpy

Why NumPy is Fast

Exercise

Sum an array.

arr.()

Creating Arrays

Exercise

Create five evenly spaced numbers from 0 to 1.

np.(0, 1, 5)

Array Attributes

Exercise

Get the shape.

a.

Data Types

Exercise

Convert the type.

a.(int)

Random Numbers

Exercise

Create a generator.

np.random.(42)

Copies and Views

Exercise

Make an independent array.

a.()

Reshape

Exercise

Let NumPy pick a dimension.

a.reshape(2, )

Flatten and Transpose

Exercise

Swap rows and columns.

a.

Joining Arrays

Exercise

Stack arrays as rows.

np.([a, b])

Splitting Arrays

Exercise

Split into three parts.

np.split(a, )

Sorting and Searching

Exercise

Indexes that sort.

np.(a)

Unique Values and Counts

Exercise

Count occurrences.

np.unique(a, return_=True)

dtype and Type Conversion

Exercise

Convert type.

a.(float)

Array Attributes

Exercise

Dimensions count.

a.

zeros, ones, full and empty

Exercise

All zeros.

np.(5)

arange and linspace

Exercise

Evenly spaced count.

np.(0, 1, 5)

Random Numbers

Exercise

Seeded generator.

np.random.default_rng()

Arrays from Lists

Exercise

Nested lists.

np.([[1, 2], [3, 4]])

Copies and Views

Exercise

Independent data.

a.()

concatenate, stack and split

Exercise

Join side by side.

np.([a, b])

sort, argsort and unique

Exercise

Order of indexes.

np.(a)

Lists versus Arrays

Exercise

Double every item.

a  2

argmax along axes

Exercise

Per row.

a.argmax(axis=)

Counting with Masks

Exercise

Count non-zero.

np.(a)

Iterating Arrays

Exercise

Index and value.

np.(a)

Array Indexing

Exercise

Row 0, column 2.

a[0, ]

Slicing

Exercise

Every second element.

a[::]

Boolean Indexing

Exercise

Select values above 6.

a[a  6]

Fancy Indexing

Exercise

Pick positions 0 and 2.

a[[0, ]]

where, select and clip

Exercise

Limit to 0 and 10.

np.(a, 0, 10)

Changing Values

Exercise

Set masked values.

a[a > 3]  -1

Indexing and Slicing

Exercise

Last item.

a[]

Boolean Indexing

Exercise

Mask.

a[a  6]

Fancy Indexing

Exercise

List of positions.

a[[0, ]]

where

Exercise

Choose by condition.

np.(a > 0, 1, 0)

Assigning Values

Exercise

Set a slice.

a[1:3]  5

take, put and compress

Exercise

Index of max.

np.(a)

Slicing Rows and Columns

Exercise

All rows second column.

a[, 1]

Diagonals and Triangles

Exercise

Main diagonal.

np.(a)

Structured Arrays

Exercise

Named fields.

dtype=[("a", "")]

Masked Arrays

Exercise

Hide invalid values.

np.ma.(data, mask)

Saving and Loading

Exercise

Load a file.

np.("a.npy")

Print Options

Exercise

Decimals.

np.set_printoptions(=2)

Vectorising Code

Exercise

Avoid loops.

Use  operations

Set Operations

Exercise

Common items.

np.(a, b)

meshgrid

Exercise

Coordinate grids.

np.(x, y)

Practice: Normalise Data

Exercise

Smallest value.

a.()

Practice: Moving Average

Exercise

Window average.

np.(a, k, mode="valid")

Practice: Grade Matrix

Exercise

Row averages.

s.mean(axis=)

Array Arithmetic

Exercise

Square each element.

a  2

Universal Functions

Exercise

Take square roots.

np.(a)

Aggregations

Exercise

Sum each column.

m.sum(axis=)

Statistics

Exercise

Middle value.

np.(a)

Cumulative Operations

Exercise

Running total.

np.(a)

Rounding and Precision

Exercise

Compare floats safely.

np.(a, b)

Missing Values: NaN

Exercise

Find NaN values.

np.(a)

Element-wise Math

Exercise

Square root.

np.([4, 9])

sum, mean, min, max and axes

Exercise

Column sums.

a.sum(axis=)

Statistics

Exercise

Middle value.

np.(a)

Cumulative Operations

Exercise

Running total.

np.(a)

Rounding

Exercise

Round down.

np.(a)

Handling NaN

Exercise

Ignore NaN.

np.(a)

Comparing Arrays

Exercise

Near equality.

np.(a, b)

Sorting Along an Axis

Exercise

Along rows.

np.sort(a, axis=)

Histograms and Bins

Exercise

Count in bins.

np.(a, bins=3)

Trigonometry and Constants

Exercise

Pi constant.

np.

Logs and Exponentials

Exercise

Base 10 log.

np.(a)

clip, minimum and maximum

Exercise

Limit the range.

np.(a, 0, 10)

Weighted Averages

Exercise

Weighted mean.

np.(a, weights=w)

Random Distributions

Exercise

Normal draws.

rng.(0, 1, 5)

Shuffle and Permutation

Exercise

Shuffled copy.

rng.(a)

Broadcasting

Exercise

Add a scalar to all.

m  10

Adding Axes

Exercise

Remove length 1 axes.

np.(x)

Grids with meshgrid

Exercise

Build coordinate grids.

np.(x, y)

Repeat and Tile

Exercise

Repeat the whole array.

np.(a, 3)

Working With Axes

Exercise

Keep the dimension.

m.sum(axis=0, =True)

reshape and -1

Exercise

Infer a size.

a.reshape(2, )

flatten, ravel and T

Exercise

Transpose.

a.

Adding Axes

Exercise

Add a dimension.

a[:, np.]

Broadcasting Rules

Exercise

Add a row.

a + np.array([1, 2, ])

squeeze and swapaxes

Exercise

Remove size 1 axes.

a.()

tile and repeat

Exercise

Repeat the block.

np.(a, 3)

Dot Product and Matrix Multiplication

Exercise

Matrix product operator.

a  b

Determinant and Inverse

Exercise

Find the determinant.

np.linalg.(a)

Solving Linear Equations

Exercise

Solve for x.

np.linalg.(A, b)

Eigenvalues and Eigenvectors

Exercise

Compute eigenvalues.

np.linalg.(a)

Norms and Distances

Exercise

Length of a vector.

np.linalg.(v)

Special Matrices

Exercise

Identity matrix.

np.(3)

Singular Value Decomposition

Exercise

Compute the decomposition.

np.linalg.(a)

Dot Product and Matrix Product

Exercise

Matrix multiply.

a  b

Inverse and Determinant

Exercise

Inverse.

np.linalg.(a)

Solving Linear Equations

Exercise

Solve A x = b.

np.linalg.(A, b)

Eigenvalues

Exercise

Eigen function.

np.linalg.(a)

Norms and Distance

Exercise

Vector length.

np.linalg.(a)

Outer and Cross Products

Exercise

Product table.

np.([1, 2], [3, 4])

Matrix Power and Rank

Exercise

Repeat a product.

np.linalg.(a, 3)

einsum Introduction

Exercise

Matrix product by notation.

np.("ij,jk->ik", a, b)

Least Squares Fit

Exercise

Fit a line.

np.(x, y, 1)

Structured Arrays

Premium exercise. See plans

Masked Arrays

Premium exercise. See plans

einsum

Premium exercise. See plans

Strides and Memory Layout

Premium exercise. See plans

vectorize and apply_along_axis

Premium exercise. See plans

Saving and Loading

Premium exercise. See plans

Polynomials and Fitting

Premium exercise. See plans

Interpolation and Gradients

Premium exercise. See plans

Fourier Transform

Premium exercise. See plans

Set Operations

Premium exercise. See plans

Histograms and Binning

Premium exercise. See plans

Performance Tips

Premium exercise. See plans

Saving Memory with dtypes

Premium exercise. See plans

Images as Arrays

Premium exercise. See plans

Monte Carlo Estimation

Premium exercise. See plans

Normalising Data

Premium exercise. See plans

Linear Regression with NumPy

Premium exercise. See plans

PCA from Scratch

Premium exercise. See plans

Game of Life

Premium exercise. See plans

NumPy with pandas and Plotting

Premium exercise. See plans

Testing Numerical Code

Premium exercise. See plans

NumPy Best Practices

Premium exercise. See plans

Next Steps

Premium exercise. See plans