forked from ML-Society/MLSoc-Weekly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
65 lines (51 loc) · 1.9 KB
/
data.py
File metadata and controls
65 lines (51 loc) · 1.9 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
# Provides helper for loading the data to np
# array
import numpy as np
import os
from urllib.request import urlopen
from sklearn.model_selection import train_test_split
data_uri = "https://raw.githubusercontent.com/jadeyee/r2d3-part-1-data/master/part_1_data.csv"
def _load_data():
"""
Returns data in np array sorted as:
in_sf,beds,bath,price,year_built,sqft,price_per_sqft,elevation
"""
res = ""
if os.path.isfile("./.data"):
with open("./.data", "r") as f:
res = f.read()
else:
u = urlopen(data_uri)
res = u.read().decode(u.headers.get_content_charset())
# Make local backup
with open("./.data", "w") as f:
f.write(res)
return np.loadtxt("./.data", delimiter=",", comments="#", skiprows=3, unpack=True)
def load_train_data():
"""
Returns X_train, y_train with 8:2 split and random_state=0
"""
s_sf, beds, bath, price, year_built, sqft, price_per_sqft, elevation = _load_data()
X = np.column_stack((beds, bath, price, year_built, sqft, price_per_sqft, elevation))
y = s_sf
X_train, _, y_train, _ = train_test_split(X, y, test_size=0.2, random_state=0)
return X_train, y_train
def _load_test_data():
"""
Returns X_test, y_test with 8:2 split and random_state=0
"""
s_sf, beds, bath, price, year_built, sqft, price_per_sqft, elevation = _load_data()
X = np.column_stack((beds, bath, price, year_built, sqft, price_per_sqft, elevation))
y = s_sf
_, X_test, _, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
return X_test, y_test
def grade(fn):
"""
Grade a function, which classifies the data. The function is
passed:
np.column_stack((beds,bath,price,year_built,sqft,price_per_sqft,elevation))
"""
X, y = _load_test_data()
pred = fn(X)
target = y
return 1 - (np.sum(np.abs(pred-target)) / len(target))