iqranaz's picture
Update inference/predict.py
955e101 verified
import pandas as pd
#import lightgbm as lgb
#from sklearn.preprocessing import LabelEncoder
import subprocess
import sys
import warnings
warnings.filterwarnings('ignore')
try:
import lightgbm as lgb
from sklearn.preprocessing import LabelEncoder
from scipy.stats import boxcox
except:
print("An exception occurred")
subprocess.run([sys.executable, "-m", "pip", "install", "lightgbm"])
subprocess.run([sys.executable, "-m", "pip", "install", "scipy"])
subprocess.run([sys.executable, "-m", "pip", "install", "scikit-learn"])
from sklearn.preprocessing import LabelEncoder
import lightgbm as lgb
from scipy.stats import boxcox
def preprocess_input(input_data):
# Preprocess the input data (assuming a similar preprocessing as in the training script)
input_data['rain'] = boxcox(input_data['rain'] + 1)[0]
return input_data
def load_model(model_path='inference/lgbm_model.txt'):
# Load the trained LightGBM model
model = lgb.Booster(model_file=model_path)
return model
def predict(input_data, model):
# Preprocess the input data
preprocessed_data = preprocess_input(pd.DataFrame([input_data]))
# Make predictions using the loaded model
prediction = model.predict(preprocessed_data, num_iteration=model.best_iteration)
predicted_class = prediction.argmax(axis=1)
# Decode the predicted class (inverse transform)
label_encoder = LabelEncoder()
predicted_label = label_encoder.inverse_transform(predicted_class)[0]
return predicted_label