本文研究的主要是java編程通過匹配合并數據(數據預處理)的相關內容,具體如下。
數據描述
以下程序是對如下格式的數據進行合并處理。
這個表的每一行表示用戶id及用戶的特征。其中,一個用戶只有一個特征向量,即第一列不會重復。
這張表的第一列,表示用戶的id,第二列表示用戶所看的電影,第三列表示用戶對電影的打分(1-13分),第四列表示用戶對電影的打分,但分值范圍是1-5分。
問題描述
在做數據預處理時,如何將第二張表添加上用戶特征呢?其實,方法很簡單,將第二張表的用戶id與第一張表的用戶id進行匹配就行。合并結果如下圖所示。
數據處理程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
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:合肥工業大學 管院學院 錢洋 * email:[email protected] */ public class getpuser { public static list<string> readdocs(string docspath,string code) throws 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++) { //獲取用戶id string user_id=userproductscore.get(i).split( "\t" )[ 0 ]; //獲取用戶特征 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 { //讀取兩個文本 list<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(userproductscore,userfeaturemap); } } |
總結
以上就是本文關于java編程通過匹配合并數據實例解析(數據預處理)的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/qy20115549/article/details/59110167