Igigoto / model-ML-

0 stars 0 forks source link

implementacja #1

Open Igigoto opened 4 months ago

Igigoto commented 4 months ago

perceptron.py

import numpy as np

class Perceptron: def init(self, learning_rate=0.01, n_iters=1000): self.learning_rate = learning_rate self.n_iters = n_iters self.activation_func = self._unit_step_func self.weights = None self.bias = None

def fit(self, X, y):
    n_samples, n_features = X.shape
    self.weights = np.zeros(n_features)
    self.bias = 0

    y_ = np.array([1 if i > 0 else 0 for i in y])

    for _ in range(self.n_iters):
        for idx, x_i in enumerate(X):
            linear_output = np.dot(x_i, self.weights) + self.bias
            y_predicted = self.activation_func(linear_output)

            update = self.learning_rate * (y_[idx] - y_predicted)
            self.weights += update * x_i
            self.bias += update

def predict(self, X):
    linear_output = np.dot(X, self.weights) + self.bias
    y_predicted = self.activation_func(linear_output)
    return y_predicted

def _unit_step_func(self, x):
    return np.where(x >= 0, 1, 0)
Igigoto commented 4 months ago

app.py

from flask import Flask, request, jsonify import numpy as np from perceptron import Perceptron

app = Flask(name)

model = Perceptron() model.fit(np.array([[1,1], [2,2], [3,3], [4,4]]), np.array([0, 0, 1, 1]))

@app.route('/predict', methods=['POST']) def predict(): data = request.get_json(force=True) prediction = model.predict(np.array(data['features'])) return jsonify({'prediction': prediction.tolist()})

if name == 'main': app.run(host='0.0.0.0', port=5000)

Igigoto commented 4 months ago

Flask