<jupyter_start><jupyter_text>Libraries<jupyter_code>import pandas as pd
import itertools<jupyter_output><empty_output><jupyter_text>Read Data<jupyter_code>df = pd.read_excel("../data/example_3dim.xlsx")
df.head(6)<jupyter_output><empty_output><jupyter_text>Derivatives<jupyter_code># Basic Definitions
featuresArray = list(df.columns)[:-1]
TargetArray = list(df.columns)[-1]
# Get Range of Data
featuresMaxArray = df[featuresArray].max()
featuresMinArray = df[featuresArray].min()
#Complete Range
featuresRangeArray = []
for featuresRangeArrayIte in range(len(featuresMaxArray)):
    featuresRangeArray.append(range(featuresMinArray[featuresRangeArrayIte],featuresMaxArray[featuresRangeArrayIte]+1))
#Get Range Array
allRangeArray = []
for i in featuresRangeArray:
    allRangeArray.append(list(i))
#Create Data Frame Without Empty Spaces
dfT = pd.DataFrame(itertools.product(*allRangeArray),columns=featuresArray)
dfT = pd.merge(left=dfT,right=df,on=featuresArray,how='left')
dfT.head(6)
def catchByFilter(df,fil):
    #Query
    qry = ' and '.join(["{} == {}".format(k,v) for k,v in fil.items()])   
    #Return
    return(df.query(qry).reset_index())
step = 1
listDictsGradient = []
for gradientFeature in featuresArray:
    
    #Cada Linha do DF
    print("Gradient "+gradientFeature+" --> "+"G_"+gradientFeature)
    for eachRow in range(len(dfT)):
        
        #Filters
        ##Point
        filterPoint = dfT.iloc[eachRow,:][featuresArray]
        ##Before:
        filterBefore = filterPoint.copy()
        filterBefore[gradientFeature] -= step
        #Filtros
        filterAfter = filterPoint.copy()
        filterAfter[gradientFeature] += step
        
        
        #Catch Data
        dfP = catchByFilter(df=dfT,fil=filterPoint)
        dfB = catchByFilter(df=dfT,fil=filterBefore)
        dfA = catchByFilter(df=dfT,fil=filterAfter)
        
        #Denominator
        if (len(dfB)+len(dfP)+len(dfA)) == 3:
            denominator = 2
        else:
            denominator = 1
        
        #Gradient
        if len(dfB) == 0:
            gradient = (dfA[TargetArray] - dfP[TargetArray])/denominator
        elif len(dfA) == 0:
            gradient = (dfP[TargetArray] - dfB[TargetArray])/denominator
        elif (len(dfB)+len(dfA)) == 2:
            gradient = (dfA[TargetArray] - dfB[TargetArray])/denominator
            
        #Insert Gradient
        outDict = dfP.to_dict(orient='records')[0]
        ##Remove Index
        del outDict['index'] 
        ##Add Gradient
        outDict[('G_'+gradientFeature)]=gradient[0]
        ##Apend
        listDictsGradient.append(outDict)
       
#Join All Data
dfDict = pd.DataFrame(listDictsGradient)
dfDict
#Group All Data
dfTF = dfT.copy()
for iGradient in featuresArray:
    filterGradient = ("G_"+iGradient)
    dfDictFilterGradient = dfDict.loc[~dfDict[filterGradient].isnull(),(featuresArray+[TargetArray,filterGradient])]
    dfTF = pd.merge(left=dfTF,right=dfDictFilterGradient,how='left',on=featuresArray+[TargetArray])
dfTF<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12746386333771353, 'fraction_numerical': 0.00492772667542707, 'mean_word_length': 3.599697885196375, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '588840', 'license': 'MIT', 'n_tokens_mistral': 1017, 'n_tokens_neox': 990, 'n_words': 167, 'path': 'notebooks/Gradient.ipynb', 'repo_name': 'ThiagueraBarao/MeltedDerivatives', 'size': '3044'}