-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstat_read_x.py
42 lines (32 loc) · 943 Bytes
/
stat_read_x.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
"""
Read and construct design matrix.
Author: Chao Huang ([email protected])
Last update: 2017-08-14
"""
import numpy as np
"""
installed all the libraries above
"""
def read_x(var_matrix, var_type):
"""
Read and construct design matrix.
Args:
var_matrix (matrix): un-normalized design matrix (n*(p-1))
var_type (vector): covariate type in var_matrix (0-discrete; 1-continuous)
"""
n, p = var_matrix.shape
if n < p:
mat = var_matrix.T
else:
mat = var_matrix
n, p = mat.shape
mat_new = np.zeros((n, p))
for kk in range(p):
if var_type[kk] == 1:
mat_new[:, kk] = (mat[:, kk] - np.mean(mat[:, kk]))/np.std(mat[:, kk])
# mat_new[:, kk] = mat[:, kk]/np.sqrt(np.sum(mat[:, kk]**2))
else:
mat_new[:, kk] = mat[:, kk]
const = np.ones((n, 1))
x_design = np.hstack((const, mat_new))
return x_design