logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

Write a function plotrandstem that will receive two parameters as input: the name of a text file and a number n

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

EXERCISE 1 (3 points)

Write a function plotrandstem that will receive two parameters as input: the name of a text file and a number n. The text file to be used as input to plotrandstem is already provided on the Moodle page (file pointsEx1.txt). plotrandstem will read pointsEx1.txt which contains the x and y coordinates for the data points and will create a stem plot using n points chosen at random. The format of every line in the file is the letter ‘x’, a space, the x value, a space, the letter ‘y’, space, and the y value. The number of points drawn will be shown in the plot title. For example, the function call:

plotrandstem (‘pointsEx1.txt’,10)

Your function will need to check that the value of n is smaller than the total number of data points in the file, and provide the user with an appropriate error message in case it is bigger.

(HINT: you could use fopen followed by fscanf to read the file).

EXERCISE 2          (4 points)

Write a Matlab function myplots that will received three input arguments:

  1. a cell array containing 4 strings taken from the set {sin, cos, tan, sinh, cosh, tanh}

  2. a vector of 4 characters specifying a colour taken from the set {k, m, c, r, g, b}

  3. a vector of 4 markers taken from the set {+, o, *, x, s, p}. Your function should produce one figure containing 4

The first subplot will contain the plot of the first mathematical function in the cell array (that is, the first string in the cell array). The second subplot will contain the second one, and so on for all 4 subplots. The title of each subplot should indicate the mathematical function being plotted.

The mathematical function in subplot i, will appear with colour and marker as specified by the corresponding i entries in the second and third input vectors. All mathematical functions should be plotted in the interval [-2p, 2p] with a step of 0.3 and a marker should appear at each step.

Your matlab function should output an error in case the input vectors have length different than 4. For example, the following call to myplots:

myplots({'sin', 'sinh', 'cos', 'cosh'}, ['b', 'r', 'k', 'g'], ['o', 'x', 's', '*'])

(Hint: the matlab function eval can be useful here.)

EXERCISE 3 (8 points)

In this exercise, you will write a vanilla version of the well-known k-means clustering algorithm for data points in 2 dimensions – this algorithm is part of the Data Analysis course. Briefly, the algorithm partitions a dataset of n points into k disjoint clusters; each cluster is identified by a centroid which changes its position during the learning; each point belongs to the cluster whose centroid is nearest to it.

The algorithm that you will implement is as follows:

  • Step 1: Random initialization of the centroids from the data. Select k points at random from the dataset as initial

  • Step 2: Compute the Euclidean distances between the data points and the centroids. [Hint: use the build-in function pdist2].

  • Step 3: Assign each point to the centroid which is closest to

  • Step 4: Update each of the K centroids by moving it to the mean of all the points which were assigned to

  • Step 5: Go to Step 2

Stopping criterion: The loop (steps 2 to 5) stops when the position of the centroids does not change between two consecutive iterations.

In this exercise you will write the function mykmeans that receives two inputs:

  • a dataset of n points in 2D (an example is provided in the file mat)

  • the number of clusters k.

mykmeans will implement the algorithm described above and will:

  • Return a matrix C of size k x 2, containing the final position of the centroids, one centroid per row.

  • Return a vector V specifying the cluster to which each point This vector will have n elements, and the ith element will contain an integer in the range [1, k] indicating the cluster to which the ith element belongs (note that V(i) specifies the cluster having C(V(i),:) as its centroid).

  • Plot a figure showing the different k clusters obtained, in different colours, together with their centroids – make sure the centroids are clearly visible in the figure. For example, using the data provided in the file mat, the call to the function:

[C, V] = mykmeans(Data, 2);

produced the following figure (here the centroids are drawn as large red diamonds).

[Warning: the above figures should only be taken as references since every run of k-means is likely to produce different clusterings as the algorithm is affected by local minima].

Finally, note that this exercise is about implementing k-means yourself, so you cannot use the kmeans

function available in Matlab.

EXERCISE 4 (10 points)

