-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaling.py
29 lines (23 loc) · 943 Bytes
/
scaling.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
from linear_algebra import shape, get_column, make_matrix
from standard_math import mean
from statistics import standard_deviation
def scale(data_matrix):
"""returns the mean and standard deviation of each column"""
num_rows, num_cols = shape(data_matrix)
means = [mean(get_column(data_matrix,j)) for j in range(num_cols)]
stdevs = [standard_deviation(get_column(data_matrix, j))
for j in range(num_cols)]
return means, stdevs
def rescale(data_matrix):
"""rescales the input data so that each column has mean 0
and standard deviation 1
columns with no deviation remain unchanged
"""
means, stdevs = scale(data_matrix)
def rescaled(i, j):
if stdevs[j] > 0:
return (data_matrix[i][j] - means[j]) / stdevs[j]
else:
return data_matrix[i][j]
num_rows, num_cols = shape(data_matrix)
return make_matrix(num_rows, num_cols, rescaled)