Spaces:
Sleeping
Sleeping
File size: 1,046 Bytes
5c8d177 |
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 |
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.")
|