In this exercise you will write a script called docdistances that will calculate distances between pairs of text documents. These distances will be based on a vanilla version of term frequency–inverse document frequency (tf-idf). Your script will calculate the distances between 6 documents: 3 documents are synopsis of fairy tales (Red riding hood, the Princess and the pea and Cinderella); the other 3 documents are the abstract of papers related to protein function prediction (identified as CAFA1, CAFA2 and CAFA3). You will find these documents on the Moodle page (the files name are: RedRidingHood.txt, PrincessPea.txt, Cinderella.txt, CAFA1.txt, CAFA2.txt, CAFA3.txt).

Your script will:

  1. For each document, calculate its tf-idf

The tf-idf vector of a document is a vector whose length is equal to the total number of different terms (words) which are present in the corpus (in this case, the corpus is the entire set of 6 documents). Each term is assigned a specific element of the vector, which is in the same position for the tf-idf vector of every document. For a given document d, the vector element corresponding to term t is calculated as the product of 2 values:

  1. Term frequency: the number of times that term t appears in document d

  2. Inverse document frequency: the log base 10 of the inverse fraction of the documents that contain the term, e.

๐‘›๐‘ข๐‘š๐‘๐‘’๐‘Ÿ ๐‘œ๐‘“ ๐‘‘๐‘œ๐‘๐‘ข๐‘š๐‘’๐‘›๐‘ก๐‘  ๐‘–๐‘› ๐‘กโ„Ž๐‘’ ๐‘๐‘œ๐‘Ÿ๐‘๐‘ข๐‘ 

๐‘™๐‘œ๐‘”10 ๐‘›๐‘ข๐‘š๐‘๐‘’๐‘Ÿ ๐‘œ๐‘“ ๐‘‘๐‘œ๐‘๐‘ข๐‘š๐‘’๐‘›๐‘ก๐‘  ๐‘คโ„Ž๐‘’๐‘Ÿ๐‘’ ๐‘ก๐‘’๐‘Ÿ๐‘š ๐‘ก ๐‘Ž๐‘๐‘๐‘’๐‘Ž๐‘Ÿ๐‘ 

  1. Calculate the cosine distance between every pair of tf-idf vectors representing each document. (This is equal to 1 minus the cosine of the angle between the 2 )

  2. Collect these distances into a 6x6 matrix where the value in the (i,j) element contains the distance between document i and document j. Then make a figure that displays the Your Figure should look similar to the Figure below (here I have used imagesc and set the colormap to gray).

It is interesting to note that the 2 types of documents form 2 clear groups: the synopsis of fairy tales are more similar to each other than they are to scientific papers. Also, the Princess and the pea is more similar to Cinderella than to Red Riding Hood, and this makes sense as the Princess and the pea and Cinderella have many more elements in common …

[HINT: the functions textread and pdist could be useful for exercise 4]

EXERCISE 5

In this exercise you will explore Principal Components Analysis (PCA). The exercise is divided in 2 parts. In the first part, you will work with a small toy dataset of artificial data, developing functions needed for carrying out PCA. In the second part, you will work with a larger dataset of real world data (images of faces of famous people), and you will be able to re-use the functions you wrote for the small toy dataset on a larger scale.

Note that this is a good approach to use when implementing algorithms: make sure that they work well on small datasets and then apply them to your real problem. Remember that the implementation for the small dataset needs to be efficient, otherwise its execution time will be very large when applied to the larger dataset.

For both datasets, you will:

  1. Calculate the principal components of your data

  2. Project your high dimensional data onto (a smaller space defined by) a few principal components

  3. Recover your original high dimensional data by re-projecting back the projected data onto the original space.

Note also that this exercise is about implementing PCA yourself, so you cannot use any of the different functions available in Matlab which implement it (e.g. pca). You will have to calculate the principal components through the eigendecomposition of the covariance matrix of the data (you will need to use the Matlab function eig for the eigendecomposition).

Part 1 (8 points)

