SunDou commited on
Commit
4b0c608
·
verified ·
1 Parent(s): fea230f

Upload data/dataset_Biochemistry.csv with huggingface_hub

Browse files
Files changed (1) hide show
  1. data/dataset_Biochemistry.csv +968 -0
data/dataset_Biochemistry.csv ADDED
@@ -0,0 +1,968 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ "keyword","repo_name","file_path","file_extension","file_size","line_count","content","language"
2
+ "Biochemistry","Bin-Cao/TCLRmodel","Template/Execution template/template.py",".py","5138","104","#coding=utf-8
3
+ from TCLR import TCLRalgorithm as model
4
+
5
+
6
+ """"""
7
+ :param correlation : {'PearsonR(+)','PearsonR(-)',''MIC','R2'},default PearsonR(+).
8
+ Methods:
9
+ * PearsonR: (+)(-). for linear relationship.
10
+ * MIC for no-linear relationship.
11
+ * R2 for no-linear relationship.
12
+
13
+ :param tolerance_list: constraints imposed on features, default is null
14
+ list shape in two dimensions, viz., [[constraint_1,tol_1],[constraint_2,tol_2]...]
15
+ constraint_1, constraint_2 (string) are the feature name ;
16
+ tol_1, tol_2 (float)are feature's tolerance ratios;
17
+ relative variation range of features must be within the tolerance;
18
+ example: tolerance_list = [['feature_name1',0.2],['feature_name2',0.1]].
19
+
20
+ :param gpl_dummyfea: dummy features in gpleran regression, default is null
21
+ list shape in one dimension, viz., ['feature_name1','feature_name2',...]
22
+ dummy features : 'feature_name1','feature_name2',... are not used anymore in gpleran regression
23
+
24
+ :param minsize : a int number (default=3), minimum unique values for linear features of data on each leaf.
25
+
26
+ :param threshold : a float (default=0.9), less than or equal to 1, default 0.95 for PearsonR.
27
+ In the process of dividing the dataset, the smallest relevant index allowed in the you research.
28
+ To avoid overfitting, threshold = 0.5 is suggested for MIC 0.5.
29
+
30
+ :param mininc : Minimum expected gain of objective function (default=0.01)
31
+
32
+ :param split_tol : a float (default=0.8), constrained features value shound be narrowed in a minmimu ratio of split_tol on split path
33
+
34
+ :param gplearn : Whether to call the embedded gplearn package of TCLR to regress formula (default=False).
35
+
36
+ :param population_size : integer, optional (default=500), the number of programs in each generation.
37
+
38
+ :param generations : integer, optional (default=100),the number of generations to evolve.
39
+
40
+ :param verbose : int, optional (default=0). Controls the verbosity of the evolution building process.
41
+
42
+ :param metric : str, optional (default='mean absolute error')
43
+ The name of the raw fitness metric. Available options include:
44
+ - 'mean absolute error'.
45
+ - 'mse' for mean squared error.
46
+ - 'rmse' for root mean squared error.
47
+ - 'pearson', for Pearson's product-moment correlation coefficient.
48
+ - 'spearman' for Spearman's rank-order correlation coefficient.
49
+
50
+ :param function_set : iterable, optional (default=['add', 'sub', 'mul', 'div', 'log', 'sqrt',
51
+ 'abs', 'neg','inv','sin','cos','tan', 'max', 'min'])
52
+ The functions to use when building and evolving programs. This iterable can include strings
53
+ to indicate either individual functions as outlined below.
54
+ Available individual functions are:
55
+ - 'add' : addition, arity=2.
56
+ - 'sub' : subtraction, arity=2.
57
+ - 'mul' : multiplication, arity=2.
58
+ - 'div' : protected division where a denominator near-zero returns 1.,
59
+ arity=2.
60
+ - 'sqrt' : protected square root where the absolute value of the
61
+ argument is used, arity=1.
62
+ - 'log' : protected log where the absolute value of the argument is
63
+ used and a near-zero argument returns 0., arity=1.
64
+ - 'abs' : absolute value, arity=1.
65
+ - 'neg' : negative, arity=1.
66
+ - 'inv' : protected inverse where a near-zero argument returns 0.,
67
+ arity=1.
68
+ - 'max' : maximum, arity=2.
69
+ - 'min' : minimum, arity=2.
70
+ - 'sin' : sine (radians), arity=1.
71
+ - 'cos' : cosine (radians), arity=1.
72
+ - 'tan' : tangent (radians), arity=1.
73
+
74
+ Algorithm Patent No. : 2021SR1951267, China
75
+ Reference : Domain knowledge guided interpretive machine learning —— Formula discovery for the oxidation behavior of Ferritic-Martensitic steels in supercritical water. Bin Cao et al., 2022, JMI, journal paper.
76
+ DOI : 10.20517/jmi.2022.04
77
+ """"""
78
+
79
+
80
+ dataSet = ""testdata.csv""
81
+ correlation = 'PearsonR(+)'
82
+ tolerance_list = [
83
+ ['E_Cr_split_feature_1',0.001],
84
+ ]
85
+
86
+ gpl_dummyfea = ['ln(t)_split_feature_4',]
87
+ minsize = 3
88
+ threshold = 0.9
89
+ mininc = 0.01
90
+ split_tol = 0.8
91
+ gplearn = True
92
+ population_size = 500
93
+ generations = 100
94
+ verbose = 1
95
+ metric = 'mean absolute error'
96
+ function_set = ['add', 'sub', 'mul', 'div', 'log', 'sqrt', 'abs', 'neg','inv','sin','cos','tan', 'max', 'min']
97
+
98
+
99
+ model.start(filePath = dataSet, correlation = correlation, tolerance_list = tolerance_list, gpl_dummyfea = gpl_dummyfea, minsize = minsize, threshold = threshold,
100
+ mininc = mininc ,split_tol = split_tol, gplearn = gplearn, population_size = population_size,
101
+ generations = generations,verbose = verbose, metric =metric, function_set =function_set)
102
+
103
+
104
+
105
+ ","Python"
106
+ "Biochemistry","Bin-Cao/TCLRmodel","TCLR/TCLRalgorithm.py",".py","38792","861","""""""
107
+ Tree Classifier for Linear Regression (TCLR)
108
+ Author : Bin CAO ([email protected])
109
+
110
+ TCLR is a new tree model proposed by Professor T-Y Zhang and Mr. Bin Cao et al. for capturing the functional relationships
111
+ between features and target variables. The model partitions the feature space into a set of rectangles, with each partition
112
+ embodying a specific function. This approach is conceptually simple, yet powerful for distinguishing mechanisms. The entire
113
+ feature space is divided into disjointed unit intervals by hyperplanes parallel to the coordinate axes. Within each partition,
114
+ the target variable y is modeled as a linear function of a feature xj (j = 1,⋯,m), which is the linear function used in our studied problem.
115
+
116
+ Patent No. : 2021SR1951267, China
117
+ Reference : Domain knowledge guided interpretive machine learning —— Formula discovery for the oxidation behavior of Ferritic-Martensitic steels in supercritical water. Bin Cao et al., 2022, JMI, journal paper.
118
+ DOI : 10.20517/jmi.2022.04
119
+ """"""
120
+
121
+ import math
122
+ import re
123
+ from tabnanny import check
124
+ from textwrap import indent
125
+ import time
126
+ import copy
127
+ import os
128
+ import warnings
129
+ import random
130
+ from typing import List
131
+ import numpy as np
132
+ import pandas as pd
133
+ from graphviz import Digraph
134
+ from scipy import stats
135
+ from gplearn import genetic
136
+ from minepy import MINE
137
+
138
+
139
+ # Define the basic structure of a Tree Model - Node
140
+ class Node:
141
+ def __init__(self, data):
142
+ self.data = data
143
+ self.lc = None
144
+ self.rc = None
145
+ self.slope = None
146
+ self.intercept = None
147
+ self.size = data.shape[0]
148
+ self.R = 0
149
+ self.bestFeature = 0
150
+ self.bestValue = 0
151
+ self.leaf_no = -1
152
+
153
+
154
+ def start(filePath, correlation='PearsonR(+)', minsize=3, threshold=0.95, mininc=0.01, split_tol = 0.8, epochs = 5,random_seed=42, Generate_Features = True, tolerance_list = None , weight=True,
155
+ gplearn = False, gpl_dummyfea = None, population_size = 500, generations = 100, verbose = 1,
156
+ metric = 'mean absolute error',
157
+ function_set = ['add', 'sub', 'mul', 'div', 'log', 'sqrt', 'abs', 'neg','inv','sin','cos','tan',]):
158
+
159
+ """"""
160
+ Tree Classifier for Linear Regression (TCLR)
161
+
162
+ TCLR is a new tree model proposed by Professor T-Y Zhang and Mr. Bin Cao et al. for capturing the functional relationships
163
+ between features and target variables. The model partitions the feature space into a set of rectangles, with each partition
164
+ embodying a specific function. This approach is conceptually simple, yet powerful for distinguishing mechanisms. The entire
165
+ feature space is divided into disjointed unit intervals by hyperplanes parallel to the coordinate axes. Within each partition,
166
+ the target variable y is modeled as a linear function of a feature xj (j = 1,⋯,m), which is the linear function used in our studied problem.
167
+
168
+ Patent No. : 2021SR1951267, China
169
+ Reference : Domain knowledge guided interpretive machine learning —— Formula discovery for the oxidation behavior of Ferritic-Martensitic steels in supercritical water. Bin Cao et al., 2022, JMI, journal paper.
170
+ DOI : 10.20517/jmi.2022.04
171
+
172
+ :param correlation : {'PearsonR(+)','PearsonR(-)',''MIC','R2'}, default PearsonR(+).
173
+ Methods:
174
+ * PearsonR: (+)(-). for linear relationship.
175
+ * MIC for no-linear relationship.
176
+ * R2 for no-linear relationship.
177
+
178
+ The evaluation factor for capture the functional relationship between feature and response
179
+ 1>
180
+ PearsonR:
181
+ Pearson correlation coefficient, also known as Pearson's r, the Pearson product-moment correlation coefficient.
182
+ PearsonR is a measure of linear correlation between two sets of data.
183
+ PearsonR = Cov(X,Y) / (sigmaX * sigmaY)
184
+
185
+ 2>
186
+ MIC:
187
+ The maximal information coefficient (MIC). MIC captures a wide range of associations both functional and not,
188
+ and for functional relationships provides a score that roughly equals the coefficient of determination (R2) of
189
+ the data relative to the regression function. MIC belongs to a larger class of maximal information-based
190
+ nonparametric exploration (MINE) statistics for identifying and classifying relationship.
191
+ Reference : Reshef, D. N., Reshef, Y. A., Finucane, H. K., Grossman, S. R., McVean, G., Turnbaugh, P. J., ...
192
+ and Sabeti, P. C. (2011). Detecting novel associations in large data sets. science, 334(6062), 1518-1524.
193
+
194
+ 3>
195
+ R2:
196
+ In statistics, the coefficient of determination, denoted R2 or r2 and pronounced ""R squared"",
197
+ is the proportion of the variation in the dependent variable that is predictable from the independent variable(s).
198
+ t is a statistic used in the context of statistical models whose main purpose is either the prediction of future
199
+ outcomes or the testing of hypotheses, on the basis of other related information. It provides a measure of how well
200
+ observed outcomes are replicated by the model, based on the proportion of total variation of outcomes explained by the model.
201
+ Definition from Wikipedia : https://en.wikipedia.org/wiki/Coefficient_of_determination
202
+ R2 = 1 - SSres / SStot. Its value may be a negative one for poor correlation.
203
+
204
+
205
+ :param minsize :
206
+ a int number (default=3), minimum unique values for linear features of data on each leaf.
207
+
208
+ :param threshold :
209
+ a float (default=0.9), less than or equal to 1, default 0.95 for PearsonR.
210
+ In the process of dividing the dataset, the smallest relevant index allowed in the you research.
211
+ To avoid overfitting, threshold = 0.5 is suggested for MIC 0.5.
212
+
213
+ :param mininc : Minimum expected gain of objective function (default=0.01)
214
+
215
+ :param split_tol : a float (default=0.8), constrained features value shound be narrowed in a minmimu ratio of split_tol on split path
216
+
217
+ :param epochs : an integer (default=5), see parameter Generate_Features (below)
218
+
219
+ :param random_seed : an integer (default=42), see parameter Generate_Features (below)
220
+
221
+ :param Generate_Features : boole (default=True). When Generate_Features = True, TCLR will generate new features by operating
222
+ the ['+','-','*'] on original features. Iterating [param : epoachs] times, and each time generating 3 new features. [param : random_seed]
223
+ is used to control the randomness.
224
+ When Generate_Features = False, TCLR will apply the original features
225
+
226
+ :param tolerance_list:
227
+ constraints imposed on features, default is null
228
+ list shape in two dimensions, viz., [['feature_name1',tol_1],['feature_name2',tol_2]...]
229
+ 'feature_name1', 'feature_name2' (string) are names of input features;
230
+ tol_1, tol_2 (float, between 0 to 1) are feature's tolerance ratios;
231
+ the variations of feature values on each leaf must be in the tolerance;
232
+ if tol_1 = 0, the value of feature 'feature_name1' must be a constant on each leaf,
233
+ if tol_1 = 1, there is no constraints on value of feature 'feature_name1';
234
+ example: tolerance_list = [['feature_name1',0.2],['feature_name2',0.1]].
235
+
236
+ :param weight:
237
+ The weight of the gain function, default is True.
238
+ When weight is True: linear_gain = R(father node) - ( W_l * R(left child node) + W_r * R(right child node)) / 2
239
+ Where W_l is the ratio of the number of samples in the left child node to the total number of samples ;
240
+ W_r is the ratio of the number of samples in the right child node to the total number of samples.
241
+ When weight is False: linear_gain = R(father node) - ( R(left child node) + R(right child node)) / 2
242
+
243
+ :param gplearn : Whether to call the embedded gplearn package of TCLR to regress formula (default=False).
244
+
245
+ :param gpl_dummyfea:
246
+ dummy features in gpleran regression, default is null
247
+ list shape in one dimension, viz., ['feature_name1','feature_name2',...]
248
+ dummy features : 'feature_name1','feature_name2',... are not used anymore in gpleran regression
249
+
250
+ :param population_size : integer, optional (default=500), the number of programs in each generation.
251
+
252
+ :param generations : integer, optional (default=100),the number of generations to evolve.
253
+
254
+ :param verbose : int, optional (default=0). Controls the verbosity of the evolution building process.
255
+
256
+ :param metric :
257
+ str, optional (default='mean absolute error')
258
+ The name of the raw fitness metric. Available options include:
259
+ - 'mean absolute error'.
260
+ - 'mse' for mean squared error.
261
+ - 'rmse' for root mean squared error.
262
+ - 'pearson', for Pearson's product-moment correlation coefficient.
263
+ - 'spearman' for Spearman's rank-order correlation coefficient.
264
+
265
+ :param function_set :
266
+ iterable, optional (default=['add', 'sub', 'mul', 'div', 'log', 'sqrt',
267
+ 'abs', 'neg','inv','sin','cos','tan', 'max', 'min'])
268
+ The functions to use when building and evolving programs. This iterable can include strings
269
+ to indicate either individual functions as outlined below.
270
+ Available individual functions are:
271
+ - 'add' : addition, arity=2.
272
+ - 'sub' : subtraction, arity=2.
273
+ - 'mul' : multiplication, arity=2.
274
+ - 'div' : protected division where a denominator near-zero returns 1.,
275
+ arity=2.
276
+ - 'sqrt' : protected square root where the absolute value of the
277
+ argument is used, arity=1.
278
+ - 'log' : protected log where the absolute value of the argument is
279
+ used and a near-zero argument returns 0., arity=1.
280
+ - 'abs' : absolute value, arity=1.
281
+ - 'neg' : negative, arity=1.
282
+ - 'inv' : protected inverse where a near-zero argument returns 0.,
283
+ arity=1.
284
+ - 'max' : maximum, arity=2.
285
+ - 'min' : minimum, arity=2.
286
+ - 'sin' : sine (radians), arity=1.
287
+ - 'cos' : cosine (radians), arity=1.
288
+ - 'tan' : tangent (radians), arity=1.
289
+
290
+ Exampel :
291
+ #coding=utf-8
292
+ from TCLR import TCLRalgorithm as model
293
+
294
+ dataSet = ""testdata.csv""
295
+ correlation = 'PearsonR(+)'
296
+ minsize = 3
297
+ threshold = 0.9
298
+ mininc = 0.01
299
+ split_tol = 0.8
300
+
301
+ model.start(filePath = dataSet, correlation = correlation, minsize = minsize, threshold = threshold,
302
+ mininc = mininc ,split_tol = split_tol,)
303
+ """"""
304
+
305
+ os.makedirs('Segmented', exist_ok=True)
306
+ # global var. for statisticaling results
307
+ global record
308
+ record = 0
309
+ timename = time.localtime(time.time())
310
+ namey, nameM, named, nameh, namem = timename.tm_year, timename.tm_mon, timename.tm_mday, timename.tm_hour, timename.tm_min
311
+
312
+ read_csvData = pd.read_csv(filePath)
313
+
314
+ input_csvData = read_csvData.iloc[:,:-2]
315
+ if Generate_Features == True:
316
+ # cal an appropriate value of batch
317
+ if len(input_csvData) - 1 <= 3:
318
+ batch = 1
319
+ else:
320
+ batch = 3
321
+ # generate new dataset
322
+ for epoch in range(epochs):
323
+ # for increasing the randomness
324
+ random_seed += 1
325
+ input_csvData = generate_random_features(input_csvData,[column for column in input_csvData],batch,random_seed)
326
+
327
+ input_csvData = input_csvData.assign(linear_X=read_csvData.iloc[:,-2])
328
+ csvData = input_csvData.assign(linear_Y=read_csvData.iloc[:,-1])
329
+
330
+ else:
331
+ csvData = read_csvData
332
+
333
+
334
+ copy_csvData = copy.deepcopy(csvData)
335
+ copy_csvData['slope'] = None
336
+ copy_csvData['intercept'] = None
337
+ copy_csvData[correlation] = None
338
+ copy_csvData.to_csv('Segmented/all_dataset.csv', index=False)
339
+
340
+ feats = [column for column in csvData]
341
+ csvData = np.array(csvData)
342
+ root, _ = createTree(csvData, csvData, feats, 0, correlation,tolerance_list, minsize, threshold, mininc, split_tol,weight)
343
+
344
+ print('All non-image results have been successfully saved!')
345
+ print('#'*80,'\n')
346
+
347
+ # excute gplearn
348
+ if gplearn == True :
349
+ if correlation == 'MIC' or correlation == 'R2':
350
+ print('{name} is a non-linear correlation metrics'.format(name = correlation ))
351
+ print('This is illegal, linear slopes are only allowed to generate when PearsonR is chosen')
352
+ elif correlation == 'PearsonR(+)' or correlation == 'PearsonR(-)':
353
+ sr_data = pd.read_csv('Segmented/all_dataset.csv')
354
+ sr_featurname = sr_data.columns
355
+ sr_data = np.array(sr_data)
356
+
357
+ if gpl_dummyfea == None:
358
+
359
+ gpmodel = genetic.SymbolicRegressor(
360
+ population_size = population_size, generations = generations,
361
+ verbose = verbose,feature_names = sr_featurname[:-4],function_set = function_set,
362
+ metric = metric
363
+ )
364
+ formula = gpmodel.fit(sr_data[:,:-4], sr_data[:,-3])
365
+ score = gpmodel.score(sr_data[:,:-4], sr_data[:,-3])
366
+ print( 'slope = ' + str(formula))
367
+
368
+ else:
369
+ # fea_num --> fea_loc
370
+ dummyfea = []
371
+ for i in range(len(gpl_dummyfea)):
372
+ index = feats.index(gpl_dummyfea[i])
373
+ dummyfea.append(index)
374
+ # remove fea_loc
375
+ index_array = [i for i in range(len(sr_featurname)-4)]
376
+ for i in range(len(gpl_dummyfea)):
377
+ index_array.remove(dummyfea[i])
378
+
379
+ gpmodel = genetic.SymbolicRegressor(
380
+ population_size = population_size, generations = generations,
381
+ verbose = verbose,feature_names = sr_featurname[index_array],function_set = function_set,
382
+ metric = metric
383
+ )
384
+ formula = gpmodel.fit(sr_data[:,index_array], sr_data[:,-3])
385
+ score = gpmodel.score(sr_data[:,index_array], sr_data[:,-3])
386
+ print( 'slope = ' + str(formula))
387
+
388
+
389
+ with open(os.path.join('Segmented', 'A_formula derived by gplearn.txt'), 'w') as wfid:
390
+ print('Formula : ', file=wfid)
391
+ print(str(formula), file=wfid)
392
+ print('Fitness : ', file=wfid)
393
+ print(str(metric) + ' = ' + str(score), file=wfid)
394
+ print('\n', file=wfid)
395
+ print('#'*80, file=wfid)
396
+ print('Symbols annotation:', file=wfid)
397
+ print('- add : addition, arity=2.', file=wfid)
398
+ print('- sub : subtraction, arity=2.', file=wfid)
399
+ print('- mul : multiplication, arity=2.', file=wfid)
400
+ print('- div : protected division where a denominator near-zero returns 1.', file=wfid)
401
+ print('- sqrt : protected square root where the absolute value of the argument is used.', file=wfid)
402
+ print('- log : protected log where the absolute value of the argument is used.', file=wfid)
403
+ print('- abs : absolute value, arity=1.', file=wfid)
404
+ print('- neg : negative, arity=1.', file=wfid)
405
+ print('- inv : protected inverse where a near-zero argument returns 0.', file=wfid)
406
+ print('- max : maximum, arity=2.', file=wfid)
407
+ print('- sin : sine (radians), arity=1.', file=wfid)
408
+ print('- cos : cosine (radians), arity=1.', file=wfid)
409
+ print('- tan : tangent (radians), arity=1.', file=wfid)
410
+
411
+
412
+
413
+ elif gplearn == False:
414
+ pass
415
+
416
+
417
+ try:
418
+ # generate figure in pdf
419
+ warnings.filterwarnings('ignore')
420
+ dot = Digraph(comment='Result of TCLR')
421
+ render('A', root, dot, feats)
422
+ dot.render(
423
+ 'Result of TCLR {year}.{month}.{day}-{hour}.{minute}'.format(year=namey, month=nameM, day=named, hour=nameh,
424
+ minute=namem))
425
+ return True
426
+ except :
427
+ print('Can not generate the Tree plot !')
428
+ print('Please ensure that the executable files of Graphviz are present on your system.')
429
+ print('See : https://github.com/Bin-Cao/TCLRmodel/tree/main/User%20Guide')
430
+ return True
431
+
432
+
433
+
434
+ # Capture the functional relationships between features and target
435
+ # Partitions the feature space into a set of rectangles,
436
+ def createTree(dataSet, ori_dataset,feats, leaf_no, correlation,tolerance_list, minsize, threshold, mininc,split_tol,weight):
437
+ # It is a positive linear relationship
438
+ if correlation == 'PearsonR(+)':
439
+ node = Node(dataSet)
440
+ # Initial R0
441
+ bestR = PearsonR(dataSet[:, -2], dataSet[:, -1])
442
+ node.R = bestR
443
+ __slope = stats.linregress(dataSet[:, -2], dataSet[:, -1])[0]
444
+ node.slope = __slope
445
+ node.intercept = stats.linregress(dataSet[:, -2], dataSet[:, -1])[1]
446
+ if bestR >= threshold and fea_tol(dataSet,ori_dataset,feats,tolerance_list) == True:
447
+ node.leaf_no = leaf_no
448
+ leaf_no += 1
449
+ write_csv(node, feats, True, correlation)
450
+ return node, leaf_no
451
+ # Leave the last two columns of DataSet, a feature of interest and a response
452
+ numFeatures = len(dataSet[0]) - 2
453
+ splitSuccess = False
454
+ bestFeature = -1
455
+ bestValue = 0
456
+
457
+ check_valve = False
458
+ for i in range(numFeatures):
459
+ featList = [example[i] for example in dataSet]
460
+ uniqueVals = sorted(list(set(featList)))
461
+ for value in range(len(uniqueVals) - 1):
462
+ # constraints imposed on features (greater tolerance in split process)
463
+ if not fea_tol_split(dataSet,ori_dataset,feats,tolerance_list,split_tol):
464
+ continue
465
+ subDataSetA, subDataSetB = splitDataSet(dataSet, i, uniqueVals[value])
466
+
467
+ if np.unique(subDataSetA[:, -2]).size <= minsize - 1 or np.unique(
468
+ subDataSetB[:, -2]).size <= minsize - 1:
469
+ continue
470
+
471
+ R = weight_gain(subDataSetA,subDataSetB,weight,0)
472
+
473
+ if R - bestR >= mininc:
474
+ check_valve = True
475
+ splitSuccess = True
476
+ bestR = R
477
+ lc = subDataSetA
478
+ rc = subDataSetB
479
+ bestFeature = i
480
+ bestValue = uniqueVals[value]
481
+
482
+ if check_valve == False:
483
+ for i in range(numFeatures):
484
+ featList = [example[i] for example in dataSet]
485
+ uniqueVals = sorted(list(set(featList)))
486
+ for value in range(len(uniqueVals) - 1):
487
+ subDataSetA, subDataSetB = splitDataSet(dataSet, i, uniqueVals[value])
488
+
489
+ if np.unique(subDataSetA[:, -2]).size <= minsize - 1 or np.unique(
490
+ subDataSetB[:, -2]).size <= minsize - 1:
491
+ continue
492
+
493
+ R = weight_gain(subDataSetA,subDataSetB,weight,0)
494
+
495
+ if R - bestR >= mininc:
496
+ splitSuccess = True
497
+ bestR = R
498
+ lc = subDataSetA
499
+ rc = subDataSetB
500
+ bestFeature = i
501
+ bestValue = uniqueVals[value]
502
+ else:
503
+ pass
504
+
505
+
506
+ # The recursive boundary is unable to find a division node that can increase factor(R, MIC, R2) by mininc or more.
507
+ if splitSuccess:
508
+ node.lc, leaf_no = createTree(lc, ori_dataset,feats, leaf_no, correlation, tolerance_list,minsize, threshold, mininc,split_tol,weight)
509
+ node.rc, leaf_no = createTree(rc, ori_dataset,feats, leaf_no, correlation,tolerance_list, minsize, threshold, mininc,split_tol,weight)
510
+ node.bestFeature, node.bestValue = bestFeature, bestValue
511
+
512
+ # This node is leaf
513
+ if node.lc is None:
514
+ node.leaf_no = leaf_no
515
+ leaf_no += 1
516
+ # determine if this node is to save in all_dataset.csv
517
+ save_in_all = False
518
+ if node.R >= threshold and fea_tol(node.data,ori_dataset,feats,tolerance_list) == True:
519
+ save_in_all = True
520
+ write_csv(node, feats, save_in_all, correlation)
521
+
522
+ return node, leaf_no
523
+
524
+ # It is a negative linear relationship
525
+ elif correlation == 'PearsonR(-)':
526
+ node = Node(dataSet)
527
+ bestR = PearsonR(dataSet[:, -2], dataSet[:, -1])
528
+ node.R = bestR
529
+ __slope = stats.linregress(dataSet[:, -2], dataSet[:, -1])[0]
530
+ node.slope = __slope
531
+ node.intercept = stats.linregress(dataSet[:, -2], dataSet[:, -1])[1]
532
+ if bestR <= -threshold and fea_tol(dataSet,ori_dataset,feats,tolerance_list) == True:
533
+ node.leaf_no = leaf_no
534
+ leaf_no += 1
535
+ write_csv(node, feats, True, correlation)
536
+ return node, leaf_no
537
+
538
+ numFeatures = len(dataSet[0]) - 2
539
+ splitSuccess = False
540
+ bestFeature = -1
541
+ bestValue = 0
542
+
543
+ check_valve = False
544
+ for i in range(numFeatures):
545
+ featList = [example[i] for example in dataSet]
546
+ uniqueVals = sorted(list(set(featList)))
547
+ for value in range(len(uniqueVals) - 1):
548
+ # constraints imposed on features (greater tolerance in split process)
549
+ if not fea_tol_split(dataSet,ori_dataset,feats,tolerance_list,split_tol):
550
+ continue
551
+ subDataSetA, subDataSetB = splitDataSet(dataSet, i, uniqueVals[value])
552
+
553
+ if np.unique(subDataSetA[:, -2]).size <= minsize - 1 or np.unique(
554
+ subDataSetB[:, -2]).size <= minsize - 1:
555
+ continue
556
+
557
+
558
+ R = weight_gain(subDataSetA,subDataSetB,weight,0)
559
+
560
+ if R - bestR <= - mininc:
561
+ check_valve = True
562
+ splitSuccess = True
563
+ bestR = R
564
+ lc = subDataSetA
565
+ rc = subDataSetB
566
+ bestFeature = i
567
+ bestValue = uniqueVals[value]
568
+
569
+ if check_valve == False:
570
+ for i in range(numFeatures):
571
+ featList = [example[i] for example in dataSet]
572
+ uniqueVals = sorted(list(set(featList)))
573
+ for value in range(len(uniqueVals) - 1):
574
+ subDataSetA, subDataSetB = splitDataSet(dataSet, i, uniqueVals[value])
575
+
576
+ if np.unique(subDataSetA[:, -2]).size <= minsize - 1 or np.unique(
577
+ subDataSetB[:, -2]).size <= minsize - 1:
578
+ continue
579
+
580
+ R = weight_gain(subDataSetA,subDataSetB,weight,0)
581
+
582
+ if R - bestR <= - mininc:
583
+ splitSuccess = True
584
+ bestR = R
585
+ lc = subDataSetA
586
+ rc = subDataSetB
587
+ bestFeature = i
588
+ bestValue = uniqueVals[value]
589
+ else: pass
590
+
591
+ if splitSuccess:
592
+ node.lc, leaf_no = createTree(lc, ori_dataset,feats, leaf_no, correlation,tolerance_list, minsize, threshold, mininc,split_tol,weight)
593
+ node.rc, leaf_no = createTree(rc, ori_dataset,feats, leaf_no, correlation,tolerance_list, minsize, threshold, mininc,split_tol,weight)
594
+ node.bestFeature, node.bestValue = bestFeature, bestValue
595
+
596
+ if node.lc is None:
597
+ node.leaf_no = leaf_no
598
+ leaf_no += 1
599
+ # determine if this node is to save in all_dataset.csv
600
+ save_in_all = False
601
+ if node.R >= threshold and fea_tol(node.data,ori_dataset,feats,tolerance_list) == True:
602
+ save_in_all = True
603
+ write_csv(node, feats, save_in_all, correlation)
604
+
605
+ return node, leaf_no
606
+
607
+ elif correlation == 'MIC':
608
+ node = Node(dataSet)
609
+ bestR = MIC(dataSet[:, -2], dataSet[:, -1])
610
+ node.R = bestR
611
+ node.slope == None
612
+ if bestR >= threshold and fea_tol(dataSet,ori_dataset,feats,tolerance_list) == True:
613
+ node.leaf_no = leaf_no
614
+ leaf_no += 1
615
+ write_csv(node, feats, True, correlation)
616
+ return node, leaf_no
617
+
618
+ numFeatures = len(dataSet[0]) - 2
619
+ splitSuccess = False
620
+ bestFeature = -1
621
+ bestValue = 0
622
+
623
+ check_valve = False
624
+ for i in range(numFeatures):
625
+ featList = [example[i] for example in dataSet]
626
+ uniqueVals = sorted(list(set(featList)))
627
+ for value in range(len(uniqueVals) - 1):
628
+ # constraints imposed on features (greater tolerance in split process)
629
+ if not fea_tol_split(dataSet,ori_dataset,feats,tolerance_list,split_tol):
630
+ continue
631
+ subDataSetA, subDataSetB = splitDataSet(dataSet, i, uniqueVals[value])
632
+ if np.unique(subDataSetA[:, -2]).size <= minsize - 1 or np.unique(
633
+ subDataSetB[:, -2]).size <= minsize - 1:
634
+ continue
635
+
636
+ R = weight_gain(subDataSetA,subDataSetB,weight,1)
637
+
638
+ if R - bestR >= mininc:
639
+ check_valve = True
640
+ splitSuccess = True
641
+ bestR = R
642
+ lc = subDataSetA
643
+ rc = subDataSetB
644
+ bestFeature = i
645
+ bestValue = uniqueVals[value]
646
+
647
+ if check_valve == False:
648
+ for i in range(numFeatures):
649
+ featList = [example[i] for example in dataSet]
650
+ uniqueVals = sorted(list(set(featList)))
651
+ for value in range(len(uniqueVals) - 1):
652
+ subDataSetA, subDataSetB = splitDataSet(dataSet, i, uniqueVals[value])
653
+ if np.unique(subDataSetA[:, -2]).size <= minsize - 1 or np.unique(
654
+ subDataSetB[:, -2]).size <= minsize - 1:
655
+ continue
656
+
657
+ R = weight_gain(subDataSetA,subDataSetB,weight,1)
658
+
659
+ if R - bestR >= mininc:
660
+ splitSuccess = True
661
+ bestR = R
662
+ lc = subDataSetA
663
+ rc = subDataSetB
664
+ bestFeature = i
665
+ bestValue = uniqueVals[value]
666
+ else: pass
667
+
668
+ if splitSuccess:
669
+ node.lc, leaf_no = createTree(lc, ori_dataset,feats, leaf_no, correlation,tolerance_list, minsize, threshold, mininc,split_tol,weight)
670
+ node.rc, leaf_no = createTree(rc,ori_dataset, feats, leaf_no, correlation,tolerance_list, minsize, threshold, mininc,split_tol,weight)
671
+ node.bestFeature, node.bestValue = bestFeature, bestValue
672
+
673
+ if node.lc is None:
674
+ node.leaf_no = leaf_no
675
+ leaf_no += 1
676
+ # determine if this node is to save in all_dataset.csv
677
+ save_in_all = False
678
+ if node.R >= threshold and fea_tol(node.data,ori_dataset,feats,tolerance_list) == True:
679
+ save_in_all = True
680
+ write_csv(node, feats, save_in_all, correlation)
681
+
682
+ return node, leaf_no
683
+
684
+ elif correlation == 'R2':
685
+ node = Node(dataSet)
686
+ bestR = R2(dataSet[:, -2], dataSet[:, -1])
687
+ node.R = bestR
688
+ node.slope == None
689
+ if bestR >= threshold and fea_tol(dataSet,ori_dataset,feats,tolerance_list) == True:
690
+ node.leaf_no = leaf_no
691
+ leaf_no += 1
692
+ write_csv(node, feats, True, correlation)
693
+ return node, leaf_no
694
+
695
+ numFeatures = len(dataSet[0]) - 2
696
+ splitSuccess = False
697
+ bestFeature = -1
698
+ bestValue = 0
699
+
700
+ check_valve = False
701
+ for i in range(numFeatures):
702
+ featList = [example[i] for example in dataSet]
703
+ uniqueVals = sorted(list(set(featList)))
704
+ for value in range(len(uniqueVals) - 1):
705
+ # constraints imposed on features (greater tolerance in split process)
706
+ if not fea_tol_split(dataSet,ori_dataset,feats,tolerance_list,split_tol):
707
+ continue
708
+
709
+ subDataSetA, subDataSetB = splitDataSet(dataSet, i, uniqueVals[value])
710
+
711
+ if np.unique(subDataSetA[:, -2]).size <= minsize - 1 or np.unique(
712
+ subDataSetB[:, -2]).size <= minsize - 1:
713
+ continue
714
+
715
+ R = weight_gain(subDataSetA,subDataSetB,weight,2)
716
+
717
+ if R - bestR >= mininc:
718
+ check_valve = True
719
+ splitSuccess = True
720
+ bestR = R
721
+ lc = subDataSetA
722
+ rc = subDataSetB
723
+ bestFeature = i
724
+ bestValue = uniqueVals[value]
725
+
726
+ if check_valve == False:
727
+ for i in range(numFeatures):
728
+ featList = [example[i] for example in dataSet]
729
+ uniqueVals = sorted(list(set(featList)))
730
+
731
+ for value in range(len(uniqueVals) - 1):
732
+ if np.unique(subDataSetA[:, -2]).size <= minsize - 1 or np.unique(
733
+ subDataSetB[:, -2]).size <= minsize - 1:
734
+ continue
735
+
736
+ R = weight_gain(subDataSetA,subDataSetB,weight,2)
737
+
738
+ if R - bestR >= mininc:
739
+ splitSuccess = True
740
+ bestR = R
741
+ lc = subDataSetA
742
+ rc = subDataSetB
743
+ bestFeature = i
744
+ bestValue = uniqueVals[value]
745
+ else: pass
746
+
747
+ if splitSuccess:
748
+ node.lc, leaf_no = createTree(lc, ori_dataset,feats, leaf_no, correlation, tolerance_list,minsize, threshold, mininc,split_tol)
749
+ node.rc, leaf_no = createTree(rc, ori_dataset,feats, leaf_no, correlation,tolerance_list, minsize, threshold, mininc,split_tol)
750
+ node.bestFeature, node.bestValue = bestFeature, bestValue
751
+
752
+ if node.lc is None:
753
+ node.leaf_no = leaf_no
754
+ leaf_no += 1
755
+ # determine if this node is to save in all_dataset.csv
756
+ save_in_all = False
757
+ if node.R >= threshold and fea_tol(node.data,feats,tolerance_list) == True:
758
+ save_in_all = True
759
+ write_csv(node, feats, save_in_all, correlation)
760
+
761
+ return node, leaf_no
762
+
763
+
764
+ def PearsonR(X, Y):
765
+ xBar = np.mean(X)
766
+ yBar = np.mean(Y)
767
+ SSR = 0
768
+ varX = 0
769
+ varY = 0
770
+ if len(X) > 1:
771
+ for i in range(0, len(X)):
772
+ diffXXBar = X[i] - xBar
773
+ diffYYBar = Y[i] - yBar
774
+ SSR += (diffXXBar * diffYYBar)
775
+ varX += diffXXBar ** 2
776
+ varY += diffYYBar ** 2
777
+ SST = math.sqrt(varX * varY)
778
+ else:
779
+ SST = 1
780
+ SSR = 0
781
+ if SST == 0:
782
+ return 0
783
+ return SSR / SST
784
+
785
+
786
+ def MIC(X, Y):
787
+ if len(X) > 0:
788
+ mine = MINE(alpha=0.6, c=15)
789
+ mine.compute_score(X, Y)
790
+ return mine.mic()
791
+ else:
792
+ MICs = 0
793
+ return MICs
794
+
795
+
796
+ def R2(X, Y):
797
+ X = np.array(X)
798
+ Y = np.array(Y)
799
+ if len(X) > 0:
800
+ a = (X - np.mean(Y)) ** 2
801
+ SStot = np.sum(a)
802
+ b = (X - Y) ** 2
803
+ SSres = np.sum(b)
804
+ r2 = 1 - SSres / SStot
805
+ return r2
806
+ else:
807
+ r2 = -10
808
+ return r2
809
+
810
+
811
+ # Split the DataSet in a specific node
812
+ def splitDataSet(dataSet, axis, value):
813
+ retDataSetA = []
814
+ retDataSetB = []
815
+ for featVec in dataSet:
816
+ if featVec[axis] <= value:
817
+ retDataSetA.append(featVec)
818
+ else:
819
+ retDataSetB.append(featVec)
820
+ return np.array(retDataSetA), np.array(retDataSetB)
821
+
822
+ def fea_tol(dataSet,ori_dataSet,feats,tolerance_list):
823
+ if tolerance_list == None: return True
824
+ else:
825
+ record = 0
826
+ for i in range(len(tolerance_list)):
827
+ __feaname = tolerance_list[i][0]
828
+ __tolratio = float(tolerance_list[i][1])
829
+ index = feats.index(__feaname)
830
+ if (dataSet[:,index].max() - dataSet[:,index].min()) / (ori_dataSet[:,index].max()- ori_dataSet[:,index].min()) <= __tolratio:
831
+ record += 1
832
+ if record == len(tolerance_list):
833
+ return True
834
+
835
+ def fea_tol_split(dataSet,ori_dataSet,feats,tolerance_list,split_tol):
836
+ if tolerance_list == None: return True
837
+ else:
838
+ record = 0
839
+ for i in range(len(tolerance_list)):
840
+ __feaname = tolerance_list[i][0]
841
+ __tolratio = float(tolerance_list[i][1])
842
+ criter = max(split_tol,__tolratio)
843
+ index = feats.index(__feaname)
844
+ if (dataSet[:,index].max() - dataSet[:,index].min()) / (ori_dataSet[:,index].max()- ori_dataSet[:,index].min()) <= criter:
845
+ record += 1
846
+ if record > int(0.5*len(tolerance_list)):
847
+ return True
848
+
849
+
850
+ # Use graphviz to visualize the TCLR
851
+ def render(label, node, dot, feats):
852
+ mark = ''
853
+ if node.slope == None:
854
+ mark = ""#="" + str(node.size) + "" , ρ="" + str(round(node.R, 3))
855
+ else:
856
+ mark = ""#="" + str(node.size) + "" , ρ="" + str(round(node.R, 3)) + ' , slope=' + str(
857
+ round(node.slope, 3)) + ' , intercept=' + str(round(node.intercept, 3))
858
+
859
+ if node.lc is None:
860
+ mark = 'No_{}, '.format(node.leaf_no) + mark
861
+ dot.node(label, mark)
862
+
863
+ if node.lc is not None:
864
+ render(label + 'A', node.lc, dot, feats)
865
+ render(label + 'B', node.rc, dot, feats)
866
+ dot.edge(label, label + 'A', feats[node.bestFeature] + ""≤"" + str(node.bestValue))
867
+ dot.edge(label, label + 'B', feats[node.bestFeature] + "">"" + str(node.bestValue))
868
+
869
+
870
+ def write_csv(node, feats, save_in_all, correlation):
871
+ global record
872
+
873
+ frame = {}
874
+ for i in range(len(feats)):
875
+ frame[feats[i]] = node.data[:, i]
876
+ frame = pd.DataFrame(frame)
877
+
878
+ if node.slope == None:
879
+ frame['slope'] = None
880
+ frame['intercept'] = None
881
+ frame[correlation] = np.repeat(node.R, node.size)
882
+ frame.to_csv('Segmented/subdataset_{}.csv'.format(str(node.leaf_no)))
883
+ else:
884
+ frame['slope'] = node.slope
885
+ frame['intercept'] = node.intercept
886
+ frame[correlation] = np.repeat(node.R, node.size)
887
+ frame.to_csv('Segmented/subdataset_{}.csv'.format(str(node.leaf_no)))
888
+
889
+ frame.to_csv('Segmented/subdataset_{}.csv'.format(str(node.leaf_no)))
890
+
891
+ if not save_in_all: # do not save in the all_dataset.csv
892
+ _all_dataset = pd.read_csv('./Segmented/all_dataset.csv')
893
+ _all_dataset.drop(index=range(record, record+node.size), axis=0, inplace=True)
894
+ _all_dataset.to_csv('Segmented/all_dataset.csv', index=False)
895
+ return
896
+
897
+ _all_dataset = pd.read_csv('./Segmented/all_dataset.csv')
898
+ for item in range(len(frame.iloc[:, 0])):
899
+ _all_dataset.iloc[item + record, :] = frame.iloc[item, :]
900
+ record += len(frame.iloc[:, 0])
901
+ _all_dataset.to_csv('Segmented/all_dataset.csv', index=False)
902
+
903
+ def weight_gain(subDataSetA,subDataSetB,weight,matrix):
904
+ if matrix == 0:
905
+ newRa = PearsonR(subDataSetA[:, -2], subDataSetA[:, -1])
906
+ newRb = PearsonR(subDataSetB[:, -2], subDataSetB[:, -1])
907
+ elif matrix == 1:
908
+ newRa = MIC(subDataSetA[:, -2], subDataSetA[:, -1])
909
+ newRb = MIC(subDataSetB[:, -2], subDataSetB[:, -1])
910
+ elif matrix == 2:
911
+ newRa = R2(subDataSetA[:, -2], subDataSetA[:, -1])
912
+ newRb = R2(subDataSetB[:, -2], subDataSetB[:, -1])
913
+
914
+
915
+ if weight == False:
916
+ R = (newRa + newRb) / 2
917
+ return R
918
+ elif weight == True:
919
+ weightRa = len(subDataSetA[:, -1]) / (len(subDataSetA[:, -1]) + len(subDataSetB[:, -1]))
920
+ weightRb = len(subDataSetB[:, -1]) / (len(subDataSetA[:, -1]) + len(subDataSetB[:, -1]))
921
+ R = weightRa * newRa + weightRb * newRb
922
+ return R
923
+ else:
924
+ print('Parameter error | weight')
925
+
926
+
927
+ # code on 2023 May 9, Bin Cao
928
+ def generate_random_features(df: pd.DataFrame,
929
+ feature_list: List[str],
930
+ num_combinations: int,
931
+ random_seed:int) -> pd.DataFrame:
932
+ """"""
933
+ randomly generates new feature combinations.
934
+
935
+ :param df: DataFrame containing the original features.
936
+ :param feature_list: List of original features.
937
+ :param num_combinations: Number of combination features to generate.
938
+ :return: DataFrame containing the new features.
939
+ """"""
940
+ new_features = []
941
+ random.seed(random_seed)
942
+ # randomly generates combination features
943
+ for i in range(num_combinations):
944
+ # randomly chooses two features
945
+ f1 = random.choice(feature_list)
946
+ f2 = random.choice(feature_list)
947
+
948
+ # choose a operator
949
+ op = random.choice(['+', '-', '*',])
950
+
951
+ self_op1 = random.choice(['*1', '*2', '*3','*4','**2','**3'])
952
+ self_op2 = random.choice(['*1', '*2', '*3','*4','**2','**3'])
953
+
954
+ new_f1 = f'{f1} {self_op1}'
955
+ new_f2 = f'{f2} {self_op2}'
956
+
957
+ # new feature name
958
+ new_feature = f'({new_f1} {op} {new_f2})'
959
+
960
+ new_features.append(new_feature)
961
+
962
+ # cal new features
963
+ df[new_feature] = eval(f'(df[""{f1}""] {self_op1}) {op} (df[""{f2}""] {self_op2})')
964
+
965
+ # reture DataFrame
966
+ return df","Python"
967
+ "Biochemistry","Bin-Cao/TCLRmodel","Researches/Note.md",".md","38","2","# Relevant researches applied of TCLR
968
+ ","Markdown"