-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
67 lines (48 loc) · 1.94 KB
/
util.py
File metadata and controls
67 lines (48 loc) · 1.94 KB
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
"""Utility functions."""
import os
import pandas as pd
import numpy as np
ISL_URL = 'http://www-bcf.usc.edu/~gareth/ISL'
ALT_URL = 'https://raw.githubusercontent.com/selva86/datasets/master'
DATA_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data'))
def get_data(filename: str, reload: bool = False) -> pd.DataFrame:
"""Get data from ISL website and store locally for future."""
from urllib.request import urlretrieve, urlopen
src_path = os.path.join(ISL_URL, filename)
try:
urlopen(src_path)
except:
src_path = os.path.join(ALT_URL, filename)
dst_path = os.path.join(DATA_PATH, filename)
# Download file.
if not os.path.exists(dst_path) or reload:
urlretrieve(src_path, dst_path)
df = pd.read_csv(dst_path)
return df
def make_mesh(X:np.ndarray, step_size:float=0.1, dim:int=2, return_2d=False) -> np.ndarray:
"""Make mesh grid that covers the range of input features X.
A meshgrid is the coordinates of all the points in a grid
arranged in a grid form.
Example:
x_mesh, (X0, X1) = make_mesh(X)
plt.pcolormesh(X0, X1, x_mesh.mean(axis=-1))
Args:
X : feature matrix [samples, features]
step_size : step size along single dimension of the grid
dim : first n dimensions to make grid from
Return:
if return_2d:
x_mesh : [samples*dim, dim]
else:
x_mesh : [samples along X_0, ..., samples along X_i, dim]
x_samples : list of length = dim
"""
x_min = np.min(X, axis=0)
x_max = np.max(X, axis=0)
x_samples = [np.arange(mi-step_size, ma+step_size, step_size)
for mi, ma in zip(x_min, x_max)]
x_mesh = np.stack(np.meshgrid(*x_samples[:dim]), axis=-1)
if return_2d:
return x_mesh.reshape(-1, dim), x_samples[:dim]
else:
return x_mesh, x_samples[:dim]