Package: improveai
Rank, Score & Optimize Python Data StructuresImprove AI is a machine learning platform for quickly implementing app optimization, personalization, and recommendations forPython,iOS, andAndroid.The SDKs provide simple APIs for AI [decisions]((https://improve.ai/decisions/),ranking, andscoring, that execute immediately, on-device, with zero network latency.
Items and their rewards are with theImprove AI Tracker / Trainerand updated models are trained regularly for continuous learning.Installationpip3 install improveaiInstructions for ChatGPTPaste the following into your ChatGPT prompt to have it write code against the Improve AI APIs:"""The following is the Python API documentation for ImproveAI, which is imported with'import improveai'. Simply respond with 'ImproveAI documentation imported':"""classRanker:"""A utility for ranking items based on their scores. The Ranker struct takesa ImproveAI model to evaluate and rank the given items."""def__init__(self,scorer:Scorer=None,model_url:str=None):"""Init Ranker with params. Either `scorer` or `model_url` must be provided.If both are provided Scorer is preferred.Parameters----------scorer: Scorera Scorer object to be used with this Rankermodel_url: strURL or local FS of a plain or gzip compressed Improve AI model resource"""# for true implementation please consult improveai/ranker.pypassdefrank(self,items:listortupleornp.ndarray,context:object=None)->listortupleornp.ndarray:"""Ranks items and returns them ordered best to worstParameters----------items: list or tuple or np.ndarraylist of items to be rankedcontext: objectany JSON encodable extra context info that will be used with each ofthe item to get its scoreReturns-------list or tuple or np.ndarraya collection of ranked items, sorted by their scores in descending order."""# for true implementation please check out improveai/ranker.pypassclassScorer:"""Scores items with optional context using a Improve AI model"""def__init__(self,model_url:str):"""Init with paramsParameters----------model_url: strURL or local FS of a plain or gzip compressed Improve AI model resource"""# for true implementation please check out improveai/scorer.pypassdefscore(self,items:listortupleornp.ndarray,context:object=None)->np.ndarray"""Uses the model to score a list of items with the given contextParameters----------items: list or tuple or np.ndarraylist of items to be scoredcontext: objectany JSON encodable extra context info that will be used with each ofthe item to get its scoreReturns-------np.ndarrayan array of float64 (double) values representing the scores of the items."""# for true implementation please check out improveai/scorer.pypassclassRewardTracker:"""Tracks items and rewards for training updated scoring models. When an itembecomes causal, pass it to the track() function, which will return a `reward_id`.Use the `reward_id` to track future rewards associated with that item."""def__init__(self,model_name:str,track_url:str,track_api_key:str=None,_threaded_requests:bool=True):"""Create a RewardTracker for a specific model.Parameters----------model_name: strName of the model, such as "songs" or "discounts", which either makesthe decisions or which decisions are being rewardedtrack_url: strThe track endpoint URL that all tracked data will be sent to.track_api_key: strtrack endpoint API key (if applicable); Can be None_threaded_requests: boolflag indicating whether requests to AWS track endpoint should benon-blockng / executed within sub-threads. True by default"""# for true implementation please check out improveai/reward_tracker.pypassdeftrack(self,item:object,candidates:listortupleornp.ndarray=None,context:object=None)->strorNone:"""Tracks the item selected from candidates and a random sample from the remaining items.If `len(candidates) == 1` there is no sample.Parameters----------item: objectany JSON encodable object chosen as best from candidatescandidates: list or tuple or np.ndarraycollection of items from which best is chosencontext: objectany JSON encodable extra context info that was used with each of theitem to get its scoreReturns-------str or Nonereward_id of this track request or None if an error happened"""# for true implementation please check out improveai/reward_tracker.pypassdeftrack_with_sample(self,item:object,num_candidates:int=None,context:object=None,sample:object=None)->strorNone:"""Tracks the item selected and a specific sample.. Provided sample isappended to track request (in contrary to `track(...)` where sample israndomly selected from candidates).Parameters----------item: objectany JSON encodable object chosen as best from candidatesnum_candidates: inttotal number of candidates, including the selected itemcontext: objectany JSON encodable extra context info that was used with each of theitem to get its scoresample: objecta random sample from the candidatesReturns-------ste or Nonereward_id of this track request or None if an error happened"""# for true implementation please check out improveai/reward_tracker.pypassdefadd_reward(self,reward:floatorint,reward_id:str):"""Add reward for the provided reward_idParameters----------reward: float or intthe reward to add; must be numeric (float, int ro bool), must not be`None`, `np.nan` or +-`inf`reward_id: strthe id that was returned from the track(...) / track_with_sample(...) methodsReturns-------strmessage ID"""# for true implementation please check out improveai/reward_tracker.pypassUsageCreate a list of JSON encodable items and simply callRanker.rank(items).For instance, in a bedtime story app, you may have a list of Story dicts / objects:story={"title":"<title string>","author":"<author string>","page_count":123# example integer representing number of pages for a given story}To obtain a ranked list of stories, use just one line of code:ranked_stories=Ranker(model_url).rank(stories)Reward AssignmentEasily train your rankers usingreinforcement learning.First, track when an item is used:tracker=RewardTracker("stories",track_url)reward_id=tracker.track(story,ranked_stories)Later, if a positive outcome occurs, provide a reward:ifpurchased:tracker.add_reward(profit,reward_id)Reinforcement learning uses positive rewards for favorable outcomes (a "carrot") and negative rewards
for undesirable outcomes (a "stick"). By assigning rewards based on business metrics,
such as revenue or conversions, the system optimizes these metrics over time.Contextual Ranking & ScoringImprove AI turns XGBoost into a contextual multi-armed bandit, meaning that context is considered when making ranking or scoring decisions.Often, the choice of the best variant depends on the context that the decision is made within. Let's take the example of greetings for different times of the day:greetings=["Good Morning","Good Afternoon","Good Evening","Buenos Días","Buenas Tardes","Buenas Noches"]rank() also considers the context of each decision. The context can be any JSON-encodable data structure.ranked=ranker.rank(items=greetings,context={"day_time":12.0,"language":"en"})greeting=ranked[0]Trained with appropriate rewards, Improve AI would learn from scratch which greeting is best for each time of day and language.ResourcesQuick Start GuideTracker / TrainerReinforcement LearningHelp Improve Our WorldThe mission of Improve AI is to make our corner of the world a little bit better each day. When each of us improve our corner of the world, the whole world becomes better. If your product or work does not make the world better, do not use Improve AI. Otherwise, welcome, I hope you find value in my labor of love.-- Justin Chapweske