mapfeature.m

255 views
Skip to first unread message

Michael Taylor

unread,
Dec 1, 2011, 1:00:44 PM12/1/11
to ai-ml...@googlegroups.com

Does any1 know how modify mapFeature to work with any number of X input features?

I have it working for 2 features and I suppose I could hardwire it for my problem I just want to create a dynamic function that can handle any number of X.

 
degree = 2;
out = ones(size(X1(:,1)));
for i = 1:degree
for j = 0:i
out(:, end+1) = (X1.^(i-j)).*(X2.^j);
end
end
 
 
I have this so far it works with a second order polynomial but nothing else
 
 
I was wondering if some 1 could give me some pointers with this I am making "progress" but my solutions make me want to barf.
 
 
Thank you,
Michael

Herbert Mühlburger

unread,
Dec 1, 2011, 1:21:04 PM12/1/11
to ai-ml...@googlegroups.com
Hi Michael!

Am 01.12.11 19:00, schrieb Michael Taylor:


> Does any1 know how modify mapFeature to work with any number of X input
> features?

If I understand you right then you want to create polynomial features.

> I have it working for 2 features and I suppose I could hardwire it for
> my problem I just want to create a dynamic function that can handle any
> number of X.
>
> degree = 2;
> out = ones(size(X1(:,1)));
> for i = 1:degree
> for j = 0:i
> out(:, end+1) = (X1.^(i-j)).*(X2.^j);
> end
> end
> I have this so far it works with a second order polynomial but nothing else
> http://pastebin.com/puD0MRQR
> I was wondering if some 1 could give me some pointers with this I am
> making "progress" but my solutions make me want to barf.

In order to create polynomial features from a given 1D feature vector
you can use the following octave/matlab function:

https://gist.github.com/1418711

Hopefully this helps you!

Cheers,
Herbert

--
=================================================================
Herbert Muehlburger Software Development and Business Management
Graz University of Technology
www.muehlburger.at www.twitter.com/hmuehlburger
=================================================================

Michael Taylor

unread,
Dec 1, 2011, 1:58:23 PM12/1/11
to ai-ml...@googlegroups.com
Hi Herbert,
 
Thank you for the code however I was actually not running into the problem for 1D or 2D I am trying to write code for Nth'D
 
like x1, x2, x3, x4 (with n amount of input features)
 
One of the reason's why this is so important is because you will have vector X with any amount of dimensions in kaggle competitions and dynamic code that can handle that would be awesome which I am trying to hack up right now(I am kinda settling for anything that works doesnt have to look pretty).
 
 
Thank you,
Michael
 


 
2011/12/1 Herbert Mühlburger <herbert.m...@gmail.com>

Paul Alford

unread,
May 12, 2013, 6:24:43 AM5/12/13
to ai-ml...@googlegroups.com
Hi Michael,

Did you ever find a solution to this problem?

Thanks
Paul

Mark Rees

unread,
Mar 9, 2016, 4:05:37 PM3/9/16
to ai-ml-class
I don't think the original question was answered. It is really a math problem. Here is code that I believe does mapfeature for 3 variables
X1=2
X2=3
X3=5
degree = 3;

out = ones(size(X1(:,1)));
for i = 1:degree
    for j = 0:i
      for k = 0:j
        out(:, end+1) = (X1.^(i-j)).*(X2.^(j-k)).*(X3.^(k));
      end 
    end
end

end

This seems to also work for 4 variables
X1=2
X2=3
X3=5
X4=6
degree = 3;

out = ones(size(X1(:,1)));
for i = 1:degree
    for j = 0:i
      for k = 0:j
        for l = 0:k
          out(:, end+1) = (X1.^(i-j)).*(X2.^(j-k)).*(X3.^(k-l)).*(X4.^(l));
        end
      end 
    end
end

end

I am not sure an easier way to write this as I can see if you have 300 features this would be a monster to write, however I believe it solves the problem. Hope this helps
Reply all
Reply to author
Forward
0 new messages