<commit_before>#!/usr/bin/env python

"""
Construct a neural network model and support vector regression model from the data
"""

import pickle

import numpy as np
from lasagne import nonlinearities
from lasagne.layers import DenseLayer
from lasagne.layers import InputLayer
from nolearn.lasagne import NeuralNet
from sklearn import svm, metrics

__author__ = "<NAME>"
__credits__ = "<NAME>"
__license__ = "BSD 3-Clause License"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Development"

NODES = 10
NN_PICKLE = 'nn_data.pkl'
SVM_PICKLE = 'svm_data.pkl'


def build_nn(x_train, y_train, x_val, y_val):
    """
    Construct a regression neural network model from input dataframe

    :param x_train: features dataframe for model training
    :param y_train: target dataframe for model training
    :param x_val: features dataframe for model validation
    :param y_val: target dataframe for model validation
    :return: None
    """

    # Create classification model
    net = NeuralNet(layers=[('input', InputLayer),
                            ('hidden0', DenseLayer),
                            ('hidden1', DenseLayer),
                            ('output', DenseLayer)],
                    input_shape=(None, x_train.shape[1]),
                    hidden0_num_units=NODES,
                    hidden0_nonlinearity=nonlinearities.softmax,
                    hidden1_num_units=NODES,
                    hidden1_nonlinearity=nonlinearities.softmax,
                    output_num_units=len(np.unique(y_train)),
                    output_nonlinearity=nonlinearities.softmax,
                    update_learning_rate=0.1,
                    verbose=1,
                    max_epochs=100)

    param_grid = {'hidden0_num_units': [1, 4, 17, 25],
                  'hidden0_nonlinearity': 
                  [nonlinearities.sigmoid, nonlinearities.softmax],
                  'hidden1_num_units': [1, 4, 17, 25],
                  'hidden1_nonlinearity': 
                  [nonlinearities.sigmoid, nonlinearities.softmax],
                  'update_learning_rate': [0.01, 0.1, 0.5]}
    grid = sklearn.grid_search.GridSearchCV(net, param_grid, verbose=0,
                                            n_jobs=3, cv=3)
    grid.fit(x_train, y_train)

    y_pred = grid.predict(x_val)
    # Accuracy prediction score
    accuracy = sklearn.metrics.accuracy_score(y_val, y_pred)

    with open(NN_PICKLE, 'wb') as results:
        pickle.dump(grid, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(net, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(accuracy, results, pickle.HIGHEST_PROTOCOL)


def build_svm(x_train, y_train, x_val, y_val):
    """
    Construct a regression support vector regression model from input dataframe

    :param x_train: features dataframe for model training
    :param y_train: target dataframe for model training
    :param x_val: features dataframe for model validation
    :param y_val: target dataframe for model validation
    :return: None
    """

    clf = svm.SVR()
    clf.fit(x_train, y_train)
    y_pred = clf.predict(x_val)

    # Mean absolute error regression loss
    mean_abs = metrics.mean_absolute_error(y_val, y_pred)
    # Mean squared error regression loss
    mean_sq = metrics.mean_squared_error(y_val, y_pred)
    # Median absolute error regression loss
    median_abs = metrics.median_absolute_error(y_val, y_pred)
    # R^2 (coefficient of determination) regression score function
    r2 = metrics.r2_score(y_val, y_pred)
    # Explained variance regression score function
    exp_var_score = sklearn.metrics.explained_variance_score(y_val, y_pred)

    with open(SVM_PICKLE, 'wb') as results:
        pickle.dump(mean_abs, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(mean_sq, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(median_abs, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(r2, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(exp_var_score, results, pickle.HIGHEST_PROTOCOL)<commit_msg>Added build decision tree model function<commit_after>#!/usr/bin/env python

"""
Construct a neural network model, support vector and decision trees regression models from the data
"""

import pickle

import numpy as np
from lasagne import nonlinearities
from lasagne.layers import DenseLayer
from lasagne.layers import InputLayer
from nolearn.lasagne import NeuralNet
from sklearn import svm, metrics, tree, grid_search

__author__ = "<NAME>"
__credits__ = "<NAME>"
__license__ = "BSD 3-Clause License"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Development"

NODES = 10
NN_PICKLE = 'nn_data.pkl'
SVM_PICKLE = 'svm_data.pkl'
DT_PICKLE = 'dt_data.pkl'


def build_nn(x_train, y_train, x_val, y_val):
    """
    Construct a regression neural network model from input dataframe

    :param x_train: features dataframe for model training
    :param y_train: target dataframe for model training
    :param x_val: features dataframe for model validation
    :param y_val: target dataframe for model validation
    :return: None
    """

    # Create classification model
    net = NeuralNet(layers=[('input', InputLayer),
                            ('hidden0', DenseLayer),
                            ('hidden1', DenseLayer),
                            ('output', DenseLayer)],
                    input_shape=(None, x_train.shape[1]),
                    hidden0_num_units=NODES,
                    hidden0_nonlinearity=nonlinearities.softmax,
                    hidden1_num_units=NODES,
                    hidden1_nonlinearity=nonlinearities.softmax,
                    output_num_units=len(np.unique(y_train)),
                    output_nonlinearity=nonlinearities.softmax,
                    update_learning_rate=0.1,
                    verbose=1,
                    max_epochs=100)

    param_grid = {'hidden0_num_units': [1, 4, 17, 25],
                  'hidden0_nonlinearity': 
                  [nonlinearities.sigmoid, nonlinearities.softmax],
                  'hidden1_num_units': [1, 4, 17, 25],
                  'hidden1_nonlinearity': 
                  [nonlinearities.sigmoid, nonlinearities.softmax],
                  'update_learning_rate': [0.01, 0.1, 0.5]}
    grid = grid_search.GridSearchCV(net, param_grid, verbose=0,
                                    n_jobs=3, cv=3)
    grid.fit(x_train, y_train)

    y_pred = grid.predict(x_val)
    # Accuracy prediction score
    accuracy = metrics.accuracy_score(y_val, y_pred)

    with open(NN_PICKLE, 'wb') as results:
        pickle.dump(grid, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(net, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(accuracy, results, pickle.HIGHEST_PROTOCOL)


def build_svm(x_train, y_train, x_val, y_val):
    """
    Construct a support vector regression model from input dataframe

    :param x_train: features dataframe for model training
    :param y_train: target dataframe for model training
    :param x_val: features dataframe for model validation
    :param y_val: target dataframe for model validation
    :return: None
    """

    clf = svm.SVR()
    clf.fit(x_train, y_train)
    y_pred = clf.predict(x_val)

    # Mean absolute error regression loss
    mean_abs = metrics.mean_absolute_error(y_val, y_pred)
    # Mean squared error regression loss
    mean_sq = metrics.mean_squared_error(y_val, y_pred)
    # Median absolute error regression loss
    median_abs = metrics.median_absolute_error(y_val, y_pred)
    # R^2 (coefficient of determination) regression score function
    r2 = metrics.r2_score(y_val, y_pred)
    # Explained variance regression score function
    exp_var_score = metrics.explained_variance_score(y_val, y_pred)

    with open(SVM_PICKLE, 'wb') as results:
        pickle.dump(mean_abs, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(mean_sq, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(median_abs, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(r2, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(exp_var_score, results, pickle.HIGHEST_PROTOCOL)


def build_tree(x_train, y_train, x_val, y_val):
    """
    Construct a decision trees regression model from input dataframe

    :param x_train: features dataframe for model training
    :param y_train: target dataframe for model training
    :param x_val: features dataframe for model validation
    :param y_val: target dataframe for model validation
    :return: None
    """
    clf = tree.DecisionTreeRegressor()
    clf.fit(x_train, y_train)
    y_pred = clf.predict(x_val)

    # Mean absolute error regression loss
    mean_abs = metrics.mean_absolute_error(y_val, y_pred)
    # Mean squared error regression loss
    mean_sq = metrics.mean_squared_error(y_val, y_pred)
    # Median absolute error regression loss
    median_abs = metrics.median_absolute_error(y_val, y_pred)
    # R^2 (coefficient of determination) regression score function
    r2 = metrics.r2_score(y_val, y_pred)
    # Explained variance regression score function
    exp_var_score = metrics.explained_variance_score(y_val, y_pred)

    with open(DT_PICKLE, 'wb') as results:
        pickle.dump(mean_abs, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(mean_sq, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(median_abs, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(r2, results, pickle.HIGHEST_PROTOCOL)
        pickle.dump(exp_var_score, results, pickle.HIGHEST_PROTOCOL)
{'fraction_non_alphanumeric': 0.08435647756138866, 'fraction_numerical': 0.010160880609652836, 'mean_word_length': 2.955211385516953, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3155438', 'n_tokens_mistral': 3071, 'n_tokens_neox': 2929, 'n_words': 707}