File size: 2,335 Bytes
9ecca49
 
 
 
 
 
 
 
 
89365a6
3d24ae9
 
9ecca49
3d24ae9
 
9ecca49
 
3d24ae9
 
 
 
9ecca49
3d24ae9
 
9ecca49
3d24ae9
9ecca49
 
 
 
3d24ae9
9ecca49
 
3d24ae9
 
 
 
 
9ecca49
 
 
3d24ae9
 
 
 
 
 
9ecca49
3d24ae9
 
 
 
 
 
 
f4e0e4a
13eef60
 
 
921c0a2
 
3d24ae9
 
 
 
 
 
e0f980a
3d24ae9
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
IRIS Classification - class definition
"""

import os
import numpy as np
import pandas as pd
import joblib

from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split


class Classifier:
    """Classifier class - ML training and testing"""

    def __init__(self):
        pass

    def train_and_save(self):
        """ML training and saving"""
        print("\nIRIS model training...")
        iris = load_iris()
        cart = DecisionTreeClassifier(max_depth=3)

        x_train, x_test, y_train, y_test = train_test_split(
            iris.data, iris.target, test_size=0.1, random_state=42
        )
        model = cart.fit(x_train, y_train)

        print(f"Model score: {cart.score(x_train, y_train):.3f}")
        print(f"Test Accuracy: {cart.score(x_test, y_test):.3f}")

        current_dir = os.path.dirname(os.path.abspath(__file__))
        parent_dir = os.path.dirname(current_dir)
        test_data_csv_path = os.path.join(parent_dir, "data", "test_data.csv")

        pd.concat([pd.DataFrame(x_test), pd.DataFrame(y_test, columns=["4"])], axis=1).to_csv(
            test_data_csv_path, index=False
        )

        model_path = os.path.join(parent_dir, "models", "model.pkl")
        joblib.dump(model, model_path)
        print(f"Model saved to {model_path}")

    def load_and_test(self, data):
        "ML loading and testing"
        print("\nIRIS model prediction...")

        current_dir = os.path.dirname(os.path.abspath(__file__))
        parent_dir = os.path.dirname(current_dir)
        model_path = os.path.join(parent_dir, "models", "model.pkl")
        model = joblib.load(model_path)

        features = np.array(data)
        if len(features.shape) == 1:
            features = features.reshape(1, -1)

        if features.shape[-1] != 4:
            raise ValueError("Expected 4 features per input.")

        # Predict the class
        predictions = model.predict(features).tolist()
        probabilities = model.predict_proba(features).tolist()

        # Map predictions to class labels
        iris_types = {0: "setosa", 1: "versicolor", 2: "virginica"}
        prediction_labels = [iris_types[pred] for pred in predictions]

        return {"predictions": prediction_labels, "probabilities": probabilities}