1
|
|
|
"""A small personal package created to store code and data I often reuse. |
|
|
|
|
2
|
|
|
|
3
|
|
|
I'll continue to update it with useful functions that I find myself reusing. The `apoor.data` module has some common datasets and functions for reading them in as pandas DataFrames. |
|
|
|
|
4
|
|
|
""" |
5
|
|
|
|
6
|
|
|
# Version string |
7
|
|
|
__version__ = "1.1.3" |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
import itertools as it |
12
|
|
|
import numpy as np |
13
|
|
|
from . import data |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
def fdir(o=None): |
|
|
|
|
18
|
|
|
"""Same as builtin dir() without private attributes. |
|
|
|
|
19
|
|
|
""" |
20
|
|
|
if o is None: d = dir() |
|
|
|
|
21
|
|
|
else: d = dir(o) |
|
|
|
|
22
|
|
|
return [a for a in d if a[0] != "_"] |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
25
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def set_seed(n:int): |
|
|
|
|
29
|
|
|
"""Sets numpy's random seed. |
30
|
|
|
|
31
|
|
|
Args: |
32
|
|
|
n (int): The value used to set numpy's random seed. |
33
|
|
|
""" |
34
|
|
|
np.random.seed(n) |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def make_scale(dmin:float,dmax:float,rmin:float,rmax:float,clamp:bool=False): |
|
|
|
|
38
|
|
|
"""Scale function factory. |
39
|
|
|
|
|
|
|
|
40
|
|
|
Creates a scale function to map a number from a domain to a range. |
41
|
|
|
|
42
|
|
|
Args: |
43
|
|
|
dmin (float): Domain's start value |
44
|
|
|
dmax (float): Domain's end value |
45
|
|
|
rmin (float): Range's start value |
46
|
|
|
rmax (float): Range's end value |
47
|
|
|
clamp (bool): If the result is outside the range, return clamped value (default: False) |
48
|
|
|
Returns: |
49
|
|
|
A scale function taking one numeric argument and returns the value mapped from the domain to the range (and clamped if `clamp` flag is set). |
|
|
|
|
50
|
|
|
|
51
|
|
|
For example: |
52
|
|
|
|
|
|
|
|
53
|
|
|
>>> s = make_scale(0,1,0,10) |
54
|
|
|
>>> s(0.1) |
55
|
|
|
1.0 |
56
|
|
|
|
57
|
|
|
>>> s = make_scale(0,10,10,0) |
58
|
|
|
>>> s(1.0) |
59
|
|
|
9.0 |
60
|
|
|
|
61
|
|
|
>>> s = make_scale(0,1,0,1,clamp=True) |
62
|
|
|
>>> s(100) |
63
|
|
|
1.0 |
64
|
|
|
""" |
65
|
|
|
drange = dmax - dmin |
66
|
|
|
rrange = rmax - rmin |
67
|
|
|
scale_factor = rrange / drange |
68
|
|
|
def scale(n): |
|
|
|
|
69
|
|
|
n_ = (n - dmin) * scale_factor + rmin |
|
|
|
|
70
|
|
|
if clamp: return min(max(n_,rmin),rmax) |
|
|
|
|
71
|
|
|
else: return n_ |
|
|
|
|
72
|
|
|
return scale |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
75
|
|
|
def train_test_split(*arrays,test_pct:float=0.15,val_set:bool=False,val_pct:float=0.15): |
|
|
|
|
76
|
|
|
"""Splits arrays into train & test sets. |
77
|
|
|
|
78
|
|
|
Splits arrays into train, test, and (optionally) validation sets using the supplied percentages. |
79
|
|
|
|
80
|
|
|
Args: |
81
|
|
|
*arrays: |
|
|
|
|
82
|
|
|
An arbitrary number of sequences to be split |
83
|
|
|
into train, test, and (optionally) validation |
|
|
|
|
84
|
|
|
sets. Must have at least one array. |
85
|
|
|
|
86
|
|
|
test_pct: |
|
|
|
|
87
|
|
|
Float of the range [0,1] |
88
|
|
|
Percent of total n values to include in test set. |
89
|
|
|
|
|
|
|
|
90
|
|
|
The train set will have `1.0 - test_pct` pct of |
91
|
|
|
values (or `1.0 - test_pct - val_pct` pct of values |
92
|
|
|
if `val_set == True`). |
93
|
|
|
|
94
|
|
|
val_set: |
|
|
|
|
95
|
|
|
Whether or not to return a validation set, |
96
|
|
|
in addition to a test set. |
97
|
|
|
|
98
|
|
|
val_pct: |
|
|
|
|
99
|
|
|
float of the range [0,1] |
100
|
|
|
Percent of total n values to include in test set. |
101
|
|
|
|
|
|
|
|
102
|
|
|
Ignored if `val_set == False`. |
103
|
|
|
|
|
|
|
|
104
|
|
|
The train set will have `1.0 - test_pct - val_pct` |
|
|
|
|
105
|
|
|
pct of values. |
106
|
|
|
|
107
|
|
|
Returns: |
108
|
|
|
splits: tuple of numpy arrays |
109
|
|
|
Input arrays split into train, test, val sets. |
110
|
|
|
|
|
|
|
|
111
|
|
|
If `val_set == False`, `len(splits) == 2 * len(arrays)`, |
112
|
|
|
or if `val_set == True`, `len(splits) == 3 * len(arrays)`. |
113
|
|
|
|
114
|
|
|
For example: |
115
|
|
|
>>> x = np.arange(10) |
116
|
|
|
>>> train_test_split(x) |
117
|
|
|
(array([3, 9, 4, 2, 1, 0, 7, 5, 8]), array([6])) |
118
|
|
|
|
119
|
|
|
>>> x = np.arange(10) |
120
|
|
|
>>> y = x[::-1] |
121
|
|
|
>>> x_train, x_test, y_train, y_test = train_test_split(x,y) |
122
|
|
|
>>> x_train, x_test, y_train, y_test |
123
|
|
|
(array([1, 3, 5, 8, 4, 7, 6, 9]), |
124
|
|
|
array([0, 2]), |
125
|
|
|
array([8, 6, 4, 1, 5, 2, 3, 0]), |
126
|
|
|
array([9, 7])) |
127
|
|
|
|
128
|
|
|
>>> train_test_split(x,test_pct=0.3,val_set=True,val_pct=0.2) |
129
|
|
|
(array([0, 9, 5, 7, 6, 2, 8]), |
|
|
|
|
130
|
|
|
array([1, 3, 4]), |
|
|
|
|
131
|
|
|
array([3, 4])) |
132
|
|
|
|
133
|
|
|
""" |
134
|
|
|
# Perform input checks |
135
|
|
|
assert arrays, "No arrays supplied" |
136
|
|
|
lens = [len(a) for a in arrays] |
137
|
|
|
assert len(set(lens)) == 1, "arrays have varying lengths" |
138
|
|
|
assert lens[0] > 0, "supplied arrays have `len == 0`" |
139
|
|
|
if val_set: |
140
|
|
|
assert 0.0 <= test_pct <= 1.0, "`test_pct` must be in the range `0.0 <= test_pct <= 1.0`" |
141
|
|
|
assert 0.0 <= val_pct <= 1.0, "`val_pct` must be in the range `0.0 <= val_pct <= 1.0`" |
142
|
|
|
assert test_pct + val_pct <= 1.0, "Can't have `test_pc + val_pct >= 1.0`" |
143
|
|
|
else: |
144
|
|
|
assert 0.0 <= test_pct <= 1.0, "`test_pct` must be in the range `0.0 <= test_pct <= 1.0`" |
145
|
|
|
assert test_pct <= 1.0, "Can't have `test_pc >= 1.0`" |
146
|
|
|
# Calculate lengths |
147
|
|
|
n = lens[0] |
|
|
|
|
148
|
|
|
n_test = int(n * test_pct) |
149
|
|
|
# Shuffle the indexes |
150
|
|
|
indexes = np.arange(n) |
151
|
|
|
np.random.shuffle(indexes) |
152
|
|
|
# Split the data |
153
|
|
|
if val_set: |
154
|
|
|
n_val = int(n * val_pct) |
155
|
|
|
n_train = n - n_test - n_val |
156
|
|
|
splits = ( |
157
|
|
|
( |
158
|
|
|
a[indexes[:n_train]], |
|
|
|
|
159
|
|
|
a[indexes[n_train:n_train+n_test]], |
|
|
|
|
160
|
|
|
a[indexes[-n_val:]] |
161
|
|
|
) |
162
|
|
|
for a in map(np.asarray,arrays) |
|
|
|
|
163
|
|
|
) |
164
|
|
|
else: |
165
|
|
|
n_train = n - n_test |
166
|
|
|
splits = ( |
167
|
|
|
(a[indexes[:n_train]], a[indexes[n_train:]]) |
168
|
|
|
for a in map(np.asarray,arrays) |
|
|
|
|
169
|
|
|
) |
170
|
|
|
return tuple(it.chain(*splits)) |
171
|
|
|
|
|
|
|
|
172
|
|
|
|