Mercurial > pylearn
annotate _test_linear_regression.py @ 524:317a052f9b14
better main, allow to debug in a debugger.
author | Frederic Bastien <bastienf@iro.umontreal.ca> |
---|---|
date | Fri, 14 Nov 2008 16:46:03 -0500 |
parents | 8e4d2ebd816a |
children |
rev | line source |
---|---|
432
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
1 |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
2 import unittest |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
3 from linear_regression import * |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
4 from make_test_datasets import * |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
5 import numpy |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
6 |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
7 class test_linear_regression(unittest.TestCase): |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
8 |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
9 def test1(self): |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
10 trainset,testset,theta=make_artificial_datasets_from_function(n_inputs=3, |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
11 n_targets=2, |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
12 n_examples=100, |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
13 f=linear_predictor) |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
14 |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
15 assert trainset.fields()['input'].shape==(50,3) |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
16 assert testset.fields()['target'].shape==(50,2) |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
17 regressor = LinearRegression(L2_regularizer=0.1) |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
18 predictor = regressor(trainset) |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
19 test_data = testset.fields() |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
20 mse = predictor.compute_mse(test_data['input'],test_data['target']) |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
21 print 'mse = ',mse |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
22 |
8e4d2ebd816a
added a test for LinearRegression
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
diff
changeset
|
23 if __name__ == '__main__': |
524
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
24 import sys |
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
25 |
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
26 if len(sys.argv)==1: |
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
27 unittest.main() |
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
28 else: |
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
29 assert sys.argv[1]=="--debug" |
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
30 tests = [] |
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
31 for arg in sys.argv[2:]: |
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
32 tests.append(arg) |
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
33 if tests: |
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
34 unittest.TestSuite(map(T_DataSet, tests)).debug() |
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
35 else: |
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
36 module = __import__("_test_linear_regression") |
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
37 tests = unittest.TestLoader().loadTestsFromModule(module) |
317a052f9b14
better main, allow to debug in a debugger.
Frederic Bastien <bastienf@iro.umontreal.ca>
parents:
432
diff
changeset
|
38 tests.debug() |