MA544_PA1

.pdf

School

Stevens Institute Of Technology *

*We aren’t endorsed by this school

Course

544

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

3

Uploaded by eliza1998

Report
Programming Assignment - 1 Programming Assignment - 1 Question 1 Question 1 Write a function trace1D() that finds the trace of a matrix given by a one dimensional NumPy array. You can not use the built-in function np.trace() or any other builtin function in any way. Display the output of the follwing print(“Trace of the suggested matrix is: {}”.format(trace1D(np.arange(0,100,1)))) In [1]: # import statements import import numpy numpy as as np np import import math math import import sys sys # Write your function here def def trace1D (mat1D): array_len = mat1D . shape[ 0 ] print ( "You have provided an array of length " ,array_len) n = math . ceil (math . sqrt(array_len)) # Check if you have sufficient number of elements to form a square matrix if if n > math . sqrt(array_len): # sys.exit(message) raises an exception too print ( "Please provide sufficient number of elements for a square matrix. System ex it." ) sys . exit() # Sum of the elements that will appear in the diagonal to give the trace sum = 0.0 for for k in in range (n): sum += #Your code here return return sum In [5]: # Show the output of the following print ( "Trace of the suggested matrix is: {} {} " . format(trace1D(np . arange( 0 , 100 , 1 )))) Question 2 Question 2 Read an appropriate RGB-image of your choice in a 3-D Tensor named myRGB. Perform the following operations on this tensor: (A) Pad the image by 50 pixels on all sides. This operation is akin to putting a dark frame around the image. Display this RGB image. In [ ]: # Follow the example from class. Modify it to the case of color images. (B) Use a sliding window of a 3x3 matrix, K (referred to as a kernel), to perform an operation called convolution on the original image. Display some of these images after convolution. Note that you only need to use appropriate slicing of the source image, element wise product, and np.sum in loops to perform this. You have provided an array of length 100 Trace of the suggested matrix is: 495.0
Your Observations? loops to perform this. Display the images after convolutions using K=[1 0 -1;0 0 0;-1 0 1] and K = [0 -1 0; -1 4 -1; 0 -1 0]. Describe what these convolutions have achieved. To know more about convolution and how to achieve it, read Section 9.2 in https://www.deeplearningbook.org/contents/convnets.html . Here is another resource on convolution: https://developer.apple.com/library/archive/documentation/Performance/Conceptual/vImage/ConvolutionOperatio In [ ]: # Work with a B&W image. myGray = # convert myRGB to monochromic # Create a padded image from myGray by padding one pixel on all sides. Why? myGray_padded = # Do it on myGray a B&W image # If the convoluted image is called myGray_conv myGray_conv = np . zeros_like(myGray) # Python code for convolution that needs modification by you. m,n = # Find dimension of the image K = # Take appropriate Kernel for for i in in range (m): for for j in in range (n): # Take appropriate slice of myGray_padded to get a (3x3) window on the image myGray_window = # Your code here # CODE for finding the i,j-th pixel on the convoluted image myGray_conv[i,j] = # Your code here # Display the convoluted image Question 3 Question 3 IRIS flower dataset is one of the widely used resources. Load the IRIS data by using the code below. Normalize this data by using z-scoring (z = (x - x_mean)/std). Don't use loops. You can use np.mean(), np.std() and basic matrix operation with broadcasting. Visualize the distribution of this data using Matplotlib or other packages. In [ ]: # Python code from from sklearn sklearn import import datasets iris = datasets . load_iris() . data In [ ]: # Your code here Question 4 Question 4 Modify the textual data example from NB1 to convert the document-term matrix into TF-IDF (term-frequency inverse document frequency) matrix by using basic NumPy operations. Term-frequency (TF) Term-frequency (TF) (of a word in a document) is frequency of the word in a document divided by total number of words in the document. Inverse document frequency (IDF) Inverse document frequency (IDF) of a word (all documents under consideration) is the natural log of (total number of documents/number of documents having the given word). See the discussion for an example tf-idf( t , d ) = tf( t , d ) ⋅ idf( t )
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help