pyscinloopsr.sr

Module to fit PySR and perform analysis on the results

  1"""
  2Module to fit PySR and perform analysis on the results
  3"""
  4
  5from pysr import PySRRegressor
  6import joblib
  7from datetime import datetime
  8import pandas as pd
  9import numpy as np
 10from scipy import stats
 11import seaborn as snsi
 12import matplotlib.pyplot as plt
 13from sklearn.inspection import PartialDependenceDisplay
 14from matplotlib.pylab import rcParams
 15rcParams["figure.figsize"] = 15,6
 16plt.figure(figsize=(15, 10))
 17
 18
 19class pysr:
 20    """Class for handling PySR interactions"""
 21
 22    def __init__(self, modelObjtRef):
 23        """
 24        Initiate the object with your PySRRegressor defined object\n
 25        Example:\n
 26            from pyscinloopsr import sr\n
 27            pysrHandlerObject = sr.pysr(PySRRegressorObject)
 28        """
 29        self.model = modelObjtRef
 30        """Internal model reference"""
 31        self.dfxn = None
 32        """Internal predictors Dataframe name"""
 33        self.dfyn = None
 34        """Internal target Dataframe name"""
 35
 36
 37    def runPySR(self, dfx, dfy, featuresDrop=None):
 38        """
 39        Method to fit the PySR model\n
 40        Args: predictors DataFrame, target DataFrame, optional list of features names to drop\n
 41        Example:\n
 42            pysrHandlerObject.runPySR(X, y)\n
 43        Example:\n
 44            pysrHandlerObject.runPySR(X, y, ["featureNameOne", "featureNameTwo"])    
 45        """       
 46        self.dfxn = dfx.copy()
 47        self.dfyn = dfy.copy()
 48        if featuresDrop is not None:
 49            for i in featuresDrop:
 50                print("\ndropping: ", i)
 51                self.dfxn = self.dfxn.drop(i, axis=1)
 52
 53
 54        self.model.fit(self.dfxn, self.dfyn.values.ravel())
 55    
 56
 57    def exportModel(self, directory):
 58        """
 59        Method to export the fitted PySR model\n
 60        Args: directory to place the file\n
 61        File name is set automatically as YYYYMMDDHHMISS.joblib (e.g. 20240427124411.joblib)\n
 62        Example:\n
 63            pysrHandlerObject.exportModel("/content/")
 64        """        
 65        now = datetime.now()
 66        time = now.strftime("%Y%m%d%H%M%S")
 67        joblib.dump(self.model, directory + time + ".joblib")
 68 
 69    def importModel(self, fullPath):
 70        """
 71        Method to import a PySR model previously exported\n
 72        Args: full directory with file name to recover the file\n
 73        File name is set automatically as YYYYMMDDHHMISS.joblib (e.g. 20240427124411.joblib)\n
 74        Example:\n
 75            pysrHandlerObject.importModel("/content/20240427124411.joblib")
 76        """           
 77        self.model = joblib.load(fullPath)
 78
 79    def setModelToAccuracy(self):
 80        """
 81        Method to change PySR mode to accuracy\n
 82        Example:\n
 83            pysrHandlerObject.setModelToBest()
 84        """
 85        self.model.model_selection = "accuracy"
 86
 87    def setModelToBest(self):
 88        """
 89        Method to change PySR mode to best\n
 90        Example:\n
 91            pysrHandlerObject.setModelToBest()
 92        """        
 93        self.model.model_selection = "best"
 94
 95    def printEquationFormats(self):
 96        """
 97        Method to print the generated equation in three formats (equation, latex, sympy)\n
 98        Example:\n
 99            pysrHandlerObject.printEquationFormats()
100        """          
101        print("equation:\n", self.model.get_best().equation, "\n")
102        print("latex:\n", self.model.latex(), "\n") 
103        print("sympy:\n", self.model.sympy(), "\n")   
104
105    def score(self):
106        """
107        Method to print the model score\n
108        Example:\n
109            pysrHandlerObject.score()
110        """            
111        print(self.model.score(self.dfxn, self.dfyn))
112
113    def compare(self):
114        """
115        Method to scatterplot the True target value against model Prediction\n
116        Example:\n
117            pysrHandlerObject.compare()
118        """           
119        plt.figure(figsize=(5, 5))
120        plt.scatter(self.dfyn, self.model.predict(self.dfxn))
121        plt.xlabel('Truth')
122        plt.ylabel('Prediction')
123        plt.show()
124
125    def compareKde(self):
126        """
127        Method to plot KDE of True target value against model Prediction\n
128        Example:\n
129            pysrHandlerObject.compareKde()
130        """         
131        rcParams["figure.figsize"] = 8,8
132        plt.figure(figsize=(8, 8))
133        realXpredicted = self.dfyn.copy()
134        realXpredicted.columns = ["target"]
135        realXpredicted["predicted"] = self.model.predict(self.dfxn)
136        minAux = realXpredicted.target.min()
137        min = minAux
138        minAux = realXpredicted.predicted.min()
139        if minAux<min:
140            min=minAux
141        maxAux = realXpredicted.target.max()
142        max = maxAux
143        maxAux = realXpredicted.predicted.max()
144        if maxAux>max:
145            max=maxAux
146        xAxis = np.linspace(min, max, num=200)
147        ax = realXpredicted.plot.kde(ind=xAxis)
148        plt.xlabel("Values")
149        plt.ylabel("Density")
150        plt.grid(False)
151        plt.show()
152
153
154    def predictCase(self, example):
155        """
156        Method to predict an instance with user defined values for probing the mode\n
157        Example:\n
158            pysrHandlerObject.predictCase([1.67, 0.9, -0.77, -1.7, -0.6, 0.03, 1.04, -0.91,	1.42, -0.91])
159        """          
160        print(self.model.predict(np.array(example).reshape(1, -1)))     
161
162
163    def pdp(self, featureList=None):
164        """
165        Method to print PDP\n
166        Args: Optional list of features names to plot\n
167        Example (all features):\n
168            pysrHandlerObject.pdp()\n
169        Example (specified features):\n
170            pysrHandlerObject.pdp(["featureNameOne", "featureNameTwo", "featureNameThree"])    
171        """       
172        if featureList is None:
173            allFeatures = list(range(self.dfxn.shape[1]))
174        else:
175            allFeatures = featureList
176        fig, ax = plt.subplots(figsize=(10, 20))
177        ax.set_title("Partial Dependence Plots")
178        PartialDependenceDisplay.from_estimator(
179            estimator=self.model,
180            X=self.dfxn,
181            features=(allFeatures),
182            random_state=42,
183            ax=ax
184        )
185        plt.show()         
class pysr:
 20class pysr:
 21    """Class for handling PySR interactions"""
 22
 23    def __init__(self, modelObjtRef):
 24        """
 25        Initiate the object with your PySRRegressor defined object\n
 26        Example:\n
 27            from pyscinloopsr import sr\n
 28            pysrHandlerObject = sr.pysr(PySRRegressorObject)
 29        """
 30        self.model = modelObjtRef
 31        """Internal model reference"""
 32        self.dfxn = None
 33        """Internal predictors Dataframe name"""
 34        self.dfyn = None
 35        """Internal target Dataframe name"""
 36
 37
 38    def runPySR(self, dfx, dfy, featuresDrop=None):
 39        """
 40        Method to fit the PySR model\n
 41        Args: predictors DataFrame, target DataFrame, optional list of features names to drop\n
 42        Example:\n
 43            pysrHandlerObject.runPySR(X, y)\n
 44        Example:\n
 45            pysrHandlerObject.runPySR(X, y, ["featureNameOne", "featureNameTwo"])    
 46        """       
 47        self.dfxn = dfx.copy()
 48        self.dfyn = dfy.copy()
 49        if featuresDrop is not None:
 50            for i in featuresDrop:
 51                print("\ndropping: ", i)
 52                self.dfxn = self.dfxn.drop(i, axis=1)
 53
 54
 55        self.model.fit(self.dfxn, self.dfyn.values.ravel())
 56    
 57
 58    def exportModel(self, directory):
 59        """
 60        Method to export the fitted PySR model\n
 61        Args: directory to place the file\n
 62        File name is set automatically as YYYYMMDDHHMISS.joblib (e.g. 20240427124411.joblib)\n
 63        Example:\n
 64            pysrHandlerObject.exportModel("/content/")
 65        """        
 66        now = datetime.now()
 67        time = now.strftime("%Y%m%d%H%M%S")
 68        joblib.dump(self.model, directory + time + ".joblib")
 69 
 70    def importModel(self, fullPath):
 71        """
 72        Method to import a PySR model previously exported\n
 73        Args: full directory with file name to recover the file\n
 74        File name is set automatically as YYYYMMDDHHMISS.joblib (e.g. 20240427124411.joblib)\n
 75        Example:\n
 76            pysrHandlerObject.importModel("/content/20240427124411.joblib")
 77        """           
 78        self.model = joblib.load(fullPath)
 79
 80    def setModelToAccuracy(self):
 81        """
 82        Method to change PySR mode to accuracy\n
 83        Example:\n
 84            pysrHandlerObject.setModelToBest()
 85        """
 86        self.model.model_selection = "accuracy"
 87
 88    def setModelToBest(self):
 89        """
 90        Method to change PySR mode to best\n
 91        Example:\n
 92            pysrHandlerObject.setModelToBest()
 93        """        
 94        self.model.model_selection = "best"
 95
 96    def printEquationFormats(self):
 97        """
 98        Method to print the generated equation in three formats (equation, latex, sympy)\n
 99        Example:\n
100            pysrHandlerObject.printEquationFormats()
101        """          
102        print("equation:\n", self.model.get_best().equation, "\n")
103        print("latex:\n", self.model.latex(), "\n") 
104        print("sympy:\n", self.model.sympy(), "\n")   
105
106    def score(self):
107        """
108        Method to print the model score\n
109        Example:\n
110            pysrHandlerObject.score()
111        """            
112        print(self.model.score(self.dfxn, self.dfyn))
113
114    def compare(self):
115        """
116        Method to scatterplot the True target value against model Prediction\n
117        Example:\n
118            pysrHandlerObject.compare()
119        """           
120        plt.figure(figsize=(5, 5))
121        plt.scatter(self.dfyn, self.model.predict(self.dfxn))
122        plt.xlabel('Truth')
123        plt.ylabel('Prediction')
124        plt.show()
125
126    def compareKde(self):
127        """
128        Method to plot KDE of True target value against model Prediction\n
129        Example:\n
130            pysrHandlerObject.compareKde()
131        """         
132        rcParams["figure.figsize"] = 8,8
133        plt.figure(figsize=(8, 8))
134        realXpredicted = self.dfyn.copy()
135        realXpredicted.columns = ["target"]
136        realXpredicted["predicted"] = self.model.predict(self.dfxn)
137        minAux = realXpredicted.target.min()
138        min = minAux
139        minAux = realXpredicted.predicted.min()
140        if minAux<min:
141            min=minAux
142        maxAux = realXpredicted.target.max()
143        max = maxAux
144        maxAux = realXpredicted.predicted.max()
145        if maxAux>max:
146            max=maxAux
147        xAxis = np.linspace(min, max, num=200)
148        ax = realXpredicted.plot.kde(ind=xAxis)
149        plt.xlabel("Values")
150        plt.ylabel("Density")
151        plt.grid(False)
152        plt.show()
153
154
155    def predictCase(self, example):
156        """
157        Method to predict an instance with user defined values for probing the mode\n
158        Example:\n
159            pysrHandlerObject.predictCase([1.67, 0.9, -0.77, -1.7, -0.6, 0.03, 1.04, -0.91,	1.42, -0.91])
160        """          
161        print(self.model.predict(np.array(example).reshape(1, -1)))     
162
163
164    def pdp(self, featureList=None):
165        """
166        Method to print PDP\n
167        Args: Optional list of features names to plot\n
168        Example (all features):\n
169            pysrHandlerObject.pdp()\n
170        Example (specified features):\n
171            pysrHandlerObject.pdp(["featureNameOne", "featureNameTwo", "featureNameThree"])    
172        """       
173        if featureList is None:
174            allFeatures = list(range(self.dfxn.shape[1]))
175        else:
176            allFeatures = featureList
177        fig, ax = plt.subplots(figsize=(10, 20))
178        ax.set_title("Partial Dependence Plots")
179        PartialDependenceDisplay.from_estimator(
180            estimator=self.model,
181            X=self.dfxn,
182            features=(allFeatures),
183            random_state=42,
184            ax=ax
185        )
186        plt.show()         

