train_evaluate
Description
training the models on the training set and predict the target variable values for the training and validation set.
Usage
- predict.train_evaluate(training_data, validation_data, model, model_type, model_parameters=None, labels=None, base_models=None, verbose=0)
Parameters
# |
Input Name |
Input Description |
|---|---|---|
1
|
training_data
|
type: dataframe or string
default: -
details: a data frame or address of a data frame containing data
which is used to train the models. This data frame must have a column
name format conforming to Fig. 5.
example: ‘my_directory/my_data.csv’
|
2
|
validation_data
|
type: dataframe or string
default: -
details: a data frame or address of a data frame, containing the
data that is held out to evaluate models’ performance. This data frame
must have a column name format conforming to Fig. 5.
example: ‘my_directory/my_data.csv’
|
3
|
model
|
type: {‘knn’, ‘gbm’, ‘glm’, ‘nn’} or callable
default: ‘knn’
details: a model to be trained using training data and predict the
target variable values.
‘knn’: K nearest neighbour regressor for model_type of regression, K
nearest neighbour classifier for model_type of classification.
‘gbm’: Gradient boosting regression for model_type of regression,
Gradient boosting classification for model_type of classification.
‘glm’: Elastic net linear regression for model_type of regression,
logistic regression for model_type of classification.
‘nn’: Neural network with adjustable number of layers and neurons for
model_type of regression and classification.
‘sgcrf’: Sparce gaussian conditional random field (See sgcrfpy) for
model_type of regression and classification.
[callable]: a user-defined function that accepts the data frames of
features in the training and validation set and a target variable
values of the training set in a form of ndarray, and returns the
predictions on the training and validation set and the trained model
(See User defined model).
example: ‘nn’ or lstm()
|
4
|
model_type
|
type: {‘regression’,’classification}
default: ‘regression’
details: type of prediction task.
|
5
|
model_parameters
|
type: dict or None
default: None
details: For the predefined models the model hyper parameters can
be passed to the function using this argument and in a format of a
dictionary.
Since for ‘knn’, ‘glm’, and ‘gbm’ we use the implementation of the
Scikit Learn package, all of the hyperparameters defined in this
package are supported. For the ‘nn’ model the implementation of
TensorFlow is used, and the list of supported hyperparameters is as
below:
[‘hidden_layers_structure’, ‘hidden_layers_number’,
‘hidden_layers_neurons’, ‘hidden_layers_activations’,
‘output_activation’, ‘loss’, ‘optimizer’, ‘metrics’,
‘early_stopping_monitor’, ‘early_stopping_patience’, ‘batch_size’,
‘validation_split’,’epochs’]
The structure of the neural network can be specified using
‘hidden_layers_structure’ which takes a list of two item tuples each
corresponds to a hidden layer in a network with the first item of each
tuple as the number of neurons in the layer and second item as the
activation function of the layer.
Another way is to specify ‘hidden_layers_number’,
‘hidden_layers_neurons’, and ‘hidden_layers_activations’ which will
result in network with the number of hidden layers according to
‘hidden_layers_number’ having the same activation function and number
of neurons according to the values of ‘hidden_layers_neurons’ and
‘hidden_layers_activations’ parameters. The ‘output_activation’ hyper
parameter is the activation function of the output layer. The
supported values for the rest of the hyperparameters are the same as
the TensorFlow package.
example: {‘n_neighbors’:10, ‘metric’:’minkowski’}
{‘hidden_layers_neurons’:[2,4,8],
‘hidden_layers_activations’:[‘relu’,None,’exponential’]}
|
6
|
labels
|
type: list<any type> or None
default: None
details: The class labels of the target variable.
|
7
|
base_models
|
type: list<string, dict, or function> or None
default: None
details: a list of predefined model names and user-defined model
functions which will be considered as base models to form a mixed
model by taking their predictions as the inputs of the main model.
If None is passed, the model uses the input data features as the
predictors.
The supported options are the same as the model input. If the user
prefers to specify the hyperparameters of the predefined model, the
related item in the list must be in a form of a dictionary with the
name of the model as the key and the dictionary of hyperparameters as
its value. The specified hyper parameters for each model must conform
to the description of model_parameters input.
example: [{‘knn’:{‘n_neighbors’:10, ‘metric’:’minkowski’}}, ‘nn’,
lstm()]
|
8
|
verbose
|
type: int
default: 0
details: The level of details in produced logging information
available options
0: no logging
1: only important information logging
2: all details logging
|
Returns
# |
Output Name |
Output Description |
|---|---|---|
1
|
train_predictions
|
type: list<float> or list<int>
details: predictions on training_data which is of type float if
model_type is regression, and int if model_type is classification
|
2
|
validation_predictions
|
type: list<float> or list<int>
details: predictions on validation_data which is of type float
if model_type is regression, and int if model_type is
classification
|
3
|
trained_model
|
type: object
details: the trained model on the training_data
|
Example
import pandas as pd
from stpredict.predict import train_evaluate
df = pd.read_csv('./historical_data h=1.csv')
training_df = df.iloc[:-200]
validation_df = df.iloc[-200:]
train_predictions, validation_predictions, trained_model = train_evaluate(
training_data = training_df, validation_data = validation_df, model = 'nn',
model_parameters = {'hidden_layers_structure':[(2,None),(4,'relu'),(8,'relu')]})