Outline User-based knn Algorithm Basics of Matlab Control Structures Scripts and Functions Help
User-based knn Algorithm Three main steps Weight all users with respect to similarity with the active user. (Pearson, Spearman ) Select a subset of users as a set of predictors (topn neighbor, threshold) Compute predictions (no normalization, deviation from mean, z-score normalization)
i1 i2 i3 i4 i5 i6 b 5 4 1 2 c 2 4 1 3 1 d 5 3 2 4 e 5 2 1 3 f 3 5 1 2 4 Active user 4 4 2 3?
Z-score normalization based prediction
Basics of Matlab MATLAB is the high-level language and interactive environment. Matlab has many functions and toolboxes to help in various applications Computing matrix or vector formulas is easy compared to other programming languages.
Basics of Matlab Variables Files in current directory Command window Content of selected file Command history
Scripts and Functions There are two kinds of M-files: - Scripts, which do not accept input arguments or return output arguments. They operate on data in the workspace. Any variables that they create remain in the workspace, to be used in subsequent computations - Functions, which can accept input arguments and return output arguments. Internal variables are local to the function.
Scripts and Functions Functions are m-files which can be executed by specifying some inputs and supply some desired outputs. The code telling the Matlab that an m-file is actually a function is: function out1=functionname(in1) function out1=functionname(in1,in2,in3) function [out1,out2]=functionname(in1,in2) You should write this command at the beginning of the m-file and you should save the m-file with a file name same as the function name.
Scripts and Functions Example >> [a, c] = AreaCircumferenceCircle(3) a = 28.2743 c = 18.8496
Scripts and Functions Another function which takes an input array and returns the sum and product of its elements as outputs
Basics of Matlab To create a variable: a=5; //creates an integer valued of 5 b=0.5; // creates a decimal number valued as 0.5 C=[1 2 3]; //creates a vector D=[ 1 2 3; 4 5 6; 7 8 9]; //creates a 3x3 matrix To reach a specific element of a matrix, use A(i,j) where i reperesents row and j reperesents column. Matrices are defined using brackets [ and ]. ; provides to not print output of a command and seperates rows.
Basics of Matlab The prompt consists of two right arrows: >> Just type your command and press Enter. Matlab has all elementary functions. ->> sqrt(4) ans = 2 >> log(10) (Natural Logarithm) ans = 2.3026 >> log10(100) ans = 2 >> cos(pi) >> abs(-10) (Absolute Value) >> power(2,3) ans = 8
Basics of Matlab Create variables directly, and use them in other functions. All variables are created with double precision unless specified. >> a = 5^2/3 a = 8.3333 >> b = log(3) + cos(pi/3) b = 1.5986
Importing Data from Excel File >> Jester = xlsread('jester-data-3.xls'); >> JesterTest= mat2cell(jester,[100 24838]) >> JesterTestSet = [JesterTest{1}] C = mat2cell(a,dim1dist,,dimndist); C= mat2cell(x, [10, 20, 30], [25, 25]) (x is 60x50 array) OR SIMPLY JesterTestSet = Jester(1:100, :); To load a variable v from current folder: load v; To save a variable v to current folder as vname: save vname v;
Basics of Matlab Generating Matrices zeros(m,n) ones(m,n) eye(n)//identity matrix rand(m,n) [uniformly-distributed] Generate a 10x1 column vector of uniformly distributed numbers in the interval (-5,5) r = -5 + (5- (-5))*rand(10,1) R= randi([10 50],1,5); \\crates a matrix in size 1x5 including random integer numbers on [10,50]
Arrays >> A=ones(3,5) >> A=zeros(3,5) >> A=99*ones(3,5) >> A = [1 2 3 4 5 ; 3 3 3 3 3] >> A(1,2) = 5 >> c = A(2,2)
Basics of Matlab Colon : Operator 1:7 means 1 2 3 4 5 6 7 1:4:13 means 1 5 9 13 50:-5:20 means 50 45 40 35 30 25 20 A(:, 2) returns the second column of A A(2, :) returns the second row of A A(1:2, 1:3) returns the top two rows and first three columns
Basics of Matlab Matrix Concatenation A=[B C] concatenates B and C left-to-right A=[B;C] appends C to B
Basics of Matlab Deleting Rows or Columns A(5,:)=[] deletes the 5th row of A A(:,4)=[] deletes the 4th column of A
Basics of Matlab Obtaining Matrix Properties min(a) finds the minimum of each columns min(min(a)) finds the minimum element of A max(max(a)) finds the max. element of A sum(a) returns the summation of each column sum(a,2) returns the summation of each row sum(sum(a)) returns the summation size(a) returns the row and column counts length(d) returns the length of one-dimensional array D whos B shows the matrix properties
Arithmetic Operators Basics of Matlab + - *.*./.\ / \ ^.^ + [unary plus] e.g. +A - [unary minus] e.g. -A //transpose
Basics of Matlab Relational & Logical Operators < <= > >= == Equal to ~= Not equal to & AND OR ~ NOT
Control Structures Conditional Control - if, else, elseif - switch, case Write codes between if end, else end etc. blocks Loop Control - for, while, continue, break Write codes between for end, while end blocks. Program Termination - return
Control Structures If Statement Syntax if (Condition_1) Matlab Commands elseif (Condition_2) Matlab Commands elseif (Condition_3) Matlab Commands else Matlab Commands end Examples if a>5 & b~=8 Matlab Commands; end if (a<3) Matlab Commands; elseif (b==9) Matlab Commands; end if (a<=4) Matlab Commands; else Matlab Commands; end
For loop syntax Control Structures Examples for i=startindex:lastindex end Matlab Commands for i=1:50 Matlab Commands; end for j=2:2:30 Matlab Commands; end note:lastindex included for m=15:-0.5:-10 Matlab Commands; end for k=[5 8.3 12 9-6 -9.6] Matlab Commands; end
Control Structures While Loop Syntax while (condition) Matlab Commands end Example while a<=4 b==7 Matlab Commands; end
Statistical functions >> std(a) returns standard deviations of each column. >> std(a,0,2) returns standard deviations of each row. >> std(a,0,1) returns standard deviations of each column. var(a) >> mean(a) returns means of each column. >> mean(a,2) returns means of each row.
Help % is the neglect sign for Matlab (equivalent of // in C). Anything after it on the same line is neglected by Matlab compiler. Sometimes slowing down the execution is done deliberately for observation purposes. You can use the command pause for this purpose: pause %wait until any key pause(4) %wait 4 seconds
Help You can always use help of Matlab by typing >> help >> help command_name >> help toolbox_name