File size: 2,123 Bytes
3d24ae9
 
 
 
 
 
921c0a2
3d24ae9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4e0e4a
3d24ae9
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
from sklearn.ensemble import AdaBoostClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import joblib
import pandas as pd
import os
import numpy as np

class Classifier:
    def __init__(self):
        pass

    def train_and_save(self):
        print("\nIRIS model training...")
        iris = load_iris()
        ada = AdaBoostClassifier(n_estimators=5)

        X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.1, random_state=42)
        model = ada.fit(X_train, y_train)

        print(f"Model score: {ada.score(X_train, y_train):.3f}")
        print(f"Test Accuracy: {ada.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):
        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 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}