我寫使用WEKA庫寫入雙到CSV更改Java代碼來編寫雙[]到
- 訓練的分類算法
- 運行的Java程序CSV(用例= WEKA庫)用訓練算法
- 寫出來的結果爲.csv文件
問題上未標記的數據集的預測是,它目前寫出離散分類結果(即whic h類別算法猜測一行所在)。我想要寫出給定類別的概率(例如,如果我將行分類爲「垃圾郵件」或「不是垃圾郵件」,那麼我希望得到垃圾郵件的可能性)。
我的理解是,要做到這一點,我需要在我的代碼中使用distributionForInstance
而不是classifyInstance
。從WEKA:
如果你有興趣的分佈在所有的類,使用方法 distributionForInstance(實例)。此方法返回一個雙重 數組,其中包含每個類的概率。
我遇到的問題是,與classifyInstance
我對付double
數據類型和distributionForInstance
我處理的double[]
數據類型,顯然不是正確地調整我的代碼。
這裏是寫出謹慎預測的工作代碼:
public class runPredictions {
public static void runPredictions(ArrayList al2) throws IOException, Exception{
// Retrieve objects
Instances newTest = (Instances) al2.get(0);
Classifier clf = (Classifier) al2.get(1);
// Print status
System.out.println("Generating predictions...");
// create copy
Instances labeled = new Instances(newTest);
// label instances
for (int i = 0; i < newTest.numInstances(); i++) {
double clsLabel = clf.classifyInstance(newTest.instance(i));
labeled.instance(i).setClassValue(clsLabel);
}
System.out.println("Predictions complete! Writing output file to csv...");
BufferedWriter outFile = new BufferedWriter(new FileWriter("C:/Users/hackr/Desktop/silverbullet_output.csv"));
for (int i = 0; i < labeled.size(); i++)
{
outFile.write(labeled.get(i).toString());
outFile.write("\n");
}
System.out.println("Output file written.");
System.out.println("Completed successfully!");
outFile.close();
}
}
現在我工作的代碼有以下幾點:
並引發
索引越界
錯誤。
我也移動了創建clsLabel
,因爲顯然當數據類型發生變化時它找不到符號,除非我將它移動到for
循環內。
基於粗略的一瞥,它可能的索引不排隊,所以'我'可能會導致你超出界限。該函數返回結果數組,而不是存儲在索引「i」處的單個結果。您將需要遍歷結果集以獲得您期望的結果。 'for(double d:clsLabel){write(Double.toString(d))}' – Brendan
@HackR(當它使用「 - 」時它會截斷你的名字)。這可能不是全部,但它是我相信的一個開始。如果有效,我會將我的評論改寫爲答案。 – Brendan
@Brendan更新 - 是的,完全工作! :)謝謝 –