Class for handling PySR interactions

pysr(modelObjtRef)
23    def __init__(self, modelObjtRef):
24        """
25        Initiate the object with your PySRRegressor defined object\n
26        Example:\n
27            from pyscinloopsr import sr\n
28            pysrHandlerObject = sr.pysr(PySRRegressorObject)
29        """
30        self.model = modelObjtRef
31        """Internal model reference"""
32        self.dfxn = None
33        """Internal predictors Dataframe name"""
34        self.dfyn = None
35        """Internal target Dataframe name"""

Initiate the object with your PySRRegressor defined object

Example:

from pyscinloopsr import sr

pysrHandlerObject = sr.pysr(PySRRegressorObject)
model

Internal model reference

dfxn

Internal predictors Dataframe name

dfyn

Internal target Dataframe name

def runPySR(self, dfx, dfy, featuresDrop=None):
38    def runPySR(self, dfx, dfy, featuresDrop=None):
39        """
40        Method to fit the PySR model\n
41        Args: predictors DataFrame, target DataFrame, optional list of features names to drop\n
42        Example:\n
43            pysrHandlerObject.runPySR(X, y)\n
44        Example:\n
45            pysrHandlerObject.runPySR(X, y, ["featureNameOne", "featureNameTwo"])    
46        """       
47        self.dfxn = dfx.copy()
48        self.dfyn = dfy.copy()
49        if featuresDrop is not None:
50            for i in featuresDrop:
51                print("\ndropping: ", i)
52                self.dfxn = self.dfxn.drop(i, axis=1)
53
54
55        self.model.fit(self.dfxn, self.dfyn.values.ravel())

