我正在尝试将输出写入csv文件,但第一个值采用此格式
我使用了ObjectOutputStream。通常第一个值应该是1,1,1,2,2,2,3 .... 这是我的代码,请问任何想法?
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.LogLikelihoodSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
public class ItemRecommend {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("data/test.csv");
ObjectOutputStream oos = new ObjectOutputStream(fos);
try {
DataModel dm = new FileDataModel(new File("data/rated.csv"));
ItemSimilarity sim = new LogLikelihoodSimilarity(dm);
GenericItemBasedRecommender recommender = new GenericItemBasedRecommender(dm, sim);
for (LongPrimitiveIterator items = dm.getItemIDs(); items.hasNext();){
long itemID = (int)(long) items.nextLong();
List<RecommendedItem>recommendations = recommender.mostSimilarItems(itemID, 3);
for(RecommendedItem recommendation : recommendations){
oos.writeObject(itemID + "," + recommendation.getItemID() + "," + recommendation.getValue()+"\n");
//System.out.println(itemID + "," + recommendation.getItemID() + "," + recommendation.getValue());
}
}
} catch (IOException e) {
System.out.println("Error !");
e.printStackTrace();
} catch (TasteException e) {
System.out.println("Taste exception !");
e.printStackTrace();
}
finally {
oos.flush();
oos.close();
}
}
}
答案 0 :(得分:1)
您应该尝试使用FileWriter或PrintWriter