text_plain_output_1.png
from copy import deepcopy

from sklearn.metrics import f1_score, accuracy_score

import numpy as np

import pandas as pd

df = pd.read_csv('../input/train.csv')

df = df.set_index('ID_code')
y = df['target']
X = df.drop(['target'], axis=1)

X_train, X_test = (np.matrix(X_train_df.values), np.matrix(X_test_df.values))
y_train, y_test = (np.matrix(y_train_df).T, np.matrix(y_test_df).T)

def get_w_hat(X, t):
    xt_x_minus_one = np.linalg.inv(np.dot(X.T, X))
    xt_t = np.dot(X.T, t)
    w_hat = np.dot(xt_x_minus_one, xt_t)
    return w_hat
def get_sigma_hat(w, X, t, N):
    X_w = np.dot(X, w)
    t_minus_X_w = t - X_w
    sigma_hat = 1.0 / N * np.dot(t_minus_X_w.T, t_minus_X_w)
    return np.float(sigma_hat)
def get_Sigma_w(sigma_square, X, S):
    return np.linalg.inv(1.0 / sigma_square * np.dot(X.T, X) + np.linalg.inv(S))
def get_mean_w(sigma_square, Sigma, X, t):
    return 1.0 / sigma_square * np.dot(np.dot(Sigma, X.T), t)

class BayesianLinearRegressor:
    def __init__(self, n=1, sigma_unif=1.0):
        self.n = n
        self.sigma_unif = sigma_unif
    def lrfit(self, X_, t):
        X = get_X_powered(X_, self.n)
        X = get_X_w_bias(X)
        _, S = generate_prior(X.shape[1], sigma_unif=self.sigma_unif)
        w_hat = get_w_hat(X, t)
        sigma_square = get_sigma_hat(w_hat, X, t, X.shape[0])
        Sigma = get_Sigma_w(sigma_square, X, S)
        mean_w = get_mean_w(sigma_square, Sigma, X, t)
        self.mean_w, self.sigma_square, self.Sigma = (mean_w, sigma_square, Sigma)
    def predict(self, X):
        X_pow = get_X(X, self.n)
        return np.dot(X_pow, self.mean_w)

def get_X_powered(X, n):
    """
    Return the X matrix concat with all the samples powered to 2..n
    Args:
        X (numpy.matrix): Matrix (N, D)
            - N : number of rows
            - D : number of features
        n (str): The power of the matrix.
    Returns:
        X_result: Matrix X with all the n powers
    """
    if n <= 1:
        return X
    X_result = deepcopy(X)
    X_powered = deepcopy(X)
    for i in range(2, n + 1):
        X_powered = np.multiply(X_powered, X)
        X_result = np.concatenate((X_result, X_powered), axis=1)
    return X_result
def get_X_w_bias(X):
    """
    Return The X matrix with the bias term
    Args:
        X (numpy.matrix): Matrix X (N, D)
    Returns:
        X_result (numpy.matrix): Matrix X (N, D + 1) with bias term put on the right column
    """
    X_result = np.concatenate((X, np.matrix(np.ones(X.shape[0])).T), axis=1)
    return X_result
def get_X(X, n=1):
    """
    Put together the 2 functions above since they are often used together
    """
    return get_X_w_bias(get_X_powered(X, n))
def generate_prior(features, sigma_unif=3.0):
    S = np.matrix(np.diag(sigma_unif * np.ones(features)))
    return (np.matrix(np.zeros(features)), S)

def print_err(y_pred, y, total=False):
    """
    Display the error of the Regression predictions
    """
    err = y - y_pred
    tot_err = np.sum(np.multiply(err, err))

def sigmoid(x):
    return 1.0 / (1.0 + np.exp(-x))
@np.vectorize
def discretize(pred, threshold=0.5):
    """
    The decorator vectorize allows to apply the function to each element
    """
    return 1 if pred > threshold else 0

blr = BayesianLinearRegressor(n=3)
blr.lrfit(X_train, y_train)
y_test_und = blr.predict(X_test)

def print_metrics(y_test, y_pred):
    """
    Utility function to display the most important metrics
    """

y_pred = discretize(y_test_und, 0.5)
print_metrics(y_pred, y_test)