Method to fit the PySR model

Args: predictors DataFrame, target DataFrame, optional list of features names to drop

Example:

pysrHandlerObject.runPySR(X, y)

Example:

pysrHandlerObject.runPySR(X, y, ["featureNameOne", "featureNameTwo"])
def exportModel(self, directory):
58    def exportModel(self, directory):
59        """
60        Method to export the fitted PySR model\n
61        Args: directory to place the file\n
62        File name is set automatically as YYYYMMDDHHMISS.joblib (e.g. 20240427124411.joblib)\n
63        Example:\n
64            pysrHandlerObject.exportModel("/content/")
65        """        
66        now = datetime.now()
67        time = now.strftime("%Y%m%d%H%M%S")
68        joblib.dump(self.model, directory + time + ".joblib")

Method to export the fitted PySR model

Args: directory to place the file

File name is set automatically as YYYYMMDDHHMISS.joblib (e.g. 20240427124411.joblib)

Example:

pysrHandlerObject.exportModel("/content/")
def importModel(self, fullPath):
70    def importModel(self, fullPath):
71        """
72        Method to import a PySR model previously exported\n
73        Args: full directory with file name to recover the file\n
74        File name is set automatically as YYYYMMDDHHMISS.joblib (e.g. 20240427124411.joblib)\n
75        Example:\n
76            pysrHandlerObject.importModel("/content/20240427124411.joblib")
77        """           
78        self.model = joblib.load(fullPath)

