-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
379 lines (302 loc) · 11.7 KB
/
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import unittest
import numpy as np
import pandas as pd
from numpy.testing import assert_array_equal
from pandas.testing import assert_frame_equal, assert_series_equal
from functions import cov_series, create_cov_matrix, _eigenvalue, get_eigenvalue, get_pca, sub_series, \
correlation_series, correlation_frame, gini_series, weighted_average_of_impurity, get_possibility_series, \
entropy_series, classification_error_series
class TestSubSeries(unittest.TestCase):
def test_empty(self):
sr = pd.Series(dtype='float64')
exp = sr
assert_series_equal(exp, sub_series(sr), check_dtype=False)
def test1(self):
sr = pd.Series([3, 4, 1, 2, 0]) # mean = 2
exp = pd.Series([1, 2, -1, 0, -2])
assert_series_equal(exp, sub_series(sr), check_dtype=False)
class TestCvoSeries(unittest.TestCase):
def test_empty(self):
sr1 = sr2 = pd.Series(dtype='float64')
exp = 0
self.assertEqual(exp, cov_series(sr1, sr2))
def test_raise_def_length(self):
sr1 = pd.Series([1, 2, 3])
sr2 = pd.Series([1, 2])
try:
cov_series(sr1, sr2)
self.fail()
except ValueError:
self.assertEqual(True, True)
def test1(self):
sr1 = pd.Series([2, 4]) # -1, 1
sr2 = pd.Series([4, 2]) # 1, -1
exp = -1
self.assertEqual(exp, cov_series(sr1, sr2))
def test2(self):
sr1 = pd.Series([0]) # -1, 1
sr2 = pd.Series([0]) # 1, -1
exp = 0
self.assertEqual(exp, cov_series(sr1, sr2))
def test3(self):
sr1 = pd.Series([3, 4, 1, 2, 0]) # 1, 2, -1, 0, -2
sr2 = pd.Series([3, 4, 1, 2, 0]) # 1, 2, -1, 0, -2
exp = 2
self.assertEqual(exp, cov_series(sr1, sr2))
def test4(self):
sr1 = pd.Series([3, 4, 1, 2, 0]) # 1, 2, -1, 0, -2
sr2 = pd.Series([1, 3, 0, 4, 2]) # -1, 1, -2, 2, 0
exp = 3 / 5
self.assertEqual(exp, cov_series(sr1, sr2))
def test5(self):
sr1 = pd.Series([3, 4, 1, 2, 0]) # 1, 2, -1, 0, -2
sr2 = pd.Series([5, -4, 10, -11, 5]) # 4, -5, 9, -12, 4
exp = -23 / 5
self.assertEqual(exp, cov_series(sr1, sr2))
def test6(self):
sr1 = pd.Series([1, 3, 0, 4, 2]) # -1, 1, -2, 2, 0
sr2 = pd.Series([5, -4, 10, -11, 5]) # 4, -5, 9, -12, 4
exp = -51 / 5
self.assertEqual(exp, cov_series(sr1, sr2))
def test7(self):
sr1 = pd.Series([5, -4, 10, -11, 5]) # 4, -5, 9, -12, 4
sr2 = pd.Series([5, -4, 10, -11, 5]) # 4, -5, 9, -12, 4
exp = 282 / 5
self.assertEqual(exp, cov_series(sr1, sr2))
class TestCreateCovMatrix(unittest.TestCase):
def test_empty(self):
df = pd.DataFrame()
exp = pd.DataFrame()
assert_frame_equal(exp, create_cov_matrix(df), check_dtype=False)
def test1(self):
df = pd.DataFrame({'1': [3, 4, 1, 2, 0], '2': [3, 4, 1, 2, 0]})
exp = pd.DataFrame({'1': [2, 2], '2': [2, 2]}, index=['1', '2'])
assert_frame_equal(exp, create_cov_matrix(df), check_dtype=False, check_index_type=False)
def test2(self):
df = pd.DataFrame({'1': [3, 4, 1, 2, 0], '2': [1, 3, 0, 4, 2], '3': [5, -4, 10, -11, 5]})
exp = pd.DataFrame({'1': [2, 3 / 5, -23 / 5], '2': [3 / 5, 2, -51 / 5], '3': [-23 / 5, -51 / 5, 282 / 5]},
index=['1', '2', '3'])
assert_frame_equal(exp, create_cov_matrix(df), check_dtype=False, check_index_type=False)
def test3(self):
df = pd.DataFrame({'1': [3, 4, 1, 2, 0], '2': [1, 3, 0, 4, 2]})
print(df)
exp = pd.DataFrame({'1': [2, 3 / 5], '2': [3 / 5, 2]},
index=['1', '2'])
assert_frame_equal(exp, create_cov_matrix(df), check_dtype=False, check_index_type=False)
class TestTempEigenvalue(unittest.TestCase):
def test1(self):
df = pd.DataFrame({'1': [2, 0], '2': [0, 1]},
index=['1', '2'])
w, v = _eigenvalue(df)
expw = np.array([2, 1])
expv = np.array([[1, 0], [0, 1]])
assert_array_equal(expw, w)
assert_array_equal(expv, v)
def test2(self):
df = pd.DataFrame({'1': [2, 0], '2': [0, 2]},
index=['1', '2'])
w, v = _eigenvalue(df)
expw = np.array([2, 2])
expv = np.array([[0, 1], [1, 0]])
assert_array_equal(expw, w)
assert_array_equal(expv, v)
def test3(self):
df = pd.DataFrame({'1': [2, 0, 0], '2': [0, 2, 0], '3': [0, 0, 2]},
index=['1', '2', '3'])
w, v = _eigenvalue(df)
expw = np.array([2, 2, 2])
expv = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]])
assert_array_equal(expw, w)
assert_array_equal(expv, v)
def test4(self):
df = pd.DataFrame({'1': [3, 0, 0], '2': [0, 4, 0], '3': [0, 0, 0]},
index=['1', '2', '3'])
w, v = _eigenvalue(df)
expw = np.array([4, 3, 0])
expv = np.array([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
assert_array_equal(expw, w)
assert_array_equal(expv, v)
class TestEigenvalue(unittest.TestCase):
def test1(self):
df = pd.DataFrame({'1': [3, 4, 1, 2, 0], '2': [3, 4, 1, 2, 0]})
w, v = get_eigenvalue(df)
expw = np.array([4, 0])
sqr2inv = 1 / np.sqrt(2)
expv = np.array([[sqr2inv, -sqr2inv], [sqr2inv, sqr2inv]])
assert_array_equal(expw, w)
assert_array_equal(expv, v)
class TestPCA(unittest.TestCase):
def test1(self):
l = [3, 4, 1, 2, 0]
df = pd.DataFrame({'1': l, '2': l})
sqr2 = np.sqrt(2)
exp = pd.DataFrame([[sqr2 * x] for x in l])
assert_frame_equal(exp, get_pca(df), check_column_type=False)
class TestCorrelationSeries(unittest.TestCase):
def test_empty(self):
sr2 = sr1 = pd.Series(dtype='float64')
exp = 0
self.assertEqual(exp, correlation_series(sr1, sr2))
def test_raise_length_error(self):
x = pd.Series([3, 4, 2, 0])
y = pd.Series([1, 3, 0, 4, 2])
try:
correlation_series(x, y)
self.fail()
except ValueError:
self.assertEqual(True, True)
def test1(self):
x = pd.Series([3, 4, 1, 2, 0]) # 1, 2, -1, 0, -2
y = pd.Series([1, 3, 0, 4, 2]) # -1, 1, -2, 2, 0
# xy = 3
# xx = 10
# yy = 10
exp = 3 / 10
self.assertEqual(exp, correlation_series(x, y))
def test2(self):
x = pd.Series([13, 14, 35, 32, 4, 63, 234, 1])
y = pd.Series([1, 3, 0, 4, 2, 2, 42, 2])
exp = 0.961177121
np.testing.assert_almost_equal(exp, correlation_series(x, y))
class TestCorrelationFrame(unittest.TestCase):
def test_empty(self):
df = pd.DataFrame()
exp = df
assert_frame_equal(exp, correlation_frame(df))
def test1(self):
x = pd.Series([3, 4, 1, 2, 0]) # 1, 2, -1, 0, -2
y = pd.Series([1, 3, 0, 4, 2]) # -1, 1, -2, 2, 0
df = pd.DataFrame({'x': x, 'y': y})
exp = pd.DataFrame([[1, 0.3], [0.3, 1]], columns=['x', 'y'], index=['x', 'y'])
assert_frame_equal(exp, correlation_frame(df))
def test2(self):
x = pd.Series([13, 14, 35, 32, 4, 63, 234, 1])
y = pd.Series([1, 3, 0, 4, 2, 2, 42, 2])
df = pd.DataFrame({'x': x, 'y': y})
e = 0.961177121410483
exp = pd.DataFrame([[1, e], [e, 1]], columns=['x', 'y'], index=['x', 'y'])
assert_frame_equal(exp, correlation_frame(df))
class TestGetPossibilitySeries(unittest.TestCase):
def test_empty(self):
sr = pd.Series(dtype='float64')
exp = sr
assert_series_equal(exp, get_possibility_series(sr))
def test1(self):
sr = pd.Series([10, 0])
exp = pd.Series([1, 0])
assert_series_equal(exp, get_possibility_series(sr), check_dtype=False)
def test2(self):
sr = pd.Series([5, 5])
exp = pd.Series([1 / 2, 1 / 2])
assert_series_equal(exp, get_possibility_series(sr), check_dtype=False)
def test3(self):
sr = pd.Series([6, 4, 2])
exp = pd.Series([1 / 2, 1 / 3, 1 / 6])
assert_series_equal(exp, get_possibility_series(sr), check_dtype=False)
class TestGiniSeries(unittest.TestCase):
def test_empty(self):
sr = pd.Series(dtype='float64')
exp = 1
self.assertEqual(exp, gini_series(sr))
def test1(self):
sr = pd.Series([0, 0, 10])
exp = 0
np.testing.assert_almost_equal(exp, gini_series(sr))
def test2(self):
sr = pd.Series([3, 3, 3])
exp = 2 / 3
np.testing.assert_almost_equal(exp, gini_series(sr))
def test3(self):
sr = pd.Series([5, 4, 3])
exp = 94 / 144
np.testing.assert_almost_equal(exp, gini_series(sr))
def test4(self):
sr = pd.Series([10, 0])
exp = 0
np.testing.assert_almost_equal(exp, gini_series(sr))
def test5(self):
sr = pd.Series([1, 15])
exp = 15 / 128
np.testing.assert_almost_equal(exp, gini_series(sr))
def test6(self):
sr = pd.Series([5, 4])
exp = 40 / 81
np.testing.assert_almost_equal(exp, gini_series(sr))
def test7(self):
sr = pd.Series([5, 5])
exp = 1 / 2
np.testing.assert_almost_equal(exp, gini_series(sr))
class TestGiniFrame(unittest.TestCase):
def test_empty(self):
df = pd.DataFrame()
exp = 0
self.assertEqual(exp, weighted_average_of_impurity(df, gini_series))
def test1(self):
df = pd.DataFrame([
[5, 4, 3],
[0, 0, 10],
[3, 3, 3]
])
exp = 83 / 186
np.testing.assert_almost_equal(exp, weighted_average_of_impurity(df, gini_series))
def test2(self):
df = pd.DataFrame([
[10, 0],
[1, 15],
[5, 4],
[5, 5]
])
exp = 163 / 648
np.testing.assert_almost_equal(exp, weighted_average_of_impurity(df, gini_series))
class TestEntropySeries(unittest.TestCase):
def test_empty(self):
sr = pd.Series(dtype='float64')
exp = 0
self.assertEqual(exp, entropy_series(sr))
def test1(self):
sr = pd.Series([0, 0, 10])
exp = 0
np.testing.assert_almost_equal(exp, entropy_series(sr))
def test2(self):
sr = pd.Series([3, 3, 3])
exp = 1
np.testing.assert_almost_equal(exp, entropy_series(sr))
def test3(self):
sr = pd.Series([10, 0])
exp = 0
np.testing.assert_almost_equal(exp, entropy_series(sr))
def test4(self):
sr = pd.Series([5, 9])
exp = 0.9402859
np.testing.assert_almost_equal(exp, entropy_series(sr))
def test5(self):
sr = pd.Series([5, 5])
exp = 1
np.testing.assert_almost_equal(exp, entropy_series(sr))
class TestClassificationErrorSeries(unittest.TestCase):
def test_empty(self):
sr = pd.Series(dtype='float64')
exp = 0
self.assertEqual(exp, entropy_series(sr))
def test1(self):
sr = pd.Series([0, 0, 10])
exp = 0
np.testing.assert_almost_equal(exp, classification_error_series(sr))
def test2(self):
sr = pd.Series([3, 3, 3])
exp = 2 / 3
np.testing.assert_almost_equal(exp, classification_error_series(sr))
def test3(self):
sr = pd.Series([10, 0])
exp = 0
np.testing.assert_almost_equal(exp, classification_error_series(sr))
def test4(self):
sr = pd.Series([5, 9])
exp = 5 / 14
np.testing.assert_almost_equal(exp, classification_error_series(sr))
def test5(self):
sr = pd.Series([5, 5])
exp = 1/2
np.testing.assert_almost_equal(exp, classification_error_series(sr))
if __name__ == '__main__':
unittest.main()