-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhelper_methods.py
More file actions
67 lines (54 loc) · 2.17 KB
/
Copy pathhelper_methods.py
File metadata and controls
67 lines (54 loc) · 2.17 KB
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
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LassoCV, LogisticRegression, LogisticRegressionCV, MultiTaskLassoCV, RidgeCV
from sklearn.multiclass import OneVsRestClassifier
from sklearn.decomposition import PCA
from sklearn.metrics import mean_squared_error
import scipy
def pipeline_regression(X_train,y_train,X_test,regression_method,seed,n_components=None):
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
var = 0
if n_components is not None:
pca = PCA(n_components=n_components)
X_train=pca.fit_transform(X_train)
X_test=pca.transform(X_test)
variance_explained = pca.explained_variance_ratio_
for i in range(20):
var = var+variance_explained[i]
linreg =regression_method(X_train,y_train,seed)
# return var
return linreg,X_test,var
def custom_logistic_regression(X,y,seed):
clf = LogisticRegressionCV(cv=5, random_state=seed,max_iter=200,scoring='roc_auc')
multi_clf = OneVsRestClassifier(clf,n_jobs=-1)
estimator= multi_clf.fit(X,y)
return estimator
def custom_linear_regression(X, y, seed):
# print(y.shape)
if len(y.shape) > 1:
linreg = MultiTaskLassoCV(max_iter=1000, n_alphas=200, random_state=seed, n_jobs=-1)
else:
linreg = LassoCV(max_iter=1000, n_alphas=200, random_state=seed, n_jobs=-1)
try:
estimator = linreg.fit(X, y)
except ValueError:
print("Error in fitting")
return None
return estimator
def metrics_per_descritor(X, y, linreg):
predicted = linreg.predict(X)
mseerrors = []
correlations = []
if len(y.shape) > 1:
for i in range(y.shape[1]):
mseerror = mean_squared_error(predicted[:, i], y[:, i])
correlation = scipy.stats.pearsonr(predicted[:, i], y[:, i])
mseerrors.append(mseerror)
correlations.append(correlation)
else:
mseerror = mean_squared_error(predicted, y)
correlation = scipy.stats.pearsonr(predicted, y)
mseerrors.append(mseerror)
correlations.append(correlation)
return predicted, mseerrors, correlations