Method to import a PySR model previously exported

Args: full directory with file name to recover the file

File name is set automatically as YYYYMMDDHHMISS.joblib (e.g. 20240427124411.joblib)

Example:

pysrHandlerObject.importModel("/content/20240427124411.joblib")
def setModelToAccuracy(self):
80    def setModelToAccuracy(self):
81        """
82        Method to change PySR mode to accuracy\n
83        Example:\n
84            pysrHandlerObject.setModelToBest()
85        """
86        self.model.model_selection = "accuracy"

Method to change PySR mode to accuracy

Example:

pysrHandlerObject.setModelToBest()
def setModelToBest(self):
88    def setModelToBest(self):
89        """
90        Method to change PySR mode to best\n
91        Example:\n
92            pysrHandlerObject.setModelToBest()
93        """        
94        self.model.model_selection = "best"

Method to change PySR mode to best

Example:

pysrHandlerObject.setModelToBest()
def printEquationFormats(self):
 96    def printEquationFormats(self):
 97        """
 98        Method to print the generated equation in three formats (equation, latex, sympy)\n
 99        Example:\n
100            pysrHandlerObject.printEquationFormats()
101        """          
102        print("equation:\n", self.model.get_best().equation, "\n")
103        print("latex:\n", self.model.latex(), "\n") 
104        print("sympy:\n", self.model.sympy(), "\n")   

