How to approximate a row of Pascal’s triangle using symbolic regression /u/MrMrsPotts Python Education

I have been trying out symbolic regression and was wondering how to use it approximate a row of Pascal’s triangle for example. I make the data with:

import math def print_pascals_triangle_row(n): row = [] for k in range(n + 1): coefficient = math.comb(n, k) row.append(coefficient) return row n = 20 pascals_triangle_row = print_pascals_triangle_row(n) 

This gives:

[1, 20, 190, 1140, 4845, 15504, 38760, 77520, 125970, 167960, 184756, 167960, 125970, 77520, 38760, 15504, 4845, 1140, 190, 20, 1] y = pascals_triangle_row X = np.array(range(len(y))).reshape(-1, 1) 

Set up the model:

from pysr import PySRRegressor model = PySRRegressor( maxsize=15, niterations=5000, # < Increase me for better results binary_operators=["+", "*"], unary_operators=[ "log", "exp", "inv", "square", "sqrt", "sign", # ^ Custom operator (julia syntax) ], # ^ Define operator for SymPy as well elementwise_loss="loss(prediction, target) = (prediction - target)^2", # ^ Custom loss function (julia syntax) ) 

And finally fit the model:

model.fit(X, y) 

This doesn’t give a model that is anywhere near accurate. My guess is the fundamental reason is that it can’t do x**x which is need to approximate factorial.

Is there any way to allow symbolic regression to use the binary operator **?

submitted by /u/MrMrsPotts
[link] [comments]

​r/learnpython I have been trying out symbolic regression and was wondering how to use it approximate a row of Pascal’s triangle for example. I make the data with: import math def print_pascals_triangle_row(n): row = [] for k in range(n + 1): coefficient = math.comb(n, k) row.append(coefficient) return row n = 20 pascals_triangle_row = print_pascals_triangle_row(n) This gives: [1, 20, 190, 1140, 4845, 15504, 38760, 77520, 125970, 167960, 184756, 167960, 125970, 77520, 38760, 15504, 4845, 1140, 190, 20, 1] y = pascals_triangle_row X = np.array(range(len(y))).reshape(-1, 1) Set up the model: from pysr import PySRRegressor model = PySRRegressor( maxsize=15, niterations=5000, # < Increase me for better results binary_operators=[“+”, “*”], unary_operators=[ “log”, “exp”, “inv”, “square”, “sqrt”, “sign”, # ^ Custom operator (julia syntax) ], # ^ Define operator for SymPy as well elementwise_loss=”loss(prediction, target) = (prediction – target)^2″, # ^ Custom loss function (julia syntax) ) And finally fit the model: model.fit(X, y) This doesn’t give a model that is anywhere near accurate. My guess is the fundamental reason is that it can’t do x**x which is need to approximate factorial. Is there any way to allow symbolic regression to use the binary operator **? submitted by /u/MrMrsPotts [link] [comments] 

I have been trying out symbolic regression and was wondering how to use it approximate a row of Pascal’s triangle for example. I make the data with:

import math def print_pascals_triangle_row(n): row = [] for k in range(n + 1): coefficient = math.comb(n, k) row.append(coefficient) return row n = 20 pascals_triangle_row = print_pascals_triangle_row(n) 

This gives:

[1, 20, 190, 1140, 4845, 15504, 38760, 77520, 125970, 167960, 184756, 167960, 125970, 77520, 38760, 15504, 4845, 1140, 190, 20, 1] y = pascals_triangle_row X = np.array(range(len(y))).reshape(-1, 1) 

Set up the model:

from pysr import PySRRegressor model = PySRRegressor( maxsize=15, niterations=5000, # < Increase me for better results binary_operators=["+", "*"], unary_operators=[ "log", "exp", "inv", "square", "sqrt", "sign", # ^ Custom operator (julia syntax) ], # ^ Define operator for SymPy as well elementwise_loss="loss(prediction, target) = (prediction - target)^2", # ^ Custom loss function (julia syntax) ) 

And finally fit the model:

model.fit(X, y) 

This doesn’t give a model that is anywhere near accurate. My guess is the fundamental reason is that it can’t do x**x which is need to approximate factorial.

Is there any way to allow symbolic regression to use the binary operator **?

submitted by /u/MrMrsPotts
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *