Collaborative filtering simply uses the preferences of a group with similar interests and common experience to recommend information that users are interested in. Individuals respond to the information to a considerable degree (such as rating) through cooperative mechanisms and record it to achieve the purpose of filtering to help others filter information. The response is not necessarily limited to the records of information that are particularly interested, and the records of information that are particularly uninterested are also very important.
Collaborative filtering can be divided into rating or social filtering. Collaborative filtering is very popular in the global Internet field with its excellent speed and robustness.
The core idea of UserCF is to simulate vector similarity based on user data. Based on this similarity, we find similar users of the specified user, and then recommend things that similar users have bought but not bought by the specified user to the specified user. The calculation of recommendation degree also combines the similarity between similar users and the specified user to the specified user. Note that we default to the user's hidden feedback behavior, so the influence factor of each item is 1 by default.
package cn.csu.CFUtils;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Scanner;import java.util.Set;/** * Implementation of A abd B ac C be D cde * @author Administrator * */public class UserCF {public static void main(String[] args) {/** * Enter user-->Item entry One user corresponds to multiple items* User ID Item ID collection* A abd * B ac * C be * D cde */Scanner scanner = new Scanner(System.in);System.out.println("Input the total users number:");//Input the total number of users int N = scanner.nextint();int[][] sparseMatrix = new int[N][N];//Create a user sparse matrix for user similarity calculation [similarity matrix] Map<String, Integer> userItemLength = new HashMap<>();//Storage the total number of different items corresponding to each user eg: A 3 Map<String, Set<String>> itemUserCollection = new HashMap<>();//Create an inverted table from items to users eg: a AB Set<String> items = new HashSet<>();//Auxiliary storage item collection Map<String, Integer> userID = new HashMap<>();//Auxiliary storage userID map Map<Integer, String> idUser = new HashMap<>();//Assisted to store the user map corresponding to each ID System.out.println("Input user--items mapping information:<eg:A ab d>");scanner.nextLine();for (int i = 0; i < N ; i++){//Process N user input data in sequence with space intervals String[] user_item = scanner.nextLine().split(");int length = user_item.length;userItemLength.put(user_item[0], length-1);//eg: A 3 userID.put(user_item[0], i);//Create a correspondence between the user ID and the sparse matrix idUser.put(i, user_item[0]);//Create an item-user inverted table for (int j = 1; j < length; j ++){if(items.contains(user_item[j])){//If the corresponding item has been included-user map, directly add the corresponding user itemUserCollection.get(user_item[j]).add(user_item[0]);} else{//Otherwise create the corresponding item-user collection map items.add(user_item[j]);itemUserCollection.put(user_item[j], new HashSet<String>());//Create item--User reverse relationship itemUserCollection.get(user_item[j]).add(user_item[0]);}}}System.out.println(itemUserCollection.toString());//Calculate the similarity matrix [sparse] Set<Entry<String, Set<String>>> entrySet = itemUserCollection.entrySet();Iterator<Entry<String, Set<String>>> iterator = entrySet.iterator(); while(iterator.hasNext()){Set<String> commonUsers = iterator.next().getValue(); for (String user_u : commonUsers) {for (String user_v : commonUsers) {if(user_u.equals(user_v)){continue;}sparseMatrix[userID.get(user_u)][userID.get(user_v)] += 1;//Calculate the total number of items that have positive feedback from both user u and user v}}}}System.out.println(userItemLength.toString());System.out.println("Input the user for recommendation:<eg:A>");String recommendUser = scanner.nextLine();System.out.println(userID.get(recommendUser));//Calculate the similarity between users [cosine similarity] int recommendUserId = userID.get(recommendUser); for (int j = 0;j < sparseMatrix.length; j++) {if(j != recommendUserId){System.out.println(idUser.get(recommendUserId)+"--"+idUser.get(j)+"Similarity:"+sparseMatrix[recommendUserId][j]/Math.sqrt(userItemLength.get(idUser.get(recommendUserId))*userItemLength.get(idUser.get(j))));}}//Calculate the item recommendation degree of the specified user's recommendedUser for (String item: items){//Transtraight each item Set<String> users = itemUserCollection.get(item);//Get all user collections for purchasing the current item if(!users.contains(recommendUser)){//If the recommended user does not purchase the current item, the recommendation degree calculation is performed double itemRecommendDegree = 0.0; for (String user: users){itemRecommendDegree += sparseMatrix[userID.get(recommendUser)][userID.get(user)]/Math.sqrt(userItemLength.get(recommendUser)*userItemLength.get(user));//Recommendation degree calculation}System.out.println("The item "+item+" for "+recommendUser +"'s recommended degree:"+itemRecommendDegree);}}scanner.close();}}result:
Input the total users number:6Input user--items mapping information:<eg:A ab d>aassdddjshgjh2415231424dsjkj dklsjf ladkjsfdf8g78dfg78 8787 48787 sdfasd{dklsjf=[dsjkj], sdfasd=[48787], 8787=[df8g78dfg78], ladkjsf=[dsjkj]}{aassdd=0, df8g78dfg78=1, 48787=1, 2415231424=0, djshgjh=0, dsjkj=2}Input the user for recommendation:<eg:A>aassdd0aassdd--djshgjh Similarity:NaNaassdd--2415231424 Similarity:NaNaassdd--dsjkj Similarity:NaNaassdd--df8g78dfg78 Similarity:NaNaassdd--48787 Similarity:NaNTThe item dklsjf for aassdd's recommended degree:NaNTThe item sdfasd for aassdd's recommended degree:NaNTThe item 8787 for aassdd's recommended degree:NaNTThe item ladkjsf for aassdd's recommended degree:NaNSummarize
The above is the entire content of this article about Java programming to implement user-based collaborative filtering recommended algorithm code examples. I hope it will be helpful to everyone. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!