Repository navigation
Expand file tree
/
Copy path3code_transfer learning.py
More file actions
159 lines (135 loc) · 5.34 KB
/
Copy path3code_transfer learning.py
File metadata and controls
159 lines (135 loc) · 5.34 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
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
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 31 19:06:58 2025
@author: Gao Jie
Article Title: Construction of a machine learning-based pulmonary function prediction model for the Chinese population
Description:Transfer learning
Method:
Fuqing cohort assists other cohort in modeling,
FEV1 modeling using top six features,
FVC modeling using top seven features.
"""
import numpy as np
import pandas as pd
#from xgboost import XGBRegressor
#from deepforest import CascadeForestRegressor
from sklearn.model_selection import KFold, RepeatedKFold
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
from sklearn.ensemble import RandomForestRegressor,GradientBoostingRegressor, AdaBoostRegressor,BaggingRegressor
from sklearn.ensemble import HistGradientBoostingRegressor,ExtraTreesRegressor
from sklearn.tree import DecisionTreeRegressor,ExtraTreeRegressor
from sklearn.linear_model import LinearRegression,Ridge,Lasso,LogisticRegression
from sklearn.neighbors import KNeighborsRegressor
from sklearn.neural_network import MLPRegressor
from sklearn.svm import LinearSVR
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from math import sqrt
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import RidgeCV
from sklearn.svm import LinearSVR
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import StackingRegressor
from sklearn.dummy import DummyRegressor
from sklearn.preprocessing import normalize
from scipy.spatial import distance
from sklearn.svm import SVR, LinearSVR
import shap
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
#parameter setting
seed = 42
n_splits=10
#method define
def load_data(dataname):
data = pd.read_csv(dataname)
data = data.values
label1 = data.shape[1]-1
label2 = data.shape[1]-2
x = data[:,:label2]
y1 = data[:,label1] #y1 corresponds to FEV1
y2 = data[:,label2] #y2 corresponds to FVC
return data,x,y1,y2
def delete_noise(data,rate):
#Normalize data
data_normalize = normalize(data, norm="l2")
#Calculate cluster center
clster_center = np.mean(data_normalize, axis=0)
#Calculate the distance between each sample and the cluster center
distance_list=[]
for item in data_normalize:
distance_os = distance.euclidean(item,clster_center)
distance_list.append(distance_os)
#Sample ranking by Euclidean distance
distance_list = np.array(distance_list)
sorted_index = np.argsort(distance_list)
#Eliminate noise
size = int(len(sorted_index)*rate)
sorted_index_remained = sorted_index[:size]
return sorted_index_remained
#main process
data_fq, x_fq, y1_fq, y2_fq = load_data("fuqing.csv")
sorted_index_remained_fq = delete_noise(data_fq,0.99)
x_fq = x_fq[sorted_index_remained_fq,:]
y1_fq = y1_fq[sorted_index_remained_fq]
y2_fq = y2_fq[sorted_index_remained_fq]
model1 = GradientBoostingRegressor(random_state=seed).fit(x_fq, y1_fq)
model2 = GradientBoostingRegressor(random_state=seed).fit(x_fq, y2_fq)
feature_sort_index1 = np.argsort(-model1.feature_importances_)[0:6]
feature_sort_index2 = np.argsort(-model2.feature_importances_)[0:7]
x_fq1 = x_fq[:,feature_sort_index1]
x_fq2 = x_fq[:,feature_sort_index2]
data,x,y1,y2 = load_data("zhuhai.csv") #this can be replaced with Wuhan data
sorted_index_remained = delete_noise(data,0.99)
x1 = x[sorted_index_remained]
x1 = x1[:,feature_sort_index1]
x2 = x[sorted_index_remained]
x2 = x2[:,feature_sort_index2]
y1 = y1[sorted_index_remained]
y2 = y2[sorted_index_remained]
r21,r22 = [],[]
rmse1,rmse2 = [],[]
mae1, mae2 = [],[]
rkf = RepeatedKFold(n_splits=n_splits, n_repeats=5, random_state=seed) #, random_state=seed
for i, (train_index, test_index) in enumerate(rkf.split(x1)):
x_train1 = x1[train_index]
x_train2 = x2[train_index]
y_train1 = y1[train_index]
y_train2 = y2[train_index]
x_test1 = x1[test_index]
x_test2 = x2[test_index]
y_test1 = y1[test_index]
y_test2 = y2[test_index]
x_train1 = np.vstack([x_train1, x_fq1])
y_train1 = np.hstack([y_train1, y1_fq])
x_train2 = np.vstack([x_train2, x_fq2])
y_train2 = np.hstack([y_train2, y2_fq])
model1=GradientBoostingRegressor(random_state=seed).fit(x_train1, y_train1)
model2=GradientBoostingRegressor(random_state=seed).fit(x_train2, y_train2)
y_pred1 = model1.predict(x_test1)
y_pred2 = model2.predict(x_test2)
r2_temp1 = r2_score(y_test1,y_pred1)
r2_temp2 = r2_score(y_test2,y_pred2)
r21.append(r2_temp1)
r22.append(r2_temp2)
mse1 = mean_squared_error(y_test1,y_pred1)
mse2 = mean_squared_error(y_test2,y_pred2)
rmse_temp1 = sqrt(mse1)
rmse_temp2 = sqrt(mse2)
rmse1.append(rmse_temp1)
rmse2.append(rmse_temp2)
mae_temp1 = mean_absolute_error(y_test1,y_pred1)
mae_temp2 = mean_absolute_error(y_test2,y_pred2)
mae1.append(mae_temp1)
mae2.append(mae_temp2)
print(i+1,"-th modeling complete")
#Result output
print("******")
print("R2 for FEV1:", round(np.mean(r21),2))
print("R2 for FVC:", round(np.mean(r22),2))
print("******")
print("RMSE for FEV1:", round(np.mean(rmse1),2))
print("RMSE for FVC:", round(np.mean(rmse2),2))
print("******")
print("MAE for FEV1:", round(np.mean(mae1),2))
print("MAE for FVC:", round(np.mean(mae2),2))