File size: 918 Bytes
e184c79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Train and save the breast cancer detection model (30 features).
Run this script if breast_cancer_detector.pickle is missing.
The app expects a model that accepts (1, 30) input and returns 0 or 1.
"""
import os
import pickle
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

MODEL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'breast_cancer_detector.pickle')

def train_and_save():
    data = load_breast_cancer()
    X, y = data.data, data.target
    X_train, _, y_train, _ = train_test_split(X, y, test_size=0.2, random_state=42)
    model = RandomForestClassifier(n_estimators=50, random_state=42)
    model.fit(X_train, y_train)
    with open(MODEL_PATH, 'wb') as f:
        pickle.dump(model, f)
    print(f"Model saved to {MODEL_PATH}")

if __name__ == '__main__':
    train_and_save()