Upload final AdaBoost model and experiment log
Browse files- adaboost_final_model.joblib +3 -0
- data_register.py +29 -0
- final_adaboost_model_log.csv +2 -0
- prep.py +53 -0
- train.py +105 -0
adaboost_final_model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:199bac0eaec7724fd9ee921ed80cba8f7f0e5f7ba9a5e07a59e5051bb85d64df
|
| 3 |
+
size 97796
|
data_register.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import HfApi
|
| 2 |
+
from huggingface_hub.utils import RepositoryNotFoundError, HfHubHTTPError
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
repo_id = "avatar2102/engine-predictive-maintenance"
|
| 6 |
+
repo_type = "dataset"
|
| 7 |
+
|
| 8 |
+
token = os.getenv("PREDICTIVE_GIT_TOKEN")
|
| 9 |
+
if token is None:
|
| 10 |
+
raise ValueError("PREDICTIVE_GIT_TOKEN environment variable not set")
|
| 11 |
+
|
| 12 |
+
api = HfApi(token=token)
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
api.repo_info(repo_id=repo_id, repo_type=repo_type)
|
| 16 |
+
print(f"Dataset repo '{repo_id}' already exists. Using it.")
|
| 17 |
+
except (RepositoryNotFoundError, HfHubHTTPError):
|
| 18 |
+
print(f"Dataset repo '{repo_id}' not found. Creating new repo...")
|
| 19 |
+
api.create_repo(repo_id=repo_id, repo_type=repo_type, exist_ok=True)
|
| 20 |
+
print(f"Dataset repo '{repo_id}' created.")
|
| 21 |
+
|
| 22 |
+
api.upload_folder(
|
| 23 |
+
folder_path="prediction_project/data",
|
| 24 |
+
repo_id=repo_id,
|
| 25 |
+
repo_type=repo_type,
|
| 26 |
+
commit_message="Add engine predictive maintenance dataset"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
print("Dataset uploaded successfully.")
|
final_adaboost_model_log.csv
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
model,n_estimators,learning_rate,cv_f1_score,test_accuracy,test_precision,test_recall,test_f1_score,confusion_matrix
|
| 2 |
+
AdaBoost,150,0.05,0.7742989393943112,0.6539544407473765,0.6536652835408022,0.9593991067803491,0.7775584073708457,"[[192, 1252], [100, 2363]]"
|
prep.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import HfApi
|
| 2 |
+
from sklearn.model_selection import train_test_split
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
repo_id = "avatar2102/engine-predictive-maintenance"
|
| 7 |
+
token = os.getenv("PREDICTIVE_GIT_TOKEN")
|
| 8 |
+
|
| 9 |
+
if token is None:
|
| 10 |
+
raise ValueError("PREDICTIVE_GIT_TOKEN environment variable not set")
|
| 11 |
+
|
| 12 |
+
api = HfApi(token=token)
|
| 13 |
+
|
| 14 |
+
# Load dataset directly from Hugging Face using pandas
|
| 15 |
+
data_path = f"hf://datasets/{repo_id}/engine_data.csv"
|
| 16 |
+
df = pd.read_csv(data_path)
|
| 17 |
+
|
| 18 |
+
print("Dataset loaded successfully from Hugging Face.")
|
| 19 |
+
print("Original shape:", df.shape)
|
| 20 |
+
|
| 21 |
+
# Standardize column names
|
| 22 |
+
df.columns = df.columns.str.strip().str.lower().str.replace(" ", "_")
|
| 23 |
+
|
| 24 |
+
# Basic cleaning
|
| 25 |
+
df = df.drop_duplicates()
|
| 26 |
+
df = df.dropna()
|
| 27 |
+
|
| 28 |
+
print("Data cleaning completed.")
|
| 29 |
+
print("Shape after cleaning:", df.shape)
|
| 30 |
+
|
| 31 |
+
# Split into train and test
|
| 32 |
+
train_df, test_df = train_test_split(
|
| 33 |
+
df,
|
| 34 |
+
test_size=0.2,
|
| 35 |
+
random_state=42,
|
| 36 |
+
stratify=df["engine_condition"]
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Save locally
|
| 40 |
+
train_df.to_csv("prediction_project/data/train.csv", index=False)
|
| 41 |
+
test_df.to_csv("prediction_project/data/test.csv", index=False)
|
| 42 |
+
|
| 43 |
+
print("Train and test datasets saved locally.")
|
| 44 |
+
|
| 45 |
+
# Upload updated data folder back to Hugging Face
|
| 46 |
+
api.upload_folder(
|
| 47 |
+
folder_path="prediction_project/data",
|
| 48 |
+
repo_id=repo_id,
|
| 49 |
+
repo_type="dataset",
|
| 50 |
+
commit_message="Upload processed train and test datasets"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
print("Train and test datasets uploaded successfully to Hugging Face.")
|
train.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
|
| 5 |
+
from sklearn.ensemble import AdaBoostClassifier
|
| 6 |
+
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
|
| 7 |
+
from huggingface_hub import HfApi
|
| 8 |
+
from huggingface_hub.utils import RepositoryNotFoundError, HfHubHTTPError
|
| 9 |
+
|
| 10 |
+
DATASET_REPO_ID = "avatar2102/engine-predictive-maintenance"
|
| 11 |
+
MODEL_REPO_ID = "avatar2102/engine-predictive-maintenance-model"
|
| 12 |
+
|
| 13 |
+
token = os.getenv("PREDICTIVE_GIT_TOKEN")
|
| 14 |
+
if token is None:
|
| 15 |
+
raise ValueError("PREDICTIVE_GIT_TOKEN environment variable not set")
|
| 16 |
+
|
| 17 |
+
api = HfApi(token=token)
|
| 18 |
+
|
| 19 |
+
# Load train/test data from Hugging Face dataset repo
|
| 20 |
+
train_path = f"hf://datasets/{DATASET_REPO_ID}/train.csv"
|
| 21 |
+
test_path = f"hf://datasets/{DATASET_REPO_ID}/test.csv"
|
| 22 |
+
|
| 23 |
+
train_df = pd.read_csv(train_path)
|
| 24 |
+
test_df = pd.read_csv(test_path)
|
| 25 |
+
|
| 26 |
+
print("Train and test datasets loaded successfully from Hugging Face.")
|
| 27 |
+
print("Train shape:", train_df.shape)
|
| 28 |
+
print("Test shape:", test_df.shape)
|
| 29 |
+
|
| 30 |
+
# Split features and target
|
| 31 |
+
X_train = train_df.drop("engine_condition", axis=1)
|
| 32 |
+
y_train = train_df["engine_condition"]
|
| 33 |
+
|
| 34 |
+
X_test = test_df.drop("engine_condition", axis=1)
|
| 35 |
+
y_test = test_df["engine_condition"]
|
| 36 |
+
|
| 37 |
+
print("Feature-target split completed.")
|
| 38 |
+
|
| 39 |
+
# Final AdaBoost model using tuned parameters from interim phase
|
| 40 |
+
final_model = AdaBoostClassifier(
|
| 41 |
+
n_estimators=150,
|
| 42 |
+
learning_rate=0.05,
|
| 43 |
+
random_state=42
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Train model
|
| 47 |
+
final_model.fit(X_train, y_train)
|
| 48 |
+
print("Final AdaBoost model trained successfully.")
|
| 49 |
+
|
| 50 |
+
# Predict on test data
|
| 51 |
+
y_pred = final_model.predict(X_test)
|
| 52 |
+
|
| 53 |
+
# Evaluate model
|
| 54 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 55 |
+
precision = precision_score(y_test, y_pred)
|
| 56 |
+
recall = recall_score(y_test, y_pred)
|
| 57 |
+
f1 = f1_score(y_test, y_pred)
|
| 58 |
+
cm = confusion_matrix(y_test, y_pred)
|
| 59 |
+
|
| 60 |
+
print("Model evaluation completed.")
|
| 61 |
+
print("Accuracy:", accuracy)
|
| 62 |
+
print("Precision:", precision)
|
| 63 |
+
print("Recall:", recall)
|
| 64 |
+
print("F1-score:", f1)
|
| 65 |
+
print("Confusion Matrix:")
|
| 66 |
+
print(cm)
|
| 67 |
+
|
| 68 |
+
# Save experiment log
|
| 69 |
+
log_df = pd.DataFrame([{
|
| 70 |
+
"model": "AdaBoost",
|
| 71 |
+
"n_estimators": 150,
|
| 72 |
+
"learning_rate": 0.05,
|
| 73 |
+
"cv_f1_score": 0.7742989393943112,
|
| 74 |
+
"test_accuracy": accuracy,
|
| 75 |
+
"test_precision": precision,
|
| 76 |
+
"test_recall": recall,
|
| 77 |
+
"test_f1_score": f1,
|
| 78 |
+
"confusion_matrix": str(cm.tolist())
|
| 79 |
+
}])
|
| 80 |
+
|
| 81 |
+
log_df.to_csv("prediction_project/model_building/final_adaboost_model_log.csv", index=False)
|
| 82 |
+
print("Experiment log saved successfully.")
|
| 83 |
+
|
| 84 |
+
# Save model locally
|
| 85 |
+
joblib.dump(final_model, "prediction_project/model_building/adaboost_final_model.joblib")
|
| 86 |
+
print("Model saved locally as joblib.")
|
| 87 |
+
|
| 88 |
+
# Create model repo if missing
|
| 89 |
+
try:
|
| 90 |
+
api.repo_info(repo_id=MODEL_REPO_ID, repo_type="model")
|
| 91 |
+
print(f"Model repo '{MODEL_REPO_ID}' already exists. Using it.")
|
| 92 |
+
except (RepositoryNotFoundError, HfHubHTTPError):
|
| 93 |
+
print(f"Model repo '{MODEL_REPO_ID}' not found. Creating new repo...")
|
| 94 |
+
api.create_repo(repo_id=MODEL_REPO_ID, repo_type="model", exist_ok=True)
|
| 95 |
+
print(f"Model repo '{MODEL_REPO_ID}' created.")
|
| 96 |
+
|
| 97 |
+
# Upload model_building folder to HF Model Hub
|
| 98 |
+
api.upload_folder(
|
| 99 |
+
folder_path="prediction_project/model_building",
|
| 100 |
+
repo_id=MODEL_REPO_ID,
|
| 101 |
+
repo_type="model",
|
| 102 |
+
commit_message="Upload final AdaBoost model and experiment log"
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
print("Model uploaded successfully to Hugging Face Model Hub.")
|