models/feature_selection_methods.py
def chi2_fs(X_df,X_train_all,X_test_all,y_train,p_val_thresh):
    '''Chi2 statistical test for feature selection'''
    #Df with only continuous variables
    cont_data = X_df.loc[:, X_df.apply(lambda x: x.nunique()) >= 1000]

    #Find column indices of continuous features
    cont_data_id =  np.where(np.isin(X_df.columns, cont_data.columns))[0]

    cont_data_colnames = cont_data.columns

    #Remove continuous features for chi-sq
    X_train_fs = np.delete(X_train_all,cont_data_id,1)

    c,p = chi2(X_train_fs,y_train)

    feature_ids = np.where(p<=p_val_thresh)[0]

    #Df with no continuous variables. This is used for chi-sq feature selection
    data_fs = X_df.drop(cont_data.columns,axis=1)

    selected_features = np.append(cont_data_colnames,np.array(data_fs.columns[feature_ids]))
    selected_feature_id = np.where(np.isin(X_df.columns, selected_features))[0]

    #New X_train and X_test matrices
    X_train = X_train_all[:,selected_feature_id]
    X_test = X_test_all[:,selected_feature_id]

    X_train_df = pd.DataFrame(X_train,columns=selected_features)

    return selected_features, X_train_df, X_train, X_test
Chi2 statistical test for feature selection
Chi2 statistical test for feature selection
Chi2 statistical test for feature selection
def chi2_fs(X_df,X_train_all,X_test_all,y_train,p_val_thresh):
    cont_data = X_df.loc[:, X_df.apply(lambda x: x.nunique()) >= 1000]
    cont_data_id =  np.where(np.isin(X_df.columns, cont_data.columns))[0]
    cont_data_colnames = cont_data.columns
    X_train_fs = np.delete(X_train_all,cont_data_id,1)
    c,p = chi2(X_train_fs,y_train)
    feature_ids = np.where(p<=p_val_thresh)[0]
    data_fs = X_df.drop(cont_data.columns,axis=1)
    selected_features = np.append(cont_data_colnames,np.array(data_fs.columns[feature_ids]))
    selected_feature_id = np.where(np.isin(X_df.columns, selected_features))[0]
    X_train = X_train_all[:,selected_feature_id]
    X_test = X_test_all[:,selected_feature_id]
    X_train_df = pd.DataFrame(X_train,columns=selected_features)
    return selected_features, X_train_df, X_train, X_test
def chi2_fs ( X_df , X_train_all , X_test_all , y_train , p_val_thresh ) : cont_data = X_df . loc [ : , X_df . apply ( lambda x : x . nunique ( ) ) >= 1000 ] cont_data_id = np . where ( np . isin ( X_df . columns , cont_data . columns ) ) [ 0 ] cont_data_colnames = cont_data . columns X_train_fs = np . delete ( X_train_all , cont_data_id , 1 ) c , p = chi2 ( X_train_fs , y_train ) feature_ids = np . where ( p <= p_val_thresh ) [ 0 ] data_fs = X_df . drop ( cont_data . columns , axis = 1 ) selected_features = np . append ( cont_data_colnames , np . array ( data_fs . columns [ feature_ids ] ) ) selected_feature_id = np . where ( np . isin ( X_df . columns , selected_features ) ) [ 0 ] X_train = X_train_all [ : , selected_feature_id ] X_test = X_test_all [ : , selected_feature_id ] X_train_df = pd . DataFrame ( X_train , columns = selected_features ) return selected_features , X_train_df , X_train , X_test
Chi2 statistical test for feature selection
Chi2 statistical test for feature selection
'''Chi2 statistical test for feature selection''' #Df with only continuous variables #Find column indices of continuous features #Remove continuous features for chi-sq #Df with no continuous variables. This is used for chi-sq feature selection #New X_train and X_test matrices
X_df
X_train_all
X_test_all
y_train
p_val_thresh