Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Calculation and use of IV value by python

Shulou Source: shulou.com Published: 2022-06-03 04:25:50 09月16日 Update

For more information on big data's analysis and modeling, please follow the official account "bigdatamodeling".

After dividing the variable into boxes, you need to calculate the importance of the variable. IV is one of the statistics to evaluate the differentiation or importance of the variable. The code for python to calculate the IV value is as follows:

Def CalcIV (Xvar Yvar): np.sum (Yvar==0) Numb1 = np.sum (Yvar==1) N_0_group = np.zeros (np.unique (Xvar) .shape) N_1_group = np.zeros (np.unique (Xvar) .shape) for i in range (len (np.unique (Xvar): N_0_group [I] = Yvar [(Xvar = = np.unique (Xvar) [I]) & (Yvar==0)]. Count () Numb1Group [I] = Yvar [(Xvar = = np.unique (Xvar) [I]) & (Yvar = = 1)] .count () iv = np.sum ((N_0_group/N_0-N_1_group/N_1) * np.log ((N_0_group/N_0) / (N_1_group/N_1)) return iv def caliv_batch (df) Kvar, Yvar): df_Xvar = df.drop ([Kvar, Yvar], axis=1) ivlist = [] for col in df_Xvar.columns: iv = CalcIV (df [col], df [Yvar]) ivlist.append (iv) names = list (df_Xvar.columns) iv_df = pd.DataFrame ({'Var': names,' Iv': ivlist}, columns= ['Var',' Iv']) return iv_df

Where df is the dataset after the box, Kvar is the primary key, and Yvar is the y variable (0 is good, 1 is bad). The running result of the code is as follows:

Tags: Variables importance code data importance public content data analysis more results differentiation analysis statistics evaluation run Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple vpn Shulou Information Huawei