Question

Question 1: using python Given a matrix, please write a python script to calculate the singular...

Question 1: using python

Given a matrix, please write a python script to calculate the singular value decomposition (SVD).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The calculate SVD, following packages should be installed with in PYTHON:

  1. numpy
  2. scipy

numpy is to define array (matrix).

scipy to calculate SVD.

Steps:

  1. define a matrix/array
  2. output the matrix
  3. use svd() method from scipy which takes matrix/array as input and returns the value of U, s and VT
  4. output the calculated SVD

CODE (PLEASE REFER THE SCREENSHOTS FOR CODE INDENTATION):

'''
Given a matrix, please write a python script to calculate the singular
value decomposition (SVD)

The pre-requisite for program is numpy and scipy module

'''
# import array from numpy
from numpy import array

# import svd from scipy.linalg
from scipy.linalg import svd

# input matrix
matrix = array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print ("Input matrix = ")
print(matrix)

# calculate SVD using scipy svd() method.
# it returns U , s and VT
U, s, VT = svd(matrix)

# print the SVD
print("\nU =")
print (U)
print ("\ns =")
print(s)
print("\nVT =")
print(VT)

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Question 1: using python Given a matrix, please write a python script to calculate the singular...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT