execute the following line to import numpy and scipy


In [1]:
import numpy as np
import scipy as sp
import scipy.optimize

Create a numpy array containing the numbers from 0 to 20

  1. print the last value
  2. print the last 4 values
  3. print every second value

In [ ]:

Find indices of non-zero elements from [1,2,0,0,4,0]


In [ ]:

return all values greather or equal to 2 from [1,2,0,0,4,0]


In [ ]:

Create a null vector of size 10


In [ ]:

Create a vector containing 50 evenly spaced values between 0 and 2


In [ ]:

Construct a matrix by repeating the following row 5 times: [4, 0.2, 5.6, 1.2] (hint: np.repeat)


In [ ]:

Create a 4x4 identity matrix


In [ ]:

Create a 8x8 chessboard matrix (values 0 and 1)


In [ ]:

create a copy of the following matrix and set the first column to zeros. Check that the original matrix is ualtered


In [ ]:

Create a random vector of size 10 and sort it


In [ ]:

compute the mean of the random array


In [ ]:

find the smallest element of the random arary


In [ ]:

generate the follwing matrix (without explicitly writing it)

[[1, 6, 11],
[2, 7, 12],
[3, 8, 13],
[4, 9, 14],
[5, 10, 15]]


In [ ]:

compute the matrix product of two matrices of your choice


In [ ]:

compute the matrix product of the following matrices


In [ ]:

compute the determinant of the matrix product


In [ ]:

find the smallest element in matrix A


In [ ]:

use the matrix inverse function (np.linalg.inv) to solve the following linear equation system:

  • 8 x + 2 y = 24
  • -4 x + y = -8

In [ ]:

compute the integral of the polynomial x^5 - 13x^3+ 3x^2


In [ ]:

search in the scipy library for a function to compute a zero-crossing (root) of the above polynomial


In [ ]:

Linear regression

We have a dataset which relates the final univerity grade of students to their high-school grades and grades from SAT tests[1]. The dataset is given below and containts the following columns: 1. high school grade point average, 2. Math SAT score, 3. Verbal SAT score, 4. Computer science grade point average, 5. Overall university grade point average.

Compute a linear regression to predict the overall university grade point average from the remaining variables. Hint: the standard linear regression model reads:

$ y = X * \beta $

and the coefficients $\beta$ can be computed using the following formula:

$ \beta = (X^TX)^{-1}X^Ty $

compute the predicted values for the overall university grade ($y$) and the residuals ($y_i - data_i$). Compute the mean residual.

[1] source: http://onlinestatbook.com/2/case_studies/sat.html


In [6]:
import pickle
import pylab
data = pickle.load(open('data.p', 'rb'))

In [ ]: