importnumpyasnpimportpandasaspdimportargsimportcopydefcos_sim(vector_a,vector_b):vector_a=np.mat(vector_a)vector_b=np.mat(vector_b)num=float(vector_a*vector_b.T)denom=np.linalg.norm(vector_a)*np.linalg.norm(vector_b)cos=num/denomsim=0.5+0.5*cosreturnsimdefget_topK(args,K):video_vec=pd.read_csv(args.video_vec_path,header=None)user_vec=pd.read_csv(args.user_vec_path,header=None)user_video_sim_list=[]foriinrange(user_vec.shape[0]):forjinrange(video_vec.shape[1]):user_video_sim=cos_sim(np.array(user_vec.loc[i]),np.array(video_vec[j]))user_video_sim_list.append(user_video_sim)tmp_list=copy.deepcopy(user_video_sim_list)tmp_list.sort()max_sim_index=[user_video_sim_list.index(one)foroneintmp_list[::-1][:K]]print("user:{0}, top K videos:{1}".format(i,max_sim_index))user_video_sim_list=[]if__name__=="__main__":args=args.parse_args()get_topK(args,5)