def recommend( usr, pwd ): userInfo, allWeibos = Weibo.authentication( usr, pwd ) label = Recommendation.recommend( allWeibos ) labelIdx = int(label[0]) tag = labels[labelIdx] books = Douban.getRecommondBooks(tag) for book in books: print book return books, userInfo
def changeUser(): global currentUser currentUser = Recommendation.getRandomUserID() #rest the sentiment score SentimentAnalysis.reset() currentSentimentScore = SentimentAnalysis.initSentimentScore() trackObjs = getUserRecommendation() htmlSnippet = render_template('recommendation.html', trackObjs=trackObjs) data = jsonify({'datax': htmlSnippet, 'currentUser': str(currentUser)}) return data
class Environnement(): def __init__(self, N_items, N_recommended, behaviour="random", rewardType='Similarity', rewardParameters=[1, 1], proba_p=0.7, specific_items=None, name='envi_01'): #proba_p for ramdom choice (customer) self.items = Items(N_items) self.recommendation = Recommendation(self.items, N_recommended) self.customer = Customer(self.items, self.recommendation, behaviour, proba_p, specific_items) self.name = name self.rewardType = rewardType self.rewardParameters = rewardParameters def endEpisode(self): self.customer.endEpisode() self.recommendation.endEpisode() #self.step to simulate new step of the environnement def step( self, agentRecommendation ): #here agentRecommendation is the items recommended by the agent self.recommendation.recommend( agentRecommendation) #We have set the new recommendations self.customer.choice() reward = self.computeReward() return reward def computeReward( self ): #This function will be refined to get more realistic rewards if self.rewardType == 'Similarity': reward = -self.items.items[ self.customer.choice_id].cost * self.rewardParameters[0] reward += self.items.similarities[ self.customer.previous_choice_id][ self.customer.choice_id] * self.rewardParameters[1] elif self.rewardType == 'Trust': reward = -(self.items.items[self.customer.choice_id].cost ) * self.rewardParameters[0] reward += self.customer.trust_recommendation * self.rewardParameters[ 1] else: print("Error : wrong reward type") return None return reward def display(self, print_item=False): print('---------ENVIRONNEMENT DISPLAY--------') self.items.display(print_item) self.recommendation.display() self.customer.display()
def WeekTweet(self): tempTweet = "" for x in range(1, 6): self.weatherObj = Weather(GPSLOCATION, AIRPORT, x) self.rec = Recommendation.Recommendation(self.weatherObj) tempTweet += self.weatherObj.dayofWeek["short"] + "- " + str( self.rec.rating) + "/" tempTweet += self.weatherObj.weatherStatus + "/Morn " + str( math.ceil(self.weatherObj.mornTemp)) + "°F/Eve " + str( math.ceil(self.weatherObj.eveTemp)) + "°F" if (self.rec.weatherEmoji): tempTweet += self.rec.weatherEmoji + "\n" self.tweetContents = "THIS WEEK'S CONDITIONS:\n" + tempTweet self.addHashTag() TweetPublisher(self.tweetContents)
def main(): if request.method == 'GET': return render_template('index.html') if request.method == 'POST': movie_name = request.form['movie_name'] movie_name = movie_name.title() print(movie_name) if movie_name not in data['original_title'].unique(): return render_template('negative.html') else: res = Recommendation.results(movie_name) return render_template('positive.html', movie_names=res['Title'].tolist(), director=res['Director'].tolist(), search_name=movie_name)
def slider_response( self ): Property_dict[ "Melee" ] = self.Melee.value() Property_dict[ "Physical" ] = self.Physical.value() Property_dict[ "Damage" ] = self.Damage.value() Property_dict[ "Solo" ] = self.Solo.value() Property_dict[ "Team" ] = self.Team.value() Property_dict[ "Range" ] = self.Range.value() Property_dict[ "Magical" ] = self.Magical.value() Property_dict[ "Tank" ] = self.Tank.value() Property_dict[ "Support" ] = self.Support.value() #self.career_recommendation = [] #self.friend_recommendation = [] self.career_recommendation, self.friend_recommendation = Recommendation.getInput( Property_dict ) self.comboBox.clear() self.comboBox.addItems( self.friend_recommendation ) pic = QtGui.QPixmap(self.pic_path + self.career_recommendation[0]+'.png') pic = pic.scaled(self.graphicsView.size()) self.QGraphicsScene.clear() self.QGraphicsScene.addPixmap(pic) self.commandLinkButton.setText(self.career_recommendation[0]) pic = QtGui.QPixmap(self.pic_path + self.career_recommendation[1]+'.png') pic = pic.scaled(self.graphicsView_2.size()) self.QGraphicsScene_2.clear() self.QGraphicsScene_2.addPixmap(pic) self.commandLinkButton_2.setText(self.career_recommendation[1]) pic = QtGui.QPixmap(self.pic_path + self.career_recommendation[2]+'.png') pic = pic.scaled(self.graphicsView_3.size()) self.QGraphicsScene_3.clear() self.QGraphicsScene_3.addPixmap(pic) self.commandLinkButton_3.setText(self.career_recommendation[2]) pic = QtGui.QPixmap(self.pic_path + self.career_recommendation[3]+'.png') pic = pic.scaled(self.graphicsView_4.size()) self.QGraphicsScene_4.clear() self.QGraphicsScene_4.addPixmap(pic) self.commandLinkButton_4.setText(self.career_recommendation[3]) pic = QtGui.QPixmap(self.pic_path + self.career_recommendation[4]+'.png') pic = pic.scaled(self.graphicsView_5.size()) self.QGraphicsScene_5.clear() self.QGraphicsScene_5.addPixmap(pic) self.commandLinkButton_5.setText(self.career_recommendation[4])
def note(request, heading=""): if heading: try: new_note = Ribbit.objects.get(heading=heading) form = RibbitForm(request.POST or None, instance=new_note) print 'line 142' global_heading = heading except Ribbit.DoesNotExist: redirect('/') return render(request, 'detailed_note.html', { 'ribbit': new_note, 'form': form }) if request.POST.get('extract'): extractor = rake.Rake( '/Users/seda/Xtrify/ribbit/ribbit_app/SmartStoplist.txt') note = Ribbit.objects.get(heading=request.POST['heading']) keywords = extractor.run(note.content) keywords = [str(keywords[x][0]) for x in range(len(keywords))] keywords = str(keywords)[1:-1] note.keywords = keywords note.save() h = request.POST['heading'] return redirect('/note/' + h) if request.POST.get('recommend'): rec = Recommendation.Recommendation() note = Ribbit.objects.get(heading=request.POST['heading']) keywords = note.keywords keywords = re.sub('[\']', '', keywords) keywords = keywords.split(',') results = rec.search(keywords) results.append('asdasd') results.append('adasda') print results return render(request, 'recommendation.html', {'results': results}) if request.method == "POST": ribbit_form = RibbitForm( request.POST, instance=Ribbit.objects.get(heading=request.POST['heading'])) #ribbit_form = RibbitForm(request.POST, instance=Ribbit.objects.get(heading=global_heading)) if ribbit_form.is_valid(): ribbit_form.save() return redirect('/')
def validate(job_title, df_job_post, df_course, course_num=5): ''' check the accuracy of first 5 recommended courses if the job title of the poster is in the suggested user for the courses ''' related = 0 irrelated = 0 df_course2 = df_course[pd.notnull(df_course.checked_job)] for job_post in df_job_post.iterrows(): df_recommed_course = rmd.recommendation(job_post, df_course2, course_num) df_recommed_course['relevancy'] = df_recommed_course.checked_job.apply( lambda x: True if job_title in x else False) a = df_recommed_course.relevancy.sum() related += a irrelated += course_num - a accuracy = related / (related + irrelated) return accuracy
def note(request, heading=""): if heading: try: new_note = Note.objects.get(heading=heading) form = NoteForm(request.POST or None, instance=new_note) print 'line 142' global_heading = heading except Note.DoesNotExist: redirect('/') return render(request, 'detailed_note.html', { 'note': new_note, 'form': form }) if request.POST.get('extract'): extractor = rake.Rake(os.getcwd() + '/note_app/SmartStoplist.txt') note = Note.objects.get(heading=request.POST['heading']) keywords = extractor.run(note.content) keywords = [str(keywords[x][0]) for x in range(len(keywords))] keywords = str(keywords)[1:-1] note.keywords = keywords note.save() h = request.POST['heading'] return redirect('/note/' + h) if request.POST.get('recommend'): rec = Recommendation.Recommendation() note = Note.objects.get(heading=request.POST['heading']) keywords = note.keywords keywords = re.sub('[\']', '', keywords) keywords = keywords.split(',') results = rec.recommend(keywords) print keywords return render(request, 'recommendation.html', {'results': results}) if request.method == "POST": note_form = NoteForm( request.POST, instance=Note.objects.get(heading=request.POST['heading'])) #note_form = NoteForm(request.POST, instance=Note.objects.get(heading=global_heading)) if note_form.is_valid(): note_form.save() h = request.POST['heading'] return redirect('/note/' + h)
def WeekendTweet(self): tempTweet = "" for x in range(1, 3): self.weatherObj = Weather(GPSLOCATION, AIRPORT, x) self.rec = Recommendation.Recommendation(self.weatherObj) tempTweet += self.weatherObj.dayofWeek["long"] + "- " + str( self.rec.rating) + "/" tempTweet += self.weatherObj.weatherStatus + "/Morn " + str( math.ceil(self.weatherObj.mornTemp)) + "°F/Eve " + str( math.ceil(self.weatherObj.eveTemp)) + "°F" if ((self.weatherObj.rainText) and (self.weatherObj.weatherStatus != "trace rain")): tempTweet += "/" + self.weatherObj.rainText if (self.rec.weatherEmoji): tempTweet += self.rec.weatherEmoji + "\n" #Get the emoji here self.tweetContents = "THIS WEEKEND: \n" + tempTweet self.addHashTag() TweetPublisher(self.tweetContents)
def main(argv): args = parser.parse_args(argv[1:]) # train_set, test_set = split_test_set(get_ratings(), frac=0.3, random=True) # data = get_ratings('data/ratings_test.csv') data = get_ratings(args.train) # data = hide_rating(data, frac=0.3) k, pred_user, pred_item = 10, '140756691', '' if args.method == 'cf': item_based = model_recomm.ItemBased(data) recommend_list = item_based.top_k(user=pred_user, k=k, sim_fn=item_based.sim_cos) elif args.method == 'mf': model = train_mf(data, num_steps=args.num_steps) recommend_list = model.top_k(user=pred_user, k=k) else: raise Exception('Unsupport method %s' % args.method) # 140756691 print(recommend_list)
def train_mf(df, num_steps=10000): if 'real_rating' in df.columns: train_df = df[df['rating'] > 0] else: train_df = df def _set_index(df, keys): """ Set index of given key (after drop duplicates) :param df: :param keys: :return: """ for key in keys: unique_df = df.drop_duplicates([key]).reset_index() df.insert(len(df.columns), key + '_index', np.zeros(len(df), dtype=np.int32)) for i, row in unique_df.iterrows(): origin_index = list(df[df[key] == row[key]].index) df.loc[origin_index, key + '_index'] = i _set_index(train_df, ['item', 'user']) # code.interact(local=locals()) total_item, total_user = len(train_df.drop_duplicates(['item'])), len(train_df.drop_duplicates(['user'])) estimator = model_recomm.MatrixFactorizationEstimator( R_shape=(total_item, total_user), num_features=10, model_dir="models/mf/" ) input_fn = tf.estimator.inputs.pandas_input_fn( x=train_df, y=train_df['rating'], batch_size=2, num_epochs=None, shuffle=True ) estimator.train(input_fn=input_fn, steps=num_steps) return estimator
def generateRecommdation(name, email, phoneno): data = Recommendation.RecommendationDataMap() data.name = name data.email = email data.phoneno = phoneno data.tier_city = tier_city_map[get_int_random(1, 3)] data.count_travel = get_int_random(1, 20) data.count_pol = get_int_random(1, 20) data.count_auto = get_int_random(1, 20) data.count_shop = get_int_random(1, 20) like = [] random_no = get_int_random(5, 10) choosen_array = [] while len(choosen_array) != random_no: y = get_int_random(0, len(likes_insight) - 1) if y not in choosen_array: choosen_array.append(y) like.append(likes_insight[y]) x = get_int_random(2, 15) like.append(x) data.likes_insights = like genre = [] random_no = get_int_random(2, 8) choosen_array = [] while len(choosen_array) != random_no: y = get_int_random(0, len(movie_genre) - 1) if y not in choosen_array: choosen_array.append(y) genre.append(movie_genre[y]) x = get_int_random(2, 15) genre.append(x) data.movie_genre = genre data.purchased_car = cars[get_int_random(0, len(cars) - 1)] return data
def hide_rating(data, frac=0.3): """ Hide some ratings for evaluate :param data: :param frac: how many ratings should be hidden :return: DataFrame with additional column `real_rating` for origin ratings, and some `rating` assigned by 0 """ df = data if 'rating' not in df: raise model_recomm.ColumnNotFoundError('rating') if 'real_rating' in df: df['real_rating'] = np.array(df['rating']) else: df.insert(len(df.columns), 'real_rating', np.array(df['rating'])) if frac > 0: # hide top `frac` ratings for every user def _hide(df, frac): for i, user in df['user'].drop_duplicates().items(): user_ratings = df[df['user'] == user] hide_len = int(len(user_ratings) * 0.3) if hide_len == 0: continue index = user_ratings.index[:hide_len] df.loc[index, 'rating'] = 0 return df df = _hide(df, frac) df = df.sample(frac=1).reset_index(drop=True) return df
count = 5 distancia = 5 Desenha().desenhar(movies) for f in movies: if f == src: print(f) for similar in f.similar: print('\t', similar) print('*' * 100) print('A partir do filme:') print('\t', src) print('Foram achados os seguintes filmes recomendados: ') r = Recommendation.recommend(src, count) for movie in r: print('\t', movie) Desenha().recomendados(src, r) print('*' * 100) print('A partir do filme:') print('\t', src) print('Com o gênero: ', genero) print('Foram achados os seguintes filmes recomendados: ') r2 = Recommendation.recommendInGenre(src, genero, count) for movie in r2: print('\t', movie) Desenha().recomendados(src, r2) print('*' * 100)
def __init__(self, user): super(MainWindow, self).__init__() # 使用super函数可以实现子类使用父类的方法 self.setWindowTitle("豆瓣电影推荐系统") self.setWindowIcon(QIcon('../douban.jpg')) # 设置窗口图标 self.resize(1400,800) self.user_df = pd.read_csv('../data/douban_users.csv') self.user_df = self.user_df.iloc[:,[1,2,3]] self.movies_df = pd.read_csv('../data/doubanMovies.csv', encoding='utf-8') self.movies_df = self.movies_df.iloc[:,[0,1,6,15,16]] self.movies_df = self.movies_df.drop_duplicates(subset='url') self.movies_df = self.movies_df.rename(columns={'Unnamed: 0': 'Movie_ID'}) self.user = user self.user_movie_num = len(list(self.user_df[self.user_df['user_id'] == user].movie_id)) self.user_movie_num_show = str(self.user_movie_num) self.USER_PWD = dict() self.USER_Label = dict() f = open('../data/users_info.csv', 'a+', encoding='utf-8') f.seek(0) # 文件指针指向开头 for line in f: self.USER_PWD[line.split(',')[0]] = line.split(',')[1].strip() # 存入用户名密码 try: self.USER_Label[line.split(',')[0]] = line.split(',')[2].strip() # 存入用户标签 except: self.USER_Label[line.split(',')[0]] = None f.close() self.user_label = self.USER_Label[user] self.welcome_label = QLabel(self) self.welcome_label.setText("<h1>欢迎您! " + self.user + "</h1>") self.recommend_label = QLabel("<h1>根据您的喜好为您推荐以下电影:</h1>", self) self.recommend_table = QTableWidget(self) self.hot_movies_label = QLabel("<h1>热门电影:</h1>", self) self.hot_movies_table = QTableWidget(self) self.user_center = QPushButton("个人主页", self) self.user_center.clicked.connect(lambda: self.show_user_center(user, self.user_movie_num_show, self.user_label)) self.movie_detailed_button = QPushButton("电影详细页面", self) self.movie_detailed_button.clicked.connect(self.movie_detailed) self.search_button = QPushButton("电影搜索", self) self.search_button.clicked.connect(self.search) self.hot_rec_movies = Recommendation.rec_hot_movies() # 热门电影 self.rec_movies = Recommendation.find_user_like(self.user) # 推荐电影 self.v1_layout = QVBoxLayout() self.v2_layout = QVBoxLayout() self.h1_layout = QHBoxLayout() self.h2_layout = QHBoxLayout() self.h_layout = QHBoxLayout() self.h1_layout.addWidget(self.welcome_label) self.h1_layout.addWidget(self.user_center) self.v1_layout.addLayout(self.h1_layout) self.v1_layout.addWidget(self.recommend_label) self.v1_layout.addWidget(self.recommend_table) self.h2_layout.addWidget(self.hot_movies_label) self.h2_layout.addWidget(self.movie_detailed_button) self.h2_layout.addWidget(self.search_button) self.v2_layout.addLayout(self.h2_layout) self.v2_layout.addWidget(self.hot_movies_table) self.h_layout.addLayout(self.v1_layout) self.h_layout.addLayout(self.v2_layout) self.setLayout(self.h_layout) self.rec_movies_table_init() self.hot_movies_table_init() self.set_hot_movie_table(self.hot_rec_movies) self.set_rec_movies_table(self.rec_movies)
def getUserRecommendation(): global currentUser trackObjs = Recommendation.getUserReccomendation(currentUser, currentSentimentScore) return trackObjs
1) Created Iterative Version of the Kosaraju Algoritihim """ kso = Kosaraju(tEdge) kso.adjancency_list() kso.dfsiLoop() kso.transpose_graph() print(kso.dfsiRev()) """ Task 4: Recommendation Engine 1) Using tiny directed graph, due to memory constraints for repl.it """ Karan = Recommendation(E, 3) Karan.adjacency_matrix() Karan.matrix_multiplication() print(Karan.length_k_chains(0, 3)) """ Bonus Material: Detect Cycles in directed Graphs """ cycles_in_graph = cycle_DFS_wrapper(tEdge) nocyc = 0 cyc = 0 for key in cycles_in_graph: if cycles_in_graph[key] == 'Cycles Exist at the specified Node': cyc = cyc + 1
print('Number of users who have rated 20 or more items =', len(df_final)) print('Number of unique USERS in final data = ', df_final['UserId'].nunique()) print('Number of unique ITEMS in final data = ', df_final['ProductId'].nunique()) stream = df_final.Score.to_list() L, counter = MisraGries.misra_gries(stream) print('Final Counter for Stream is :', counter) print('Final location/label for Stream is :', L) train_data, test_data = train_test_split(df_final, test_size = 0.3, random_state=0) print("Test data shape: ", test_data.shape) print("Train data shape: ", train_data.shape) print("The top 5 popular recommendations:") print(Recommendation.recommend(df_final)) df_CF = pd.concat([train_data, test_data]).reset_index() p_df = pd.pivot_table(df_CF, index=['UserId'], columns='ProductId', values="Score") p_df.fillna(0, inplace=True) p_df['user_index'] = np.arange(0, p_df.shape[0], 1) p_df.set_index(['user_index'], inplace=True) # SVD U, sigma, Vt = svds(p_df, k=20) sigma = np.diag(sigma) all_user_predicted_ratings = np.dot(np.dot(U, sigma), Vt) # Predicted ratings preds_df = pd.DataFrame(all_user_predicted_ratings, columns = p_df.columns)
from elasticsearch import Elasticsearch #TODO: Configuration host = "http://*****:*****@fantasticsearch.route('/') def index(): term = "*" results = performQuery(term, "", 0) return render_template('index.html', results=results, term=term, aggregationFields = aggregationFields, page=1, listSelection = listSelection) @fantasticsearch.route('/search') def search():
def train( train_data_dir, labels, labels_num ): Recommendation.train( train_data_dir, labels, labels_num )
def recommendation(self): self.rec = Recommendation.Recommendation(self.weatherObj)
def PMTweet(self): self.weatherObj = Weather(GPSLOCATION, AIRPORT, 1) self.rec = Recommendation.Recommendation(self.weatherObj) self.formTweet() TweetPublisher(self.tweetContents)