SciCodePile/SciCode-Domain-Code
DATA1: Domain-Specific Code Dataset Dataset Overview DATA1 is a large-scale domain-specific code dataset focusing on code samples from interdisciplinary fields such as biology, chemistry, materials science, and related areas. The dataset is collected and organized from GitHub repositories, covering 178 different domain topics with over 1.1 billion lines of code. Dataset Statistics Total Datasets: 178 CSV files Total Data Size: ~115 GB Total Lines… See the full description on the dataset page: https://huggingface.co/datasets/SciCodePile/SciCode-Domain-Code.
42.4k
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-83from TCLR import TCLRalgorithm as model4 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 null14 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 null21 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 path33 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 the61 argument is used, arity=1.62 - 'log' : protected log where the absolute value of the argument is63 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, China75 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.0477""""""78 79 80dataSet = ""testdata.csv""81correlation = 'PearsonR(+)'82tolerance_list = [83 ['E_Cr_split_feature_1',0.001],84]85 86gpl_dummyfea = ['ln(t)_split_feature_4',]87minsize = 388threshold = 0.989mininc = 0.0190split_tol = 0.891gplearn = True92population_size = 50093generations = 10094verbose = 1 95metric = 'mean absolute error'96function_set = ['add', 'sub', 'mul', 'div', 'log', 'sqrt', 'abs', 'neg','inv','sin','cos','tan', 'max', 'min']97 98 99model.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 (binjacobcao@gmail.com) 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 partition112 embodying a specific function. This approach is conceptually simple, yet powerful for distinguishing mechanisms. The entire113 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, China117 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.04119""""""120 121import math122import re123from tabnanny import check124from textwrap import indent125import time126import copy127import os128import warnings129import random130from typing import List131import numpy as np132import pandas as pd133from graphviz import Digraph134from scipy import stats135from gplearn import genetic136from minepy import MINE137 138 139# Define the basic structure of a Tree Model - Node140class Node:141 def __init__(self, data):142 self.data = data143 self.lc = None144 self.rc = None145 self.slope = None146 self.intercept = None147 self.size = data.shape[0]148 self.R = 0149 self.bestFeature = 0150 self.bestValue = 0151 self.leaf_no = -1152 153 154def 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 partition164 embodying a specific function. This approach is conceptually simple, yet powerful for distinguishing mechanisms. The entire165 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, China169 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.04171 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 response179 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 well200 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_determination202 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 path216 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 features225 226 :param tolerance_list: 227 constraints imposed on features, default is null228 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)) / 2239 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)) / 2242 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 null247 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 the277 argument is used, arity=1.278 - 'log' : protected log where the absolute value of the argument is279 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-8292 from TCLR import TCLRalgorithm as model293 294 dataSet = ""testdata.csv""295 correlation = 'PearsonR(+)'296 minsize = 3297 threshold = 0.9298 mininc = 0.01299 split_tol = 0.8300 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 results307 global record308 record = 0309 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_min311 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 batch317 if len(input_csvData) - 1 <= 3:318 batch = 1319 else:320 batch = 3321 # generate new dataset322 for epoch in range(epochs):323 # for increasing the randomness324 random_seed += 1325 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_csvData332 333 334 copy_csvData = copy.deepcopy(csvData)335 copy_csvData['slope'] = None336 copy_csvData['intercept'] = None337 copy_csvData[correlation] = None338 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.columns355 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 = metric363 )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_loc370 dummyfea = []371 for i in range(len(gpl_dummyfea)):372 index = feats.index(gpl_dummyfea[i])373 dummyfea.append(index)374 # remove fea_loc375 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 = metric383 )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 pass415 416 417 try:418 # generate figure in pdf419 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 True426 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 target435# Partitions the feature space into a set of rectangles,436def createTree(dataSet, ori_dataset,feats, leaf_no, correlation,tolerance_list, minsize, threshold, mininc,split_tol,weight):437 # It is a positive linear relationship438 if correlation == 'PearsonR(+)':439 node = Node(dataSet)440 # Initial R0441 bestR = PearsonR(dataSet[:, -2], dataSet[:, -1])442 node.R = bestR443 __slope = stats.linregress(dataSet[:, -2], dataSet[:, -1])[0]444 node.slope = __slope445 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_no448 leaf_no += 1449 write_csv(node, feats, True, correlation)450 return node, leaf_no451 # Leave the last two columns of DataSet, a feature of interest and a response452 numFeatures = len(dataSet[0]) - 2453 splitSuccess = False454 bestFeature = -1455 bestValue = 0456 457 check_valve = False458 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 continue465 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 continue470 471 R = weight_gain(subDataSetA,subDataSetB,weight,0)472 473 if R - bestR >= mininc:474 check_valve = True475 splitSuccess = True476 bestR = R477 lc = subDataSetA478 rc = subDataSetB479 bestFeature = i480 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 continue492 493 R = weight_gain(subDataSetA,subDataSetB,weight,0)494 495 if R - bestR >= mininc:496 splitSuccess = True497 bestR = R498 lc = subDataSetA499 rc = subDataSetB500 bestFeature = i501 bestValue = uniqueVals[value]502 else:503 pass504 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, bestValue511 512 # This node is leaf513 if node.lc is None:514 node.leaf_no = leaf_no515 leaf_no += 1516 # determine if this node is to save in all_dataset.csv517 save_in_all = False518 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_no523 524 # It is a negative linear relationship525 elif correlation == 'PearsonR(-)':526 node = Node(dataSet)527 bestR = PearsonR(dataSet[:, -2], dataSet[:, -1])528 node.R = bestR529 __slope = stats.linregress(dataSet[:, -2], dataSet[:, -1])[0]530 node.slope = __slope531 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_no534 leaf_no += 1535 write_csv(node, feats, True, correlation)536 return node, leaf_no537 538 numFeatures = len(dataSet[0]) - 2539 splitSuccess = False540 bestFeature = -1541 bestValue = 0542 543 check_valve = False544 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 continue551 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 continue556 557 558 R = weight_gain(subDataSetA,subDataSetB,weight,0)559 560 if R - bestR <= - mininc:561 check_valve = True562 splitSuccess = True563 bestR = R564 lc = subDataSetA565 rc = subDataSetB566 bestFeature = i567 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 continue579 580 R = weight_gain(subDataSetA,subDataSetB,weight,0)581 582 if R - bestR <= - mininc:583 splitSuccess = True584 bestR = R585 lc = subDataSetA586 rc = subDataSetB587 bestFeature = i588 bestValue = uniqueVals[value]589 else: pass590 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, bestValue595 596 if node.lc is None:597 node.leaf_no = leaf_no598 leaf_no += 1599 # determine if this node is to save in all_dataset.csv600 save_in_all = False601 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_no606 607 elif correlation == 'MIC':608 node = Node(dataSet)609 bestR = MIC(dataSet[:, -2], dataSet[:, -1])610 node.R = bestR611 node.slope == None612 if bestR >= threshold and fea_tol(dataSet,ori_dataset,feats,tolerance_list) == True:613 node.leaf_no = leaf_no614 leaf_no += 1615 write_csv(node, feats, True, correlation)616 return node, leaf_no617 618 numFeatures = len(dataSet[0]) - 2619 splitSuccess = False620 bestFeature = -1621 bestValue = 0622 623 check_valve = False624 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 continue631 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 continue635 636 R = weight_gain(subDataSetA,subDataSetB,weight,1)637 638 if R - bestR >= mininc:639 check_valve = True640 splitSuccess = True641 bestR = R642 lc = subDataSetA643 rc = subDataSetB644 bestFeature = i645 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 continue656 657 R = weight_gain(subDataSetA,subDataSetB,weight,1)658 659 if R - bestR >= mininc:660 splitSuccess = True661 bestR = R662 lc = subDataSetA663 rc = subDataSetB664 bestFeature = i665 bestValue = uniqueVals[value]666 else: pass667 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, bestValue672 673 if node.lc is None:674 node.leaf_no = leaf_no675 leaf_no += 1676 # determine if this node is to save in all_dataset.csv677 save_in_all = False678 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_no683 684 elif correlation == 'R2':685 node = Node(dataSet)686 bestR = R2(dataSet[:, -2], dataSet[:, -1])687 node.R = bestR688 node.slope == None689 if bestR >= threshold and fea_tol(dataSet,ori_dataset,feats,tolerance_list) == True:690 node.leaf_no = leaf_no691 leaf_no += 1692 write_csv(node, feats, True, correlation)693 return node, leaf_no694 695 numFeatures = len(dataSet[0]) - 2696 splitSuccess = False697 bestFeature = -1698 bestValue = 0699 700 check_valve = False701 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 continue708 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 continue714 715 R = weight_gain(subDataSetA,subDataSetB,weight,2)716 717 if R - bestR >= mininc:718 check_valve = True719 splitSuccess = True720 bestR = R721 lc = subDataSetA722 rc = subDataSetB723 bestFeature = i724 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 continue735 736 R = weight_gain(subDataSetA,subDataSetB,weight,2)737 738 if R - bestR >= mininc:739 splitSuccess = True740 bestR = R741 lc = subDataSetA742 rc = subDataSetB743 bestFeature = i744 bestValue = uniqueVals[value]745 else: pass746 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, bestValue751 752 if node.lc is None:753 node.leaf_no = leaf_no754 leaf_no += 1755 # determine if this node is to save in all_dataset.csv756 save_in_all = False757 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_no762 763 764def PearsonR(X, Y):765 xBar = np.mean(X)766 yBar = np.mean(Y)767 SSR = 0768 varX = 0769 varY = 0770 if len(X) > 1:771 for i in range(0, len(X)):772 diffXXBar = X[i] - xBar773 diffYYBar = Y[i] - yBar774 SSR += (diffXXBar * diffYYBar)775 varX += diffXXBar ** 2776 varY += diffYYBar ** 2777 SST = math.sqrt(varX * varY)778 else:779 SST = 1780 SSR = 0781 if SST == 0:782 return 0783 return SSR / SST784 785 786def 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 = 0793 return MICs794 795 796def R2(X, Y):797 X = np.array(X)798 Y = np.array(Y)799 if len(X) > 0:800 a = (X - np.mean(Y)) ** 2801 SStot = np.sum(a)802 b = (X - Y) ** 2803 SSres = np.sum(b)804 r2 = 1 - SSres / SStot805 return r2806 else:807 r2 = -10808 return r2809 810 811# Split the DataSet in a specific node812def 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 822def fea_tol(dataSet,ori_dataSet,feats,tolerance_list):823 if tolerance_list == None: return True824 else:825 record = 0826 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 += 1832 if record == len(tolerance_list):833 return True834 835def fea_tol_split(dataSet,ori_dataSet,feats,tolerance_list,split_tol):836 if tolerance_list == None: return True837 else:838 record = 0839 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 += 1846 if record > int(0.5*len(tolerance_list)):847 return True848 849 850# Use graphviz to visualize the TCLR851def 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) + mark861 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 870def write_csv(node, feats, save_in_all, correlation):871 global record872 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'] = None880 frame['intercept'] = None881 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.slope885 frame['intercept'] = node.intercept886 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.csv892 _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 return896 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 903def 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) / 2917 return R918 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 * newRb922 return R923 else:924 print('Parameter error | weight')925 926 927# code on 2023 May 9, Bin Cao928def 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 features943 for i in range(num_combinations):944 # randomly chooses two features945 f1 = random.choice(feature_list)946 f2 = random.choice(feature_list)947 948 # choose a operator949 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 name958 new_feature = f'({new_f1} {op} {new_f2})'959 960 new_features.append(new_feature)961 962 # cal new features963 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 TCLR968","Markdown"
969 