Method to print the generated equation in three formats (equation, latex, sympy)

Example:

pysrHandlerObject.printEquationFormats()
def score(self):
106    def score(self):
107        """
108        Method to print the model score\n
109        Example:\n
110            pysrHandlerObject.score()
111        """            
112        print(self.model.score(self.dfxn, self.dfyn))

Method to print the model score

Example:

pysrHandlerObject.score()
def compare(self):
114    def compare(self):
115        """
116        Method to scatterplot the True target value against model Prediction\n
117        Example:\n
118            pysrHandlerObject.compare()
119        """           
120        plt.figure(figsize=(5, 5))
121        plt.scatter(self.dfyn, self.model.predict(self.dfxn))
122        plt.xlabel('Truth')
123        plt.ylabel('Prediction')
124        plt.show()

Method to scatterplot the True target value against model Prediction

Example:

pysrHandlerObject.compare()
def compareKde(self):
126    def compareKde(self):
127        """
128        Method to plot KDE of True target value against model Prediction\n
129        Example:\n
130            pysrHandlerObject.compareKde()
131        """         
132        rcParams["figure.figsize"] = 8,8
133        plt.figure(figsize=(8, 8))
134        realXpredicted = self.dfyn.copy()
135        realXpredicted.columns = ["target"]
136        realXpredicted["predicted"] = self.model.predict(self.dfxn)
137        minAux = realXpredicted.target.min()
138        min = minAux
139        minAux = realXpredicted.predicted.min()
140        if minAux<min:
141            min=minAux
142        maxAux = realXpredicted.target.max()
143        max = maxAux
144        maxAux = realXpredicted.predicted.max()
145        if maxAux>max:
146            max=maxAux
147        xAxis = np.linspace(min, max, num=200)
148        ax = realXpredicted.plot.kde(ind=xAxis)
149        plt.xlabel("Values")
150        plt.ylabel("Density")
151        plt.grid(False)
152        plt.show()

Method to plot KDE of True target value against model Prediction

Example:

pysrHandlerObject.compareKde()
def predictCase(self, example):
155    def predictCase(self, example):
156        """
157        Method to predict an instance with user defined values for probing the mode\n
158        Example:\n
159            pysrHandlerObject.predictCase([1.67, 0.9, -0.77, -1.7, -0.6, 0.03, 1.04, -0.91,	1.42, -0.91])
160        """          
161        print(self.model.predict(np.array(example).reshape(1, -1)))     

Method to predict an instance with user defined values for probing the mode

Example:

pysrHandlerObject.predictCase([1.67, 0.9, -0.77, -1.7, -0.6, 0.03, 1.04, -0.91,     1.42, -0.91])
def pdp(self, featureList=None):
164    def pdp(self, featureList=None):
165        """
166        Method to print PDP\n
167        Args: Optional list of features names to plot\n
168        Example (all features):\n
169            pysrHandlerObject.pdp()\n
170        Example (specified features):\n
171            pysrHandlerObject.pdp(["featureNameOne", "featureNameTwo", "featureNameThree"])    
172        """       
173        if featureList is None:
174            allFeatures = list(range(self.dfxn.shape[1]))
175        else:
176            allFeatures = featureList
177        fig, ax = plt.subplots(figsize=(10, 20))
178        ax.set_title("Partial Dependence Plots")
179        PartialDependenceDisplay.from_estimator(
180            estimator=self.model,
181            X=self.dfxn,
182            features=(allFeatures),
183            random_state=42,
184            ax=ax
185        )
186        plt.show()         

Method to print PDP

Args: Optional list of features names to plot

Example (all features):

pysrHandlerObject.pdp()

Example (specified features):

pysrHandlerObject.pdp(["featureNameOne", "featureNameTwo", "featureNameThree"])