-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_test.py
98 lines (65 loc) · 2.58 KB
/
unit_test.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# /usr/bin/python
__author__ = 'vignesh'
import read_document
import simple_regression
import pca
def test_linear_regression():
train_doc = read_document.Document('resources/train.csv')
trainx = train_doc.docx
trainy = train_doc.docy
test_doc = read_document.Document('resources/test.csv')
testx = test_doc.docx
sample_doc = read_document.Document('resources/sampleSubmission.csv')
sample = sample_doc.docx
# Get only the numeric columns in traindata
train_x_numeric = train_doc.get_only_numeric(trainx)
test_x_numeric = train_doc.get_only_numeric(testx)
# Perform training
lm = simple_regression.LinearRegression(train_x_numeric,trainy, test_x_numeric, False)
lm.select_features(5) # Performing Feature Selection
lm.build_regression_model()
predicted = lm.predict()
sample['target'] = predicted
sample.to_csv('submission.csv',index=False)
print predicted
def test_pca_linear_regression():
print 'Reading training data'
print '======================='
train_doc = read_document.Document('resources/train.csv')
# Fixing missing values
print 'Fixing Missing Values'
print '======================='
# The missing values fixing is done in the initialization itself
# Getting the training and test data
trainx = train_doc.docx
trainy = train_doc.docy
print 'Reading Test data '
print '====================='
test_doc = read_document.Document('resources/test.csv')
testx = test_doc.docx
sample_doc = read_document.Document('resources/sampleSubmission.csv')
sample = sample_doc.docx
# Get only the numeric columns in traindata
train_x_numeric = train_doc.get_only_numeric(trainx)
test_x_numeric = train_doc.get_only_numeric(testx)
# Performing PCA
print 'Performing PCA'
print '======================='
model = pca.PCAMODEL(15, train_x_numeric, trainy)
model.build_model()
# Transforming the data set to PCA diamensions
train_pca_x = model.reduce_dim(train_x_numeric)
test_pca_x = model.reduce_dim(test_x_numeric)
# Perform simple linear regression using this data
print 'Building Regression Model'
print '============================='
lm = simple_regression.LinearRegression(train_pca_x, trainy, test_pca_x, False)
lm.set_train_selected(train_pca_x)
lm.set_test_selected(test_pca_x)
lm.build_regression_model()
predicted = lm.predict()
sample['target'] = predicted
sample.to_csv('submission_pca.csv',index=False)
print predicted
test_linear_regression()
# test_pca_linear_regression()