Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F120413862
helpers.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Jul 4, 05:14
Size
1 KB
Mime Type
text/x-python
Expires
Sun, Jul 6, 05:14 (2 d)
Engine
blob
Format
Raw Data
Handle
27185064
Attached To
rTZUCT ML_Project1
helpers.py
View Options
# -*- coding: utf-8 -*-
"""some helper functions."""
import
numpy
as
np
def
load_data
():
"""load data."""
data
=
np
.
loadtxt
(
"dataEx3.csv"
,
delimiter
=
","
,
skiprows
=
1
,
unpack
=
True
)
x
=
data
[
0
]
y
=
data
[
1
]
return
x
,
y
def
load_data_from_ex02
(
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
b
"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
)
x
=
x
-
mean_x
std_x
=
np
.
std
(
x
)
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
Event Timeline
Log In to Comment