text_html_output_1.png
from sklearn.base import BaseEstimator, TransformerMixin

from sklearn.preprocessing import LabelEncoder

import matplotlib.pyplot as plt

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

import pylab

import seaborn as sns

dataset = pd.read_csv('../input/census-data-set/Census Income Dataset.csv', na_values=[' ?'])

test_dataset = pd.read_csv('../input/census-test-dataset/Census Income Testset.csv', na_values=[' ?'])

def Income_bracket_binarization(feat_val):
    if feat_val == '<=50K':
        return 0
    else:
        return 1
dataset['Income_bracket'] = dataset['Income_bracket'].apply(Income_bracket_binarization)
test_dataset['Income_bracket'] = test_dataset['Income_bracket'].apply(Income_bracket_binarization)

class CategoricalImputer:
    def __init__(self, columns=None, strategy='most_frequent'):
        self.columns = columns
        self.strategy = strategy
    def fit(self, X, y=None):
        if self.columns is None:
            self.columns = X.columns
        if self.strategy is 'most_frequent':
            self.fill = {column: X[column].value_counts().index[0] for column in self.columns}
        else:
            self.fill = {column: '0' for column in self.columns}
        return self
    def transform(self, X):
        for column in self.columns:
            X[column] = X[column].fillna(self.fill[column])
        return X

def Plot():
    #Find Indices where Income is >50K and <=50K
    fig = plt.figure(figsize=(15,15))
    fig.subplots_adjust(hspace=0.7, wspace=0.7)
    pylab.suptitle("Analyzing the dataset", fontsize="xx-large")
    plt.subplot(3,2,1)
    ax = sns.countplot(x='Age', hue='Income_bracket', data=dataset)
    plt.subplot(3,2,2)
    ax =sns.countplot(x='workclass', hue='Income_bracket', data=dataset)
    ax.set_xticklabels(ax.get_xticklabels(), rotation=40, ha="right")
    plt.subplot(3,2,3)
    ax =sns.countplot(x='Education', hue='Income_bracket', data=dataset)
    ax.set_xticklabels(ax.get_xticklabels(), rotation=40, ha="right")
    plt.subplot(3,2,4)
    ax = sns.countplot(x='Occupation', hue='Income_bracket', data=dataset)
    ax.set_xticklabels(ax.get_xticklabels(), rotation=40, ha="right")
    plt.subplot(3,2,5)
    ax = sns.countplot(x='Gender', hue='Income_bracket', data=dataset)
    ax.set_xticklabels(ax.get_xticklabels(), rotation=40, ha="right")
    plt.subplot(3,2,6)
    ax = sns.countplot(x='hours_per_week', hue='Income_bracket', data=dataset)
    
    return None

Plot()
X = dataset.drop('Income_bracket', axis=1)
y = dataset['Income_bracket']

class Categorical_Encoder(BaseEstimator, TransformerMixin):
    def __init__(self, columns=None):
        self.columns = columns
        self.encoders = None
    def fit(self, data, target=None):
        """
        Expects a data frame with named columns to encode.
        """
        if self.columns is None:
            self.columns = data.columns
        self.encoders = {column: LabelEncoder().fit(data[column]) for column in self.columns}
        return self
    def transform(self, data):
        """
        Uses the encoders to transform a data frame.
        """
        output = data.copy()
        for column, encoder in self.encoders.items():
            output[column] = encoder.transform(data[column])
        return output

categorical_features = {column: list(dataset[column].unique()) for column in dataset.columns if dataset[column].dtype == 'object'}
encoder = Categorical_Encoder(categorical_features.keys())
dataset = encoder.fit_transform(dataset)

dataset.head()