text_plain_output_1.png
from sklearn import linear_model

from sklearn import metrics

from sklearn import model_selection

from sklearn import preprocessing

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

df = pd.read_csv('../input/adult-census-income/adult.csv')
df.income.value_counts()

from sklearn import model_selection
df['kfold'] = -1
df = df.sample(frac=1).reset_index(drop=True)
y = df.income.values
kf = model_selection.StratifiedKFold(n_splits=5)
for fold, (train_, valid_) in enumerate(kf.split(X=df, y=y)):
    df.loc[valid_, 'kfold'] = fold
df.to_csv('./adult_folds.csv', index=False)

df_fold = pd.read_csv('./adult_folds.csv')
df_train = df[df.kfold != 0].reset_index(drop=True)
df_train.income.isnull().sum()

from sklearn import linear_model
from sklearn import metrics
from sklearn import preprocessing
def run(fold):
    df = pd.read_csv('./adult_folds.csv')
    num_cols = ['fnlwgt', 'age', 'capital.gain', 'capital.loss', 'hours.per.week']
    df = df.drop(num_cols, axis=1)
    target_mapping = {'<=50K': 0, '>50K': 1}
    df.loc[:, 'income'] = df.income.map(target_mapping)
    features = [f for f in df.columns if f not in ('kfold', 'income')]
    for col in features:
        df.loc[:, col] = df[col].astype(str).fillna('NONE')
    df_train = df[df.kfold != fold].reset_index(drop=True)
    df_valid = df[df.kfold == fold].reset_index(drop=True)
    ohe = preprocessing.OneHotEncoder()
    All_data = pd.concat([df_train[features], df_valid[features]], axis=0)
    ohe.fit(All_data[features])
    x_train = ohe.transform(df_train[features])
    x_valid = ohe.transform(df_valid[features])
    model = linear_model.LogisticRegression(solver='liblinear')
    model.fit(x_train, df_train.income.values)
    valid_preds = model.predict_proba(x_valid)[:, 1]
    auc = metrics.roc_auc_score(df_valid.income.values, valid_preds)
    print(f'Fold = {fold}, AUC = {auc}')
if __name__ == '__main__':
    for fold_ in range(5):
        run(fold_)