text_html_output_1.png
from sklearn.base import BaseEstimator, TransformerMixin

from sklearn.preprocessing import LabelEncoder

from tensorflow.python.framework import ops

import math

import matplotlib.pyplot as plt

import numpy as np

import numpy as np # linear algebra

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

import pylab

import seaborn as sns

import tensorflow as tf

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)

data = dataset.values
X_train = np.float32(data[:, [0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12]])
Y_train = data[:, [13]]

def computeCost(A, y):
    """
    Computes the cost using the sigmoid cross entropy
    Arguments:
    logits -- vector containing z, output of the last linear unit (before the final sigmoid activation)
    labels -- vector of labels y (1 or 0) 
    Note: What we've been calling "z" and "y" in this class are respectively called "logits" and "labels" 
    in the TensorFlow documentation. So logits will feed into z, and labels into y. 
    Returns:
    cost -- runs the session of the cost (formula (2))
    """
    A = tf.cast(A, tf.float32)
    y = tf.cast(y, tf.float32)
    cross_entropy_cost = tf.nn.sigmoid_cross_entropy_with_logits(logits=A, labels=y)
    return cross_entropy_cost

def Xavier_Intializer(dim):
    tf.set_random_seed(1)
    w = tf.get_variable('w', [dim, 1], initializer=tf.contrib.layers.xavier_initializer(seed=1))
    b = 0
    assert w.shape == (dim, 1)
    assert isinstance(b, float) or isinstance(b, int)
    return (w, b)

def propagate(X, y, w, b):
    Z = tf.add(tf.matmul(X, w), b)
    A = tf.sigmoid(Z)
    return A

def random_mini_batches(X, Y, mini_batch_size, seed=0):
    m = X.shape[1]
    mini_batches = []
    np.random.seed(seed)
    permutation = list(np.random.permutation(m))
    shuffled_X = X[:, permutation]
    shuffled_Y = Y[:, permutation].reshape((Y.shape[0], m))
    num_complete_minibatches = math.floor(m / mini_batch_size)
    for k in range(0, num_complete_minibatches):
        mini_batch_X = shuffled_X[:, k * mini_batch_size:k * mini_batch_size + mini_batch_size]
        mini_batch_Y = shuffled_Y[:, k * mini_batch_size:k * mini_batch_size + mini_batch_size]
        mini_batch = (mini_batch_X, mini_batch_Y)
        mini_batches.append(mini_batch)
    if m % mini_batch_size != 0:
        mini_batch_X = shuffled_X[:, num_complete_minibatches * mini_batch_size:m]
        mini_batch_Y = shuffled_Y[:, num_complete_minibatches * mini_batch_size:m]
        mini_batch = (mini_batch_X, mini_batch_Y)
        mini_batches.append(mini_batch)
    return mini_batches

def model(X_train, Y_train, minibatch_size, learning_rate=0.005, num_iterations=1500, num_epochs=1500, print_cost=True):
    ops.reset_default_graph()
    m, n_x = X_train.shape
    n_y = Y_train.shape[0]
    costs = []
    seed = 3
    w, b = Xavier_Intializer(n_x)
    A = propagate(X_train, Y_train, w, b)
    cost = computeCost(A, Y_train)
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        for epoch in range(num_epochs):
            epoch_cost = 0.0
            num_minibatches = int(m / minibatch_size)
            seed = seed + 1
            minibatches = random_mini_batches(X_train.T, Y_train, minibatch_size, seed)
            for minibatch in minibatches:
                minibatch_X, minibatch_Y = minibatch
                _, minibatch_cost = sess.run([optimizer, cost], feed_dict={X: minibatch_X, Y: minibatch_Y})
                epoch_cost += minibatch_cost / num_minibatches
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)
        w = sess.run(w)
        b = sess.run(b)
        return (w, b)

w, b = model(X_train, Y_train, minibatch_size=256)