-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathhelpers.py
49 lines (39 loc) · 1.28 KB
/
helpers.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
# -*- coding: utf-8 -*-
"""some helper functions."""
import numpy as np
def load_data(sub_sample=True, add_outlier=False):
"""Load data and convert it to the metric system."""
path_dataset = "height_weight_genders.csv"
data = np.genfromtxt(
path_dataset, delimiter=",", skip_header=1, usecols=[1, 2])
height = data[:, 0]
weight = data[:, 1]
gender = np.genfromtxt(
path_dataset, delimiter=",", skip_header=1, usecols=[0],
converters={0: lambda x: 0 if "Male" in x else 1})
# Convert to metric system
height *= 0.025
weight *= 0.454
# sub-sample
if sub_sample:
height = height[::50]
weight = weight[::50]
if add_outlier:
# outlier experiment
height = np.concatenate([height, [1.1, 1.2]])
weight = np.concatenate([weight, [51.5/0.454, 55.3/0.454]])
return height, weight, gender
def standardize(x):
"""Standardize the original data set."""
mean_x = np.mean(x,axis = 0)
x = x - mean_x
std_x = np.std(x, axis = 0)
x = x / std_x
return x, mean_x, std_x
def build_model_data(height, weight):
"""Form (y,tX) to get regression data in matrix form."""
y = weight
x = height
num_samples = len(y)
tx = np.c_[np.ones(num_samples), x]
return y, tx