Java編程通過匹配合并數(shù)據(jù)實例解析(數(shù)據(jù)預(yù)處理)
本文研究的主要是Java編程通過匹配合并數(shù)據(jù)(數(shù)據(jù)預(yù)處理)的相關(guān)內(nèi)容,具體如下。
數(shù)據(jù)描述
以下程序是對如下格式的數(shù)據(jù)進行合并處理。

這個表的每一行表示用戶id及用戶的特征。其中,一個用戶只有一個特征向量,即第一列不會重復(fù)。

這張表的第一列,表示用戶的id,第二列表示用戶所看的電影,第三列表示用戶對電影的打分(1-13分),第四列表示用戶對電影的打分,但分值范圍是1-5分。
問題描述
在做數(shù)據(jù)預(yù)處理時,如何將第二張表添加上用戶特征呢?其實,方法很簡單,將第二張表的用戶id與第一張表的用戶id進行匹配就行。合并結(jié)果如下圖所示。

數(shù)據(jù)處理程序
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:合肥工業(yè)大學(xué) 管院學(xué)院 錢洋
* email:1563178220@qq.com
*/
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");
//匹配結(jié)果
match(userproductscore,userfeaturemap);
}
}
總結(jié)
以上就是本文關(guān)于Java編程通過匹配合并數(shù)據(jù)實例解析(數(shù)據(jù)預(yù)處理)的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
AJAX?SpringBoot?前后端數(shù)據(jù)交互的項目實現(xiàn)
本文主要介紹了AJAX?SpringBoot?前后端數(shù)據(jù)交互的項目實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
SpringBoot使用SchedulingConfigurer實現(xiàn)多個定時任務(wù)多機器部署問題(推薦)
這篇文章主要介紹了SpringBoot使用SchedulingConfigurer實現(xiàn)多個定時任務(wù)多機器部署問題,定時任務(wù)多機器部署解決方案,方式一拆分,單獨拆分出來,單獨跑一個應(yīng)用,方式二是基于aop攔截處理(搶占執(zhí)行),只要有一個執(zhí)行,其它都不執(zhí)行,需要的朋友可以參考下2023-01-01
Spring如何使用PropertyPlaceholderConfigurer讀取文件
這篇文章主要介紹了Spring如何使用PropertyPlaceholderConfigurer讀取文件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12

