Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

python - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...