Added ensemble agent updated with xg_boost agent to community-contributions
This commit is contained in:
2182
week8/community_contributions/ensemble-updated/day2.4_xgboost.ipynb
Normal file
2182
week8/community_contributions/ensemble-updated/day2.4_xgboost.ipynb
Normal file
File diff suppressed because one or more lines are too long
@@ -0,0 +1,52 @@
|
||||
import pandas as pd
|
||||
from sklearn.linear_model import LinearRegression
|
||||
import joblib
|
||||
|
||||
from agents.agent import Agent
|
||||
from agents.specialist_agent import SpecialistAgent
|
||||
from agents.frontier_agent import FrontierAgent
|
||||
from agents.random_forest_agent import RandomForestAgent
|
||||
from agents.xg_boost_agent import XGBoostAgent
|
||||
|
||||
class EnsembleAgent(Agent):
|
||||
|
||||
name = "Ensemble Agent"
|
||||
color = Agent.YELLOW
|
||||
|
||||
def __init__(self, collection):
|
||||
"""
|
||||
Create an instance of Ensemble, by creating each of the models
|
||||
And loading the weights of the Ensemble
|
||||
"""
|
||||
self.log("Initializing Ensemble Agent")
|
||||
self.specialist = SpecialistAgent()
|
||||
self.frontier = FrontierAgent(collection)
|
||||
self.random_forest = RandomForestAgent()
|
||||
self.xg_boost = XGBoostAgent()
|
||||
self.model = joblib.load('ensemble_model.pkl')
|
||||
self.log("Ensemble Agent is ready")
|
||||
|
||||
def price(self, description: str) -> float:
|
||||
"""
|
||||
Run this ensemble model
|
||||
Ask each of the models to price the product
|
||||
Then use the Linear Regression model to return the weighted price
|
||||
:param description: the description of a product
|
||||
:return: an estimate of its price
|
||||
"""
|
||||
self.log("Running Ensemble Agent - collaborating with specialist, frontier, xg boost and random forest agents")
|
||||
specialist = self.specialist.price(description)
|
||||
frontier = self.frontier.price(description)
|
||||
random_forest = self.random_forest.price(description)
|
||||
xg_boost = self.xg_boost.price(description)
|
||||
X = pd.DataFrame({
|
||||
'Specialist': [specialist],
|
||||
'Frontier': [frontier],
|
||||
'RandomForest': [random_forest],
|
||||
'XGBoost' : [xg_boost],
|
||||
'Min': [min(specialist, frontier, random_forest, xg_boost)],
|
||||
'Max': [max(specialist, frontier, random_forest, xg_boost)],
|
||||
})
|
||||
y = max(0, self.model.predict(X)[0])
|
||||
self.log(f"Ensemble Agent complete - returning ${y:.2f}")
|
||||
return y
|
||||
@@ -0,0 +1,46 @@
|
||||
# imports
|
||||
|
||||
import os
|
||||
import re
|
||||
from typing import List
|
||||
from sentence_transformers import SentenceTransformer
|
||||
import joblib
|
||||
from agents.agent import Agent
|
||||
import xgboost as xgb
|
||||
|
||||
|
||||
|
||||
|
||||
class XGBoostAgent(Agent):
|
||||
|
||||
name = "XG Boost Agent"
|
||||
color = Agent.BRIGHT_MAGENTA
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize this object by loading in the saved model weights
|
||||
and the SentenceTransformer vector encoding model
|
||||
"""
|
||||
self.log("XG Boost Agent is initializing")
|
||||
self.vectorizer = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
||||
self.model = joblib.load('xg_boost_model.pkl')
|
||||
self.log("XG Boost Agent is ready")
|
||||
|
||||
def price(self, description: str) -> float:
|
||||
"""
|
||||
Use an XG Boost model to estimate the price of the described item
|
||||
:param description: the product to be estimated
|
||||
:return: the price as a float
|
||||
"""
|
||||
self.log("XG Boost Agent is starting a prediction")
|
||||
vector = self.vectorizer.encode([description])
|
||||
vector = vector.reshape(1, -1)
|
||||
# Convert the vector to DMatrix
|
||||
dmatrix = xgb.DMatrix(vector)
|
||||
# Predict the price using the model
|
||||
result = max(0, self.model.predict(dmatrix)[0])
|
||||
self.log(f"XG Boost Agent completed - predicting ${result:.2f}")
|
||||
return result
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user