BookmarkSubscribeRSS Feed

분석 변수 Binning (그룹화) 방법

Started ‎06-14-2020 by
Modified ‎06-14-2020 by
Views 430

* Binning은 변수 값의 범위를 특정구간으로 나누어 설정하는 과정. 

 

* 아마도 E-Miner에서는 타겟을 고려한 다양한 방법의 binning 방법이 존재 하리라 생각이 됩니다.

BASE에는 9.4 이후 PROC HPBIN 프로시져가 도입이 되었으나 카겟을 고려한 방법이 지원을 하지 않아서 아래와 같은 방법을 고려해보았습니다. 참조해보시기를 바라고, 다른 방법이 있으시면 공유해주시기를 바랍니다.

 

 

- PROC HPBIN

- PROC SPLIT

- R 패키지를 사용하여서 결과 반환.

 

개별 변수에 대하여 그룹핑(BINNING) 작업 시 일반적으로 빈도,카이제곱값, WOE값 등을 기준으로 수작업 처리하는 방식을 사용하고 있는데, SAS 9.4에서 처리 시에 개별 단변량에 대하여 PROC HPSPLIT를 사용하여서 변수를 나누는 방식(노드)을 고려해도 좋을 듯 싶습니다. ( 이 방식은 그룹(노드) 개수를 정확하게 결정하기는 쉽지 않음. )

참고하시기를 바랍니다.

 

별도로 9.4 버젼의 PROC HPBIN 프로시져를 사용하여서 변수를 Binning 할 수 있지만, 타겟 변수을 고려하는 방법은 현재 지원이 안된다. 

binning 결과에 대하여 WOE와 IV 값은 산출 가능.

- calculates the weight of evidence (WOE) and information value (IV) based on binning results

 

 

* 종속변수 : 연속형;

PROC HPSPLIT DATA=DRIVER_DATA MAXDEPTH=4 MAXBRANCH=2 ASSIGNMISSING=BRANCH;

     ID PLCY_NO;

     MODEL NBSIN_1 = PROF_08;

     OUTPUT OUT=OUTTEST;

     CODE FILE ="D:\통계분석연구회.SAS";

     RULES FILE="D:\통계분석연구회.TXT";

RUN;

 

* 이진트리의 경우 종속변수(Y/N)를 0과1로 처리;

PROC HPSPLIT DATA=DRIVER_DATA;

     ID PLCY_NO;

     MODEL TARGET_10 = PROF_16;

     OUTPUT OUT=OUTTEST1;

     CODE FILE ="D:\통계분석연구회.SAS";

     RULES FILE="D:\통계분석연구회.TXT";

RUN;

 

***************************************************************; 

* R 패키지로 처리 후 결과 값 반환 받기

***************************************************************; 

 

* config 파일에 " -RLANG" 옵션 추가;

 

* R 패키 사용 가능 여부 확인 방법;

proc options option=RLANG;

run;

 

* 참고 : http://support.sas.com/documentation/cdl/en/imlug/63541/HTML/default/viewer.htm#imlug_r_sect003.htm;

 

***************************************************************;

* binary 타겟 데이터에 대한 binning;

* smbinning 예제;

* The main purpose of the package is to categorize a numeric variable into bins

* mapped to a binary target variable for its ulterior usage in scoring modeling;

***************************************************************;

proc iml;

  submit / R;

    # /* Package loading and data exploration */

    library(smbinning) # /* 사용 패키지 로딩 */

    data(chileancredit) # /* 분석을 위하여 사용 데이터 로딩 */

    chileancredit.train=subset(chileancredit,FlagSample==1)

    result=smbinning(df=chileancredit.train,y="FlagGB",x="TOB",p=0.05) # /* smbinning 실행 */

 

    # /* Generate new binned characteristic into a existing data frame */

    chileancredit= smbinning.gen(chileancredit,result,"gTOB") # /* 대상 데이터에 binning 결과 추가 */

    endsubmit;

    call ImportDataSetFromR("Work.MyData", "chileancredit"); # /* R 패키지의 데이터 프레임을 SAS 데이터로 전환 */

QUIT;

 

* sashelp.class;

* smbinning 패키지의 타겟은 Binary(0/1) 형식이어야 함.;

DATA CLASS;

 SET SASHELP.CLASS;

     IF sex = '남' THEN sex_tar = 0;

     ELSE sex_tar = 1;

RUN;

 

* 예제 데이터 생성;

DATA CLASS;

 SET CLASS CLASS CLASS CLASS CLASS CLASS CLASS CLASS CLASS;

