Skip to content

Instantly share code, notes, and snippets.

@caub
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caub/7f9debb0b1b9d31b6ba2 to your computer and use it in GitHub Desktop.
Save caub/7f9debb0b1b9d31b6ba2 to your computer and use it in GitHub Desktop.
normalizes data
% classic case, feature are by column
function [Xnorm, Xmean, Xsigma] = normalizeFeature(X)
Xmean = mean(X);
Xnorm = bsxfun(@minus, X, Xmean);
Xsigma = sqrt(sum(Xnorm.^2)/(size(Xnorm,1)-1));
Ynorm = bsxfun(@rdivide, Xnorm, Xsigma);
% all this is equivalent to zscore(X) :)
% for example collaborative filtering sets, classes are in row, and users in col
% R is a logical matrix for telling if the vote exist in Y
function [Ynorm, Ymean] = normalizeRatings(Y, R)
YR = Y.*R;
Ymean = sum(YR,2) ./ sum(R,2);
Ynorm = bsxfun(@minus, YR, Ymean);
Ynorm(~R)=0;
function [ as_ ] = polyMultiFeatures( items, k )
as = [];
function recurse(a, i)
% we should optimize and early stop a with length>k
if i>size(items,2)
if size(a,2)<=k
as{end+1} = a;
end
return;
end
a1 = a;
% recurse all possibilities
recurse(a1, i+1); % don't use item i
for j=1:k
a2 = [a repmat(items(:,i), 1, j)]; % use item i, j times
recurse(a2, i+1);
end
end
recurse([], 1);
% remove first one which is empty
as(1) = [];
% now reduce all items by multiplication
as_ = zeros(size(items,1),length(as));
for j=1:length(as)
as_(:,j) = prod(as{j},2);
end
end
% let's test
X = [1:3; 2:4]
n = size(X,2);
k = 3;
A = polyMultiFeatures(X, k)
%let's check if we have the right length
combinationWithRepetitions = @(n,k) factorial(n+k-1)./(factorial(n-1)*factorial(k));
length(A) == sum(combinationWithRepetitions(n,1:k))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment