Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import joblib | |
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.preprocessing import LabelEncoder | |
| from sklearn.model_selection import train_test_split | |
| # Load data | |
| df = pd.read_csv("data/transactions.csv") | |
| # Feature engineering | |
| df["hour"] = pd.to_datetime(df["time"], format="%H:%M").dt.hour | |
| df.drop(columns=["check_id", "time"], inplace=True) | |
| # Encode categorical variables | |
| categorical_cols = ["employee_id", "terminal_id"] | |
| encoders = {} | |
| for col in categorical_cols: | |
| enc = LabelEncoder() | |
| df[col] = enc.fit_transform(df[col]) | |
| encoders[col] = enc | |
| # Features and target | |
| X = df.drop(columns=["suspicious"]) | |
| y = df["suspicious"] | |
| # Train/test split | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| # Train model | |
| model = RandomForestClassifier(n_estimators=100, random_state=42) | |
| model.fit(X_train, y_train) | |
| # Save model and encoders | |
| joblib.dump(model, "model/model.pkl") | |
| joblib.dump(encoders, "model/encoders.pkl") | |
| print("Training complete. Model saved.") | |