File size: 636 Bytes
6e2fccb
edce0df
 
 
 
 
 
 
 
6e2fccb
 
 
 
 
 
 
 
 
 
 
edce0df
6e2fccb
 
edce0df
 
 
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
import os
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from joblib import dump, load

MODEL_PATH = "heating_model.pkl"
DATA_PATH = "mantle_training.csv"
HISTORY = []

# Function to train the model from scratch
def train_and_save_model():
    data = pd.read_csv(DATA_PATH)
    X = data[["temperature", "duration"]]
    y = data["risk_level"]
    model = RandomForestClassifier()
    model.fit(X, y)
    dump(model, MODEL_PATH)
    return model

# Safe model loader
def load_model():
    if not os.path.exists(MODEL_PATH):
        return train_and_save_model()
    return load(MODEL_PATH)

model = load_model()