简体   繁体   中英

Training personalized based machine learning models

i work on a php project along with python which uses flask as api which predict user like on a post based on the previous engagement on other posts and its purely user based.

my requirement is suppose there are 1000`s of users in our system. and they have done likes for old posts before.when new posts arrive i need to somehow identify whether user likes it or not.and this is done via a cron job

approach 1

i am using Logistic regression as model so probably need dynamic pkl file for each user.because different users engagement on same post is different so i need to save some thing like model_{user_id}.pkl file where user_id is the user id of the user

approach 2

use content based recommended system.but as far as i know it can't store like a pkl file in production. so for each users from the 1000`s of users i need to run the recommender function.

approach 1 drawback

creating dynamic pkl file for each user which means more files.i never seen this approach on internet

approach 2 drawback

calling the recommender function for each user is probably a bad idea i believe.that will heavily affect cpu usage etc.

can somebody please help me how to properly solve this problem.i am new in machine learning. please consider my question. thanks in advance.

I would suggest something like this:

  • Create the user models as an array (or data frame) of models
  • Save this array as a pkl
  • When loading the app (not on each API call), load the array of models into the memory
  • When an API is called, the model is already in the memory - use it to predict the result

Something like this (not tested - just a notion):

#for saving the model
model_data = pd.DataFrame(columns=['user','model'])
temp_model = RandomForestClassifier().fit(X,y)
new = pd.DataFrame({'user':[user_id],'model':[temp_model]})
model_data = model_data.append(new)
packed_model = jsonpickle.pickler.Pickler.flatten(model_data)

#for loading the model
unpacked_model = jsonpickle.unpickler.Unpickler.restore(packed_model) #this should be in the begining of your flask file - loaded into the memory
user_model=unpacked_model.at(user_id,'model') #this should be inside every api call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM