This lab is about getting started with
Matlab and data generation. The goal of the lab is to get familiar
with Matlab syntax and to get a practical
grasp of what we have discussed in class. Follow the instructions
below. Think hard before you call the instructors!
Check out getstart.pdf guide for a quick introduction to Matlab.
Use the Matlab functions in the zip file.
1.A By using the Matlab shell,
create a column vector v
= [1; 2; 3] and a row vector u
= [1,2,3]. What happens with the command v'?
How
is the operation “'“
called in matrix terms?
Create z
= [5;4;3].
What happens by doing v
+ z?
And u +
z?
1.B Create the matrix A
= [1 2 3; 4 5 6; 7 8 9] and the matrix B
= A'; What kind of matrix is C
= A + B?
What happens typing A(:,1)
and A(1,:)?
By
typing A(2:3,:)
and A(:)?
1.C Play with the product “*”.
What happens with 2*u,
u*2, 2*v?
What
happens with u*v
and v*u,
why?
With A*v,
u*A and A*u?
1.D Use the pointwise operators “.*” and “./”. What happens with v.*z and v./z ? Why aren't A*A and A.*A the same?
1.E Use the following functions :
zeros, ones,
rand, randn
(type help "function
name" if needed).
Create
a 3x5 matrix of all zeros, all ones, random numbers uniformly
distributed between 2 and 3 and random numbers distributed according
to a Gaussian of variance 2.
Finally, use the functions eye
and diag, create
a 3x3 identity matrix and a matrix whose diagonal is the vector v.
Open the Matlab file MixGauss.m
2.A The function MixGauss(means, sigmas, n) generates datasets where the distribution of each class is a Gaussian with given a mean and variance.
2.B Have a look at the code or, for a quick help, type "help MixGauss" on the Matlab shell.
2.C Type on the Matlab shell the commands
[X1, Y1] =
MixGauss([[0;0],[1;1]],[0.5,0.25],1000)
figure(1);
scatter(X1(:,1),X1(:,2),25,Y1); %type "help scatter" to see
what the parameters mean
title('dataset 1')
2.D Now generate a more complex dataset following the instructions below.
Call
MixGauss with appropriate parameters and produce a dataset with
four classes: the classes must live in the 2D space and each should
be centered on the corners of the unit square (0,0), (0,1) (1,1),
(1,0), all with variance 0.2.
[X2,C]=MixGauss(....)
Use the Matlab function "scatter" to plot the points.
Manipulate the data so to obtain a 2-class problem where data on opposite corners share the same class (if you produced the data following the centers order provided above, you may use the function "mod" for a quick result: Y2=mod(C,2) )
2.E Following the same procedure as above (section 2.D) generate a new set of data, from the same distribution, to be used as a test set: (X2t,Y2t)
3.A Generate more complex datasets with the MixGauss function, for instance by choosing larger variances, higher dimension, etc.
3.B You may also add noise to the data by randomly flipping the labels on the training set (set the percentage of flipped labels)
3.C Given a data set compute the distances among all input points (try not to use "for" loops): how does the mean distance change with the number of dimensions?
3.D
Generate regression data
considering a regression model defined by a linear function with
Gaussian noise. Create a Matlab
function with in input the number of points n,
the number of dimensions D,
a D dimensional
coefficients vector w
and a noise
level delta. The
output should be a n
by d
matrix X
and an
n by
1 vector
Y. You
might want to start with a 1 or 2 dimensional case, but also try
higher dimensionalities.
Plot
the regression data.
3.E Generate regression data considering a 1-dimensional regression model defined by a non linear function.
3.F Generate a data-set (either for classification or regression) where most of the input variables are "noise", in the sense that they are unrelated to the output.