Java programming through matching and merging data instance parsing (data preprocessing), java preprocessing
This article focuses on the content related to matching and merging data (data preprocessing) through Java programming. The details are as follows.
Data Description
The following procedures merge the data in the following format.
Each row in this table represents the user ID and user features. One user has only one feature vector, that is, the first column will not be repeated.
The first column of this table indicates the user's id, the second column indicates the movie you are watching, and the third column indicates the user's score for the movie (1-13 points ), the fourth column indicates the user's score for the movie, but the score range is 1-5.
Problem description
During data preprocessing, how do I add user features to the second table? In fact, the method is very simple. Match the user ID of the second table with the user ID of the first table. Shows the merge result.
Data Processing Program
Package deal; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStreamReader; import java. math. bigDecimal; import java. util. arrayList; import java. util. hashMap; import java. util. list;/** author: Qian Yang * email: 1563178220@qq.com */public class GetPUser {public static List <String> readDocs (String docsPath, String code) thr Ows IOException {BufferedReader reader = new BufferedReader (new InputStreamReader (new FileInputStream (new File (docsPath), code); String s = null; list <String> userproductscore = new ArrayList <String> (); while (s = reader. readLine ())! = Null) {userproductscore. add (s);} reader. close (); return userproductscore;} public static HashMap <String, String> MAPread (String docsPath1, String code1) throws IOException {BufferedReader reader1 = new BufferedReader (new InputStreamReader (new FileInputStream (new File (docsPath1), code1); String s1 = null; HashMap <String, string> userfeaturemap = new HashMap <String, String> (); while (s1 = reader1.readLine ())! = Null) {String arr [] = s1.split ("\ t"); String feature = ""; for (int I = 1; I <arr. length; I ++) {BigDecimal db = new BigDecimal (arr [I]); String ii = db. toPlainString (); feature + = ii + "";} userfeaturemap. put (s1.split ("\ t") [0], feature) ;}reader1.close (); return userfeaturemap;} public static List <String> match (List <String> userproductscore, hashMap <String, String> userfeaturemap) throws IOException {List <String> userscoreandfeature = new ArrayList <> (); for (int I = 0; I <userproductscore. size (); I ++) {// get user idString user_id = userproductscore. get (I ). split ("\ t") [0]; // obtain the user feature String userfeature = userfeaturemap. get (user_id); userscoreandfeature. add (userproductscore. get (I) + "\ t" + userfeature); System. out. println (userproductscore. get (I) + "\ t" + userfeature);} return userscoreandfeature;} public static void main (String [] args) throws IOException {// read two text lists <String> userproductscore = readDocs ("data/train/ydata-ymovies-user-movie-ratings-train-v1_0.txt", "gbk"); HashMap <String, string> userfeaturemap = MAPread ("data/fileofuser/yahoo.txt", "UTF-8"); // match the result (userproductscore, userfeaturemap );}}
Summary
The above is all about parsing (data preprocessing) by matching and merging data instances in Java programming. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!