RUN;

 

* 현재 x와 y는 R-project에서 integer 형식이어야 함.;

proc iml;

     run ExportDataSetToR("work.Class", "class" );  # /* SAS 데이터 세트를 R 패키지의 데이터 프레임으로 전환 */

     submit / R;

       print(class)

       print(str(class))

       # /* integer 형식으로 변경 */

       class$sex_tar <- as.integer(class$sex_tar)

       class$Weight <- as.integer(class$Weight)

       print(str(class))

 

       # /* Load package and its data */

       library(smbinning)

 

       # /* Run and save result */

       result=smbinning(df=class,y="sex_tar",x="Weight",p=0.05) 

       print(result)

       # /* Generate new binned characteristic into a existing data frame */

       chileancredit= smbinning.gen(class,result,"gTOB") # /* Update population */

     endsubmit;

     call ImportDataSetFromR("Work.MyData", "chileancredit");

QUIT;

 

***************************************************************;

* R Package 'binst'

* Data Preprocessing, Binning for Classification and Regression

* 참조 : https://github.com/Jules-and-Dave/binst;

***************************************************************;

* create_breaks(x, y = NULL, method = "kmeans", control = NULL, ...);

 

* A convenience functon for creating breaks with various methods;

* decisiontrees(dt,ctrees) : Create breaks using decision trees (recursive partitioning)

  create_dtbreaks(x, y, control = control) 

 

* earth(mars) : Create breaks using earth (i.e. MARS)

  create_earthbreaks(x, y, control = control)

 

* mdlp(entropy) : Create breaks using mdlp;

* create_mdlpbreaks(x, y);

 

* jenks : Create Jenks breaks

  create_jenksbreaks(x, control = control)

 

* kmeans : Create kmeans breaks.

create_kmeansbreaks(x, control = control);

 

DATA IRIS;

 SET SASHELP.IRIS;

     IF Species = 'Setosa'          THEN TARGET=1;

     ELSE IF Species = 'Versicolor' THEN TARGET=2;

     ELSE IF Species = 'Virginica'  THEN TARGET=3;

 

     if      _n_ <=10 then nom_var = '01';

     else if _n_ <=20 then nom_var = '02';

     else if _n_ <=30 then nom_var = '03';

     else if _n_ <=40 then nom_var = '04';

     else if _n_ <=50 then nom_var = '05';

     else if _n_ <=60 then nom_var = '06';

     else if _n_ <=70 then nom_var = '07';

     else if _n_ <=80 then nom_var = '08';

     else if _n_ <=90 then nom_var = '09';

     else if _n_ <=100 then nom_var = '10';

     else if _n_ <=110 then nom_var = '11';

     else if _n_ <=120 then nom_var = '12';

     else if _n_ <=130 then nom_var = '13';

     else if _n_ <=140 then nom_var = '14';

     else nom_var = '15';

RUN;

 

* 독립변수 : 연속형, 종속변수 : 명목형;

proc iml;

     run ExportDataSetToR("work.IRIS", "sasiris" );

     submit / R;

       # print(sasiris)

       print(str(sasiris))

       # /* Load package and its data */

       library(binst)

       library(earth)

       library(plotmo)

       library(plotrix)

       library(TeachingDemos)

       library(ape)

       library(BAMMtools)

       dt_breaks <- create_breaks(sasiris$SepalLength, sasiris$Species, method="mdlp")

       print(dt_breaks)

       sasiris$db_breaks <- create_bins(sasiris$SepalLength, dt_breaks)

     endsubmit;

     call ImportDataSetFromR("Work.iris_mdlp", "sasiris");

QUIT;

 

* 독립변수 : 연속형, 종속변수 : 연속형 -> earth;

proc iml;

     run ExportDataSetToR("work.iris", "sasiris" );

     submit / R;

       # print(sasiris)

       print(str(sasiris))

       # /* Load package and its data */

       library(binst)

       library(earth)

       library(plotmo)

       library(plotrix)

       library(TeachingDemos)

       library(ape)

       library(BAMMtools)

       dt_breaks <- create_breaks(sasiris$SepalWidth, sasiris$SepalLength, method="earth", control=list(glm=list(family=gaussian)))

       print(dt_breaks)

       sasiris$db_breaks <- create_bins(sasiris$SepalWidth, dt_breaks)

     endsubmit;

     call ImportDataSetFromR("Work.iris_earth", "sasiris");

QUIT;

Version history
Last update:
‎06-14-2020 10:43 PM
Updated by:
Contributors

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Article Labels
Article Tags