```python
import numpy as np
from IPython.display import Image
from IPython.core.display import HTML
from pathlib import Path
folder = Path('C:/Users/champ/Python_proj/ML_training_repo/Fundamentals/meta_data/')

class Perceptron(object):

    def __init__(self, no_of_inputs, threshold=100, learning_rate=0.01):
        self.threshold = threshold
        self.learning_rate = learning_rate
        self.weights = np.zeros(no_of_inputs + 1)
           
    def predict(self, inputs):
        summation = np.dot(inputs, self.weights[1:]) + self.weights[0]
        if summation > 0:
          activation = 1
        else:
          activation = 0            
        return activation

    def train(self, training_inputs, labels):
        for _ in range(self.threshold):
            for inputs, label in zip(training_inputs, labels):
                prediction = self.predict(inputs)
                self.weights[1:] += self.learning_rate * (label - prediction) * inputs
                self.weights[0] += self.learning_rate * (label - prediction)
```


```python
'''
To program a linear perceptron model this diagram I will use as my guide 
'''
Image(filename = folder / "Perceptron_diagram_detail4.png", width=800, height=800)
```

$\begin{equation}\;\;General\,Perceptron\,model\;=:f(x)=\left\{\begin{matrix}
1 & if \, w\cdot x > 0\\ 0 &\, otherwise \end{matrix}\right.\end{equation}$

$\begin{equation}\,\,weighted\,\,sum\,\,=:\,w_T\,\cdot x\,=x_1\,*w_1\,+x_2\,*\,w_2\,+\,x_3\,*\,w_3\,...\,x_d\,*w_d\end{equation}$

$\begin{equation}\,error\,\,=:\,y\,\,-\,f(x))\end{equation}$

$\begin{equation}\,\,w' = w + {\color{Red} \alpha} \,(y - f(x))\,x\end{equation}$


$\begin{equation}\,\,Update\,equation\,=:\,w'\,=w\,+\,error\,=\,w\,+\,(y-f(x))\,x\end{equation}$


```python
def update_weights():
    '''
    This updates the weights in a linear perceptron
    '''
def perceived_value():
    '''
    This function obtains the perceptron f(x) value
    '''
def generate_data(N, line_coef, line_intercept, random2_seed, random2_seed_nr):
    '''
    Generate random data given number of points, a line coefficient and intercept. 
    Generated points will be in +1/-1 class regarding their relative position to the line.
    
    Inputs:
    - N: number of points to be generated
    - line_coef: line's coefficient
    - line_intercept: line's intercept
    
    Outputs: a tuple of 2 variables:
    - X: positions of generated points
    - y: classes of points (+1/-1)
    '''
    if random2_seed:
        r = np.random.RandomState(random2_seed_nr)
        X = r.uniform(-1, 1, [N, 2])
    else:
        X = np.random.uniform(-1, 1, [N, 2])
    
    y = X[:, 1] - X[:, 0] * line_coef - line_intercept >= 0
    y = np.where(y, 1, -1)
    if np.abs(np.sum(y)) == len(y):
        X, y = generate_data(N, line_coef, line_intercept)
    return X, y

def classifies_data(x_min,x_max, dimension, random_seed, random_seed_nr):
    """
    Generate a random line with corresponding coefficient and intercept
    """
    if random_seed:
        r = np.random.RandomState(random_seed_nr)
        line_points = r.uniform(x_min, x_max, [dimension, dimension])
    else:
        line_points = np.random.uniform(x_min, x_max, [dimension, dimension])
    line_coef = (line_points[1,1] - line_points[0, 1])/(line_points[1,0] - line_points[0, 0]) # the slope m = (y1-y0)/(x1-x0)
    line_intercept = line_points[0,1] - line_coef*line_points[0,0]  # the slope m = (y1-y0)/(x1-x0)
    return line_coef, line_intercept

coef, intercept = classifies_data(x_min = -1, x_max = 1, dimension = 2, random_seed = False, random_seed_nr = 600)
print(coef, intercept)
X_train, y_train = generate_data(10, coef, intercept, random2_seed = True, random2_seed_nr = 600)
print(X_train, y_train)
```

    -0.9171929562569725 -0.22254057968251678
    [[-0.93526545  0.08481933]
     [ 0.60583922 -0.88968119]
     [-0.23547045 -0.57805882]
     [ 0.70470288 -0.76285736]
     [-0.97476387 -0.76369724]
     [-0.45142148  0.79905572]
     [-0.54373691  0.97818468]
     [-0.83648775 -0.39845106]
     [ 0.84283001  0.97481966]
     [-0.89760555 -0.79300552]] [-1 -1 -1  1 -1  1  1 -1  1 -1]



```python
r.get_state()
```

44ecba592d8bd395bd6c8567ed96f6ecf31fa40b
ipynb
Jupyter Notebook
Linear-Perceptron/Linear_perceptron.ipynb
hilsdsg3/Machine_Learning_Fundamentels
6c464df60c622dca9379dac810074f1943e5e2d1
MIT
Linear-Perceptron/Linear_perceptron.ipynb
hilsdsg3/Machine_Learning_Fundamentels
6c464df60c622dca9379dac810074f1943e5e2d1
MIT
Linear-Perceptron/Linear_perceptron.ipynb
hilsdsg3/Machine_Learning_Fundamentels
6c464df60c622dca9379dac810074f1943e5e2d1
MIT
Qwen/Qwen-72B
1. YES
2. YES