Spaces:
Configuration error
Configuration error
Download train_model.py from teohteiktoe/spam: direct link, hf CLI and curl.
- Browser
- Download file 996 Bytes
-
https://huggingface.co/spaces/teohteiktoe/spam/resolve/main/train_model.py
- Command line
-
hf download hf://spaces/teohteiktoe/spam/train_model.py
-
curl -L -o train_model.py https://huggingface.co/spaces/teohteiktoe/spam/resolve/main/train_model.py
996 Bytes
| import pandas as pd | |
| import joblib | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.pipeline import Pipeline | |
| from sklearn.feature_extraction.text import CountVectorizer | |
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.metrics import accuracy_score, classification_report | |
| df = pd.read_csv("spam_text.csv") | |
| X = df["Message"].astype(str) | |
| y = df["Category"].astype(str) | |
| X_train, X_test, y_train, y_test = train_test_split( | |
| X, y, test_size=0.2, random_state=42, stratify=y | |
| ) | |
| model = Pipeline([ | |
| ("vectorizer", CountVectorizer(stop_words="english", max_features=5000)), | |
| ("classifier", RandomForestClassifier( | |
| n_estimators=200, | |
| random_state=42, | |
| class_weight="balanced" | |
| )) | |
| ]) | |
| model.fit(X_train, y_train) | |
| pred = model.predict(X_test) | |
| print("Accuracy:", round(accuracy_score(y_test, pred), 4)) | |
| print(classification_report(y_test, pred)) | |
| joblib.dump(model, "spam_rf_model.joblib") | |
| print("Model saved as spam_rf_model.joblib") | |