<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>
StarcoderdataJupyterStructuredCleanDedup