You will write a script called PCAEx5 which will work on a small dataset of 50 random points, Gaussian distributed, in 2D. These are contained in the file pcadata.mat, available on the Moodle page. Your script will:

  1. Load the datapoints contained in the file mat. Let us call X this initial set of datapoints. X has size 50x2 as there are 50 points in 2 dimensions.

  2. Create Figure In this figure, plot the points as blue circles in 2D, in a figure whose axis are set in the range xmin = 0, xmax=7, ymin=2, ymax=8

  3. Call a function called subtractMean that you will also write and include in the submission. The function receives a dataset (i.e. a matrix) as the only input argument, and returns two arguments: a dataset obtained from the input dataset by subtracting its mean; and the mean of the input dataset. Your script will run this function on your dataset X, and obtain a new dataset Xmu and the mean of X, which we shall call

  4. Call a function called myPCA that you will also write and include in the submission. This function receives a dataset (i.e. a matrix) as the only input argument, and returns two arguments: the first is a matrix in which the columns are the principal components (i.e. the eigenvectors of the covariance matrix of the dataset) and the second is the set of corresponding eigenvalues. The eigenvectors will need to be ordered according to the size of their corresponding eigenvalues, in decreasing order; that is, the first column will be the eigenvector corresponding to the largest eigenvalue, the second column will be the eigenvector corresponding to the second largest eigenvalue, and so on.Your script will run this function on your dataset Xmu, and obtain a matrix U of principal components and a vector S with their corresponding eigenvalues.

[HINT: the principal components are the eigenvectors of the covariance matrix of the data. So to implement this step you will need the function cov for calculating the covariance of the data, and the function eig, for calculating eigenvalues and eigenvectors.]

  1. Add to Figure 1 the plot of the 2 principal components, in which the first component (corresponding to the largest eigenvalue) is red, and the second one is green. Your Figure 1 should look similar to Figure A below. Also print out on the command window the coordinates of the top eigenvector. [HINT: you can use the command line to draw the Do not forget that the eigenvectors were calculated from data from which the mean had been subtracted. So you will need to add the mean to the eigenvectors in order to make the plot.]

  2. Call a function called projectData that you will also write and include in the submission. The function receives 3 arguments: a dataset (i.e. a matrix), a set of eigenvectors and a positive integer k. It provides as the only output argument a dataset obtained by projecting the input dataset onto the first k eigenvectors. Note that the first k eigenvectors are the k eigenvectors corresponding to the k largest

Your script will run this function on your dataset Xmu, using the eigenvectors in U and with k equal to 1 and obtain a matrix Z of projected data.

[HINT: here the projection of a datapoint onto an eigenvector can be obtained by calculating the dot product between the point and the vector, since the eigenvectors you obtain from Matlab have unit norm].

  1. Print in the command window the projection of the first 3 points in your dataset, i.e. Z(1:3, :).

  2. Call a function recoverData provided on the Moodle page. The function receives 4 arguments: a dataset (i.e. a matrix), a set of eigenvectors, a positive integer k and a vector mu. It provides as the only output argument a dataset obtained by projecting back your points onto the original space. Using the variable names described above the call in your script call would be:

Xrec = recoverData(Z, U, K, mu)

Note that this function will work correctly only if the eigenvectors in U are ordered according to the size of their corresponding eigenvalues, in decreasing order, as explained in point 4.

Your script will run the above line obtaining in Xrec the recovered datapoints.

  1. Create Figure In this figure, plot the points as blue circles in 2D, in a figure whose axis are set in the range xmin = 0, xmax=7, ymin=2, ymax=8. Then add the recovered points as red stars. (Using the variable names described above your recovered points will be contained in the variable Xrec). Your Figure 2 should look similar to the Figure B below.

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Um e HaniScience

932 Answers

Hire Me
expert
Muhammad Ali HaiderFinance

500 Answers

Hire Me
expert
Husnain SaeedComputer science

604 Answers

Hire Me
expert
Atharva PatilComputer science

686 Answers

Hire Me

Get Free Quote!

302 Experts Online