-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain_with_data_example_regression.py
64 lines (50 loc) · 2.29 KB
/
train_with_data_example_regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Author : Abdullah Al Masud\n
email : [email protected]\n
LICENSE : MIT License
"""
from sklearn.ensemble import RandomForestRegressor as RFR
from sklearn.tree import DecisionTreeRegressor as DTR
import pandas as pd
from sklearn.datasets import fetch_california_housing
import torch
import os
import sys
project_dir = os.getcwd()
sys.path.append(project_dir)
from msdlib import mlutils
from msdlib import msd
savepath = 'examples/train_with_data_regression'
# Loading the data and separating data and label
source_data = fetch_california_housing()
feature_names = source_data['feature_names'].copy()
data = pd.DataFrame(source_data['data'], columns=feature_names)
label = pd.Series(source_data['target'], name=source_data['target_names'][0])
# print(source_data['DESCR'])
print('data:\n', data.head())
# Standardizing numerical data
data = msd.standardize(data)
# Splitting data set into train, validation and test
splitter = msd.SplitDataset(data, label, test_ratio=.1)
outdata = splitter.random_split(val_ratio=.1)
print('outdata.keys() :', outdata.keys())
print("outdata['train'].keys() :", outdata['train'].keys())
print("outdata['validation'].keys() :", outdata['validation'].keys())
print("outdata['test'].keys() :", outdata['test'].keys())
print('train > data, labels and index shapes :',
outdata['train']['data'].shape, outdata['train']['label'].shape, outdata['train']['index'].shape)
# defining layers inside a list
layers = mlutils.define_layers(data.shape[1], 1, [100, 100, 100, 100, 100, 100], dropout_rate=.2, model_type='regressor',
actual_units=True, activation=torch.nn.ReLU())
# building model
tmodel = mlutils.torchModel(layers=layers, model_type='regressor',
savepath=savepath, epoch=30, learning_rate=.0001, lr_reduce=.995)
models = {
'RFR': RFR(),
'DTR': DTR(),
'pytorch-DNN': tmodel
}
models, predictions = mlutils.train_with_data(outdata, feature_names, models, featimp_models=['RFR', 'DTR'],
figure_dir=savepath, model_type='regressor', evaluate=True)
assert predictions['RFR']['test']['rsquare'] >= .8, 'RFR model test set R-square is less than 0.8'
assert predictions['pytorch-DNN']['test']['rsquare'] >= .7, 'pytorch-DNN model test set R-square is less than .7'