This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_model.py
177 lines (150 loc) · 5.25 KB
/
linear_model.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3 -W ignore::DeprecationWarning
# -*- coding: utf-8 -*
"""
"""
import sys
import timeit
from typing import Tuple
import warnings
import numpy as np
import pandas as pd
from sklearn.linear_model import Lasso, LassoCV, LinearRegression, RidgeCV
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import cross_validate, train_test_split
# Helpers
def test_rmse(
models: Tuple,
X_test: pd.DataFrame,
y_test: pd.Series,
) -> float:
"""
Trains given model and calculate testing root mean squared error.
Parameters
----------
models : Tuple
A tuple of cross-validation split estimators
X_test : pd.DataFrame
The testing set of SNPs.
y_test : pd.Series
The testing set of phenotype values.
Return
------
float
The root mean squared error.
"""
rmse = 0
for model in models:
y_pred = model.predict(X_test)
rmse += np.sqrt(mean_squared_error(y_test, y_pred))
return rmse / len(models)
def num_lasso_nonzero_weights(lassos: Tuple[Lasso]) -> float:
"""
Trains given model and calculate testing root mean squared error.
Parameters
----------
lassos : tuple of Lasso objects
Cross validation lasso objects.
Return
------
float
The mean number of nonzero weights.
"""
mean_nonzero_weights = []
for lasso in lassos:
mean_nonzero_weights.append(len(np.nonzero(lasso.coef_)[0]))
return np.mean(mean_nonzero_weights)
# Scikit-learn deprecation warnings...
warnings.filterwarnings("ignore", category = DeprecationWarning)
# Pass file paths as arguments to this script and load into dataframes.
# linear_model.py [gt_pt_lm.csv]
dataset_file_path = sys.argv[1]
model_iters = float(sys.argv[2])
modeling_rounds = int(sys.argv[3])
data = pd.read_csv(dataset_file_path, index_col = [0]) # sample id as indices
print("Loading complete.")
# Format output dataframes.
output_df = pd.DataFrame(
data = 0.0, index = ["OLS", "Lasso", "Ridge"],
columns = [
"Average_RMSE_50", "Average_R2_50",
"Average_RMSE_100", "Average_R2_100",
"Average_RMSE_150", "Average_R2_150",
"Average_RMSE_200", "Average_R2_200", "Average_Runtime"
]
)
lasso_weights_df = pd.DataFrame(
data = 0.0, index = ["Lasso_Non_Zero_Weights"],
columns = ["Average_50", "Average_100", "Average_150", "Average_200"]
)
print("Output formatting complete.")
# Split 200 snps (features) into 4 sets, incrementing by 50.
snps = data.columns[1:].values
top_features = np.array([50, 100, 150, 200])
cv_scoring = ("r2", "neg_mean_squared_error")
# Train test split
X_train, X_test, y_train, y_test = train_test_split(
data.loc[:, snps], data.pt, test_size = 30
)
# Real stuff
print("Machine learning in progress...")
for i in range(modeling_rounds):
print(f"Round {i + 1}...")
for j in range(4):
num_feat = top_features[j]
curr_snps = snps[:num_feat]
# OLS
ols_start = timeit.default_timer()
ols = LinearRegression()
ols_cv = cross_validate(
estimator = ols,
X = X_train.loc[:, curr_snps],
y = y_train,
cv = 5,
return_estimator = True
)
output_df.at["OLS", str("Average_RMSE_" + str(num_feat))] += \
test_rmse(ols_cv["estimator"], X_test.loc[:, curr_snps], y_test)
output_df.at["OLS", str("Average_R2_" + str(num_feat))] += \
np.mean(ols_cv["test_score"], )
output_df.at["OLS", "Average_Runtime"] += \
(timeit.default_timer() - ols_start)
# Lasso
lasso_start = timeit.default_timer()
lasso = LassoCV(cv = 5, max_iter = model_iters)
lasso_cv = cross_validate(
estimator = lasso,
X = X_train.loc[:, curr_snps],
y = y_train,
cv = 5,
return_estimator = True
)
output_df.at["Lasso", str("Average_RMSE_" + str(num_feat))] += \
test_rmse(lasso_cv["estimator"], X_test.loc[:, curr_snps], y_test)
output_df.at["Lasso", str("Average_R2_" + str(num_feat))] += \
np.mean(lasso_cv["test_score"])
lasso_weights_df.at[
"Lasso_Non_Zero_Weights", str("Average_" + str(num_feat))
] += num_lasso_nonzero_weights(lasso_cv["estimator"])
output_df.at["Lasso", "Average_Runtime"] += \
(timeit.default_timer() - lasso_start)
# Ridge regression
ridge_start = timeit.default_timer()
ridge = RidgeCV(cv = 5)
ridge_cv = cross_validate(
estimator = ridge,
X = X_train.loc[:, curr_snps],
y = y_train,
cv = 5,
return_estimator = True
)
output_df.at["Ridge", str("Average_RMSE_" + str(num_feat))] += \
test_rmse(ridge_cv["estimator"], X_test.loc[:, curr_snps], y_test)
output_df.at["Ridge", str("Average_R2_" + str(num_feat))] += \
np.mean(ridge_cv["test_score"])
output_df.at["Ridge", "Average_Runtime"] += \
(timeit.default_timer() - ridge_start)
output_df /= modeling_rounds
lasso_weights_df /= modeling_rounds
output_df.to_csv("lm_comparison.csv")
lasso_weights_df.to_csv("lasso_nonzero_weights.csv")
print("Done!")