R Language Computing IV value and its use

Source: Internet
Author: User
More on wind control modeling, big data analysis and other content please pay attention to the public number "big data wind control bit by bit"

After splitting the variables, it is important to calculate the importance of the variables, and IV is one of the statistics that evaluates the sensitivity or importance of the variable, and the code for the R language calculates the IV value as follows:

CalcIV <- function(df_bin, key_var, y_var){ N_0<-table(df_bin[, y_var])[1] N_1<-table(df_bin[, y_var])[2] iv_c<-NULL var_c<-NULL for (col in colnames(df_bin)){   if (col != key_var && col != y_var) {     frq<-as.data.frame(table(df_bin[, col], df_bin[, y_var]))     len<-length(unique(frq$Var1))     iv<-0     for (i in 1:len){       N_i_0<-frq$Freq[frq$Var1==i & frq$Var2==0]       N_i_1<-frq$Freq[frq$Var1==i & frq$Var2==1]       iv<-iv+(N_i_0/N_0- N_i_1/N_1)*log((N_i_0/N_0)/(N_i_1/N_1))     }     iv_c<-c(iv_c, iv)     var_c<-c(var_c, col)   } } iv_df<-data.frame(var=var_c, iv=iv_c, stringsAsFactors = FALSE) return(iv_df)}

Where Df_bin is the data set after the bins, Key_var is the primary key, Y_var is the y variable (0 is good, 1 is bad). The result of the code operation is as follows:

R Language Computing IV value and its use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.