```python
def symbolize(s):
    """
    Converts a a string (equation) to a SymPy symbol object
    """
    from sympy import sympify
    s1=s.replace('.','*')
    s2=s1.replace('^','**')
    s3=sympify(s2)
    
    return(s3)
```


```python
def eval_multinomial(s,vals=None,symbolic_eval=False):
    """
    Evaluates polynomial at vals.
    vals can be simple list, dictionary, or tuple of values.
    vals can also contain symbols instead of real values provided those symbols have been declared before using SymPy
    """
    from sympy import Symbol
    sym_s=symbolize(s)
    sym_set=sym_s.atoms(Symbol)
    sym_lst=[]
    for s in sym_set:
        sym_lst.append(str(s))
    sym_lst.sort()
    if symbolic_eval==False and len(sym_set)!=len(vals):
        print("Length of the input values did not match number of variables and symbolic evaluation is not selected")
        return None
    else:
        if type(vals)==list:
            sub=list(zip(sym_lst,vals))
        elif type(vals)==dict:
            l=list(vals.keys())
            l.sort()
            lst=[]
            for i in l:
                lst.append(vals[i])
            sub=list(zip(sym_lst,lst))
        elif type(vals)==tuple:
            sub=list(zip(sym_lst,list(vals)))
        result=sym_s.subs(sub)
    
    return result
```


```python
def flip(y,p):
    import numpy as np
    lst=[]
    for i in range(len(y)):
        f=np.random.choice([1,0],p=[p,1-p])
        lst.append(f)
    lst=np.array(lst)
    return np.array(np.logical_xor(y,lst),dtype=int)
```


```python
def gen_classification_symbolic(m=None,n_samples=100,n_features=2,flip_y=0.0):
    """
    Generates classification sample based on a symbolic expression.
    Calculates the output of the symbolic expression at randomly generated (Gaussian distribution) points and
    assigns binary classification based on sign.
    m: The symbolic expression. Needs x1, x2, etc as variables and regular python arithmatic symbols to be used.
    n_samples: Number of samples to be generated
    n_features: Number of variables. This is automatically inferred from the symbolic expression. So this is ignored 
                in case a symbolic expression is supplied. However if no symbolic expression is supplied then a 
                default simple polynomial can be invoked to generate classification samples with n_features.
    flip_y: Probability of flipping the classification labels randomly. A higher value introduces more noise and make
            the classification problem harder.
    Returns a numpy ndarray with dimension (n_samples,n_features+1). Last column is the response vector.
    """
    
    import numpy as np
    from sympy import Symbol,sympify
    
    if m==None:
        m=''
        for i in range(1,n_features+1):
            c='x'+str(i)
            c+=np.random.choice(['+','-'],p=[0.5,0.5])
            m+=c
        m=m[:-1]
    sym_m=sympify(m)
    n_features=len(sym_m.atoms(Symbol))
    evals=[]
    lst_features=[]
    for i in range(n_features):
        lst_features.append(np.random.normal(scale=5,size=n_samples))
    lst_features=np.array(lst_features)
    lst_features=lst_features.T
    for i in range(n_samples):
        evals.append(eval_multinomial(m,vals=list(lst_features[i])))
    
    evals=np.array(evals)
    evals_binary=evals>0
    evals_binary=evals_binary.flatten()
    evals_binary=np.array(evals_binary,dtype=int)
    evals_binary=flip(evals_binary,p=flip_y)
    evals_binary=evals_binary.reshape(n_samples,1)
    
    lst_features=lst_features.reshape(n_samples,n_features)
    x=np.hstack((lst_features,evals_binary))
    
    return (x)
```


```python
x=gen_classification_symbolic(m='2*x1+3*x2+5*x3',n_samples=10,flip_y=0.0)
```


```python
import pandas as pd
df=pd.DataFrame(x)
```


```python
df
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1.993945</td>
      <td>1.116260</td>
      <td>2.316286</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1.230979</td>
      <td>4.672876</td>
      <td>9.164157</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-6.900166</td>
      <td>-4.564144</td>
      <td>0.344308</td>
      <td>0.0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>1.556408</td>
      <td>3.551907</td>
      <td>7.105238</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>4</th>
      <td>-3.528923</td>
      <td>7.970909</td>
      <td>-3.765006</td>
      <td>0.0</td>
    </tr>
    <tr>
      <th>5</th>
      <td>2.237070</td>
      <td>3.920372</td>
      <td>2.548449</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>6</th>
      <td>-4.423419</td>
      <td>-0.609899</td>
      <td>-8.466901</td>
      <td>0.0</td>
    </tr>
    <tr>
      <th>7</th>
      <td>5.475675</td>
      <td>-7.311347</td>
      <td>3.444522</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>8</th>
      <td>7.752732</td>
      <td>-0.346992</td>
      <td>1.162572</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>9</th>
      <td>3.104909</td>
      <td>4.527720</td>
      <td>-4.397039</td>
      <td>0.0</td>
    </tr>
  </tbody>
</table>
</div>




```python
x=gen_classification_symbolic(m='12*x1/(x2+5*x3)',n_samples=10,flip_y=0.2)
df=pd.DataFrame(x)
df
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1.192160</td>
      <td>-0.633254</td>
      <td>-0.530373</td>
      <td>0.0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1.838186</td>
      <td>-1.355400</td>
      <td>5.590450</td>
      <td>0.0</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-7.882258</td>
      <td>3.829910</td>
      <td>-5.454632</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.475023</td>
      <td>0.827497</td>
      <td>3.000924</td>
      <td>0.0</td>
    </tr>
    <tr>
      <th>4</th>
      <td>1.386086</td>
      <td>3.123963</td>
      <td>-12.426151</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>5</th>
      <td>-11.121159</td>
      <td>-3.388428</td>
      <td>-5.406741</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>6</th>
      <td>2.322904</td>
      <td>-4.708977</td>
      <td>3.414145</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>7</th>
      <td>-4.734882</td>
      <td>-3.354211</td>
      <td>-2.332581</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>8</th>
      <td>0.586311</td>
      <td>-2.557310</td>
      <td>7.387032</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>9</th>
      <td>-0.531481</td>
      <td>10.494158</td>
      <td>-2.378411</td>
      <td>0.0</td>
    </tr>
  </tbody>
</table>
</div>



31ca1b77308749467c4d60f6ab48e79035d8af54
ipynb
Jupyter Notebook
Symbolic regression classification generator.ipynb
benvictoria17/Machinelearning
0586d70c5cbec90675b87c57ca062e33b3776b85
MIT
Symbolic regression classification generator.ipynb
benvictoria17/Machinelearning
0586d70c5cbec90675b87c57ca062e33b3776b85
MIT
Symbolic regression classification generator.ipynb
benvictoria17/Machinelearning
0586d70c5cbec90675b87c57ca062e33b3776b85
MIT
Qwen/Qwen-72B
1. YES
2. YES