Skip to content

Commit 86e529b

Browse files
authored
Cleaned up snippets from notebook version
1 parent 90b8783 commit 86e529b

File tree

1 file changed

+6
-22
lines changed

1 file changed

+6
-22
lines changed

Synthetic_data_generation/Symbolic_regression_classification_generator.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1-
2-
# coding: utf-8
3-
4-
# Evaluate a polynomial string
1+
from sympy import sympify, Symbol
2+
import numpy as np
53

64
def symbolize(s):
75
"""
86
Converts a a string (equation) to a SymPy symbol object
97
"""
10-
from sympy import sympify
118
s1=s.replace('.','*')
129
s2=s1.replace('^','**')
1310
s3=sympify(s2)
1411

1512
return(s3)
1613

17-
1814
def eval_multinomial(s,vals=None,symbolic_eval=False):
1915
"""
2016
Evaluates polynomial at vals.
2117
vals can be simple list, dictionary, or tuple of values.
2218
vals can also contain symbols instead of real values provided those symbols have been declared before using SymPy
2319
"""
24-
from sympy import Symbol
2520
sym_s=symbolize(s)
2621
sym_set=sym_s.atoms(Symbol)
2722
sym_lst=[]
@@ -47,11 +42,10 @@ def eval_multinomial(s,vals=None,symbolic_eval=False):
4742

4843
return result
4944

50-
51-
# ### Helper function for flipping binary values of a _ndarray_
52-
5345
def flip(y,p):
54-
import numpy as np
46+
"""
47+
Flips random bit (used to make a classification problem haredr)
48+
"""
5549
lst=[]
5650
for i in range(len(y)):
5751
f=np.random.choice([1,0],p=[p,1-p])
@@ -60,8 +54,6 @@ def flip(y,p):
6054
return np.array(np.logical_xor(y,lst),dtype=int)
6155

6256

63-
# ### Classification sample generation based on a symbolic expression
64-
6557
def gen_classification_symbolic(m=None,n_samples=100,n_features=2,flip_y=0.0):
6658
"""
6759
Generates classification sample based on a symbolic expression.
@@ -77,9 +69,6 @@ def gen_classification_symbolic(m=None,n_samples=100,n_features=2,flip_y=0.0):
7769
Returns a numpy ndarray with dimension (n_samples,n_features+1). Last column is the response vector.
7870
"""
7971

80-
import numpy as np
81-
from sympy import Symbol,sympify
82-
8372
if m==None:
8473
m=''
8574
for i in range(1,n_features+1):
@@ -110,8 +99,6 @@ def gen_classification_symbolic(m=None,n_samples=100,n_features=2,flip_y=0.0):
11099

111100
return (x)
112101

113-
# ### Regression sample generation based on a symbolic expression
114-
115102

116103
def gen_regression_symbolic(m=None,n_samples=100,n_features=2,noise=0.0,noise_dist='normal'):
117104
"""
@@ -129,9 +116,6 @@ def gen_regression_symbolic(m=None,n_samples=100,n_features=2,noise=0.0,noise_di
129116
Returns a numpy ndarray with dimension (n_samples,n_features+1). Last column is the response vector.
130117
"""
131118

132-
import numpy as np
133-
from sympy import Symbol,sympify
134-
135119
if m==None:
136120
m=''
137121
for i in range(1,n_features+1):
@@ -173,4 +157,4 @@ def gen_regression_symbolic(m=None,n_samples=100,n_features=2,noise=0.0,noise_di
173157

174158
x=np.hstack((lst_features,evals))
175159

176-
return (x)
160+
return (x)

0 commit comments

Comments
 (0)