Reputation: 5405
I am participating in a data science competition and my final predictions would be measured via a GINI Index. It is a regression problem. I have the source code for the calculation in SAS but I dont know SAS and am not able to understand what is going on.
I want to build the same in Python. Any help would be appreciated. If someone knows Python code for this, it would help a lot.
*define GINI;
%macro gini(input=, output=, y=, py=, filter=, split_ind = );
data indsn;
set &input.;
_random=ranuni (123456789);
w=1;
if &split_ind.="&filter.";
run;
proc sort data=indsn;by &py _random;run;
/*accumulate w to calculate Gini */
data test;
set indsn;
if _N_ = 1 then do;
cumm_w0=0;
end;
retain cumm_w0
;
cumm_w0=cumm_w0+w;
run;
/*calcualate Gini */
proc sql noprint;
create table &output
as
select 1-2/(sum(w)-1)*(sum(w)-sum(&y.*cumm_w0*w)/sum(&y.*w)) as gini
from test;
quit;
proc print data=&output;
title " GINI on &filter.";run;
%mend;
Upvotes: 0
Views: 2227
Reputation: 9569
This looks like an implementation of the bottom formula in this section of the wikipedia article on the Gini Coefficient:
https://en.wikipedia.org/wiki/Gini_coefficient#Alternate_expressions
Upvotes: 0