[SOLVED] DataScience Assignment 3

30.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You will receive the following solution file(s) instantly after successful payment:

zip file icon ufo_matrixfun-mww5qn.zip (157.5 KB)
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
🔒 Securely Powered by:
Secure Checkout
5/5 - (1 vote)

The following tasks should be solved, individually or in groups of 2. Write the answers to the questions in a readme. When reviewing, check that you got the same answers.

Matrix fun

Solve the following using Python with numpy.
In numpy, there are some handy ways of working with matrixes (they are

all explained later in this document):

  1. 1  import numpy as np
  2. 2  from numpy.linalg import inv

3

  1. 4  # Creating matrices
  2. 5  A = np.array([[ 1, 2 ],[ 3, 4]])
  3. 6  B = np.array([[ 9, 8 ],[ 7, 6]])

7

  1. 8  # Transposing:
  2. 9  A.T # A transposed (danish: A transponeret)
  3. 10  B.T # B transposed

11

  1. 12  # Matrix multiplication:
  2. 13  A@B

14

  1. 15  # Inverse:
  2. 16  inv(A)

    Figure 1: Basic matrix functionality with numpy.
    In the following, when talking about multiplication, we implicitly mean ma-

    trix multiplication (same as dot product).

    Task 1

    Given the two following matrices
    􏰑3 1􏰒 􏰑−1 4􏰒

    A=26B=38 1

  1. (a)  Find AT
  2. (b)  Find BT
  3. (c)  Find AB (matrix multiplication). Compare with simple multiplication (using * instead of @ in Python). Can you see what is the difference?
  4. (d)  Find ABT
  5. (e)  Compare ABT and BT AT
  6. (f)  Find (AT )T
  7. (g)  Find AAT

Task 2

Given

(a) Find AB (b) Find BA

􏰑2 1􏰒
A=32 B=34

Confirm that they are different! Clearly, when doing matrix multiplication, order matters! AB ̸= BA, so matrix multiplication is not commutative.

Task 3

􏰑a b􏰒 TheinverseofamatrixA= c d isfoundby

−1 1􏰑d−b􏰒
A =ad−bc −c a (1)

As seen in listing 1, the inverse of a matrix can be found easily with numpy (after having imported numpy.linalg.inv) by: inv(A).

Using the same matrices from Task 2:
(a) Find A−1
(b) Find B−1
(c) Find AA−1. Look closely at the result. (d) Find A−1A. Look closely at the result. (e) Find BB−1. Look closely at the result.

(f) Find B−1B. Do you start to see a pattern?
It appears that a matrix multiplied with its inverse always gives 0 1 .

Incidentally, a matrix with only ones in the diagonal is called an identity matrix, often denoted I.

2

􏰑1 2􏰒

􏰑1 0􏰒

Task 4

Given

(a) Find A−1

􏰑2 4􏰒 A=12

Oops. We see that not all matrices have an inverse! Looking at equation 1 (the equation for finding the inverse), can you figure out why? (hint: look at the denominator!)

Task 5

Plotting (lines, graphs, coordinates, etc) can be done using matplotlib. Try the following:

  1. 1  import numpy as np
  2. 2  from matplotlib import pyplot as plt
  3. 3  from numpy.linalg import inv

4

  1. 5  xs = np.array([0,0,3,3,0,1.5,3]) # List of x coordinates
  2. 6  ys = np.array([1,0,0,1,1,2 ,1]) # List of y coordinates

7

  1. 8  fig = plt.figure()
  2. 9  fig, ax = plt.subplots()

10

  1. 11  xs_ys = np.array([xs,ys])
  2. 12  ax.axis(’equal’)

13

  1. 14  # Plot the points
  2. 15  ax.plot( *xs_ys, ’-’, color=’b’)

16

  1. 17  # Create a rotation matrix
  2. 18  rot = np.array([[1, 0],[0, 1]]) # <– CHANGE THIS

19

  1. 20  # turn the two lists (xs, ys) into a list of (x,y) tuples
  2. 21  points = np.array([[x,y] for x,y in zip(xs,ys)])

22

  1. 23  # Make the transformation:
  2. 24  points_rot = (points @ rot)

25

  1. 26  #Turnitintoarowofxsandarowofys:
  2. 27  xs_ys_rot = np.array([points_rot[:,0], points_rot[:,1]])

28

  1. 29  # Finally, plot it
  2. 30  ax.plot( *xs_ys_rot, ’-’, color=’r’)
  3. 31  fig

    (a) Settherotationmatrix(line18)torotatetheshape45degrees(π4 radians).

    3

  • ufo_matrixfun-mww5qn.zip