風險大腦-支付風險識別天池大賽(五)處理模型輸出並提交結果、“榜上有名”

Ps:若不參加比賽的同志們可忽略此篇。


官方大賽提交要求:





模型得到的結果:

        隨機森林以及其他分類算法模型會輸出當前的預測值和結果爲此值的概率。如下所示:



        假定這裏我們規定正樣本爲0(即無風險的支付行爲),負樣本爲1(即有風險的支付行爲)。第一個概率表示預測結果爲正樣本的概率,第二個概率表示預測結果爲負樣本的概率,若預測結果爲正樣本的概率小於0.5,則表示該樣本是正樣本的概率很小,即模型會預測它爲負樣本(1)。見上圖。


處理模型輸出的結果:

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;

public class ProcessResultRiskxx {


	public static void main(String[] args) throws IOException {

		// 待處理的結果文件(無ID)
		String testResultFilePath = "D:\\ant\\result_randomforest_deep15.csv";
		// b榜測試文件(用於提取ID)
		String testBFilePath = "D:\\ant\\atec_anti_fraud_test_b_convert.csv";
		
		// 最終提交的文件
		String finalFilePath = "D:\\ant\\result_randomforest_deep15_final.csv";
		
		File testReadFile = new File(testResultFilePath);
		File testBFile = new File(testBFilePath);
		File finalFile = new File(finalFilePath);

		BufferedReader brTestResultFile = new BufferedReader(new InputStreamReader(new FileInputStream(testReadFile)));
		BufferedReader brTestBFile = new BufferedReader(new InputStreamReader(new FileInputStream(testBFile)));
		BufferedWriter bwFinalFile = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(finalFile)));

		String strLine = "";
		
		// 表頭不要
		String strTestResult = brTestResultFile.readLine();
		String strTestB = brTestBFile.readLine();
		
		bwFinalFile.write("id,score\n");

		// 逐行讀入並替換引號,最後逐行寫入
		try {
			
			while ((strTestResult = brTestResultFile.readLine()) != null
					&& (strTestB = brTestBFile.readLine()) != null) {
				
				// 讀取id、風險類型
				String id = strTestB.split(",")[0];
		
				double probability = Double.parseDouble(strTestResult.split(",")[0].substring(2));
				
				// 88577行
				probability = 1.0 - probability;
				
				// 不以科學記數法記錄
				DecimalFormat decimalFormat = new DecimalFormat("#,##0.0000000000000000");
				String formattedProbability = decimalFormat.format(probability);
				
				String tmpStr = id + "," + formattedProbability + "\n";
				// 輸出控制檯
				System.out.println(id + "----" + formattedProbability);
				// 寫入文件
				bwFinalFile.write(tmpStr);
				
			}

			bwFinalFile.flush();
			// 關閉流
			brTestResultFile.close();
			brTestBFile.close();
			bwFinalFile.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			brTestResultFile.close();
			brTestBFile.close();
			bwFinalFile.close();
		}
	}
}

        本次因未將id列送入模型訓練,故需要在最後提交環節前加上id,表示唯一標示的交易。邏輯比較簡單大家隨便看下應該就懂了。這裏爲了方便大家提交,就貼出來了。


好,至此,將上一步文件提交後,英雄榜上應該就會有你的大名了!說實話,還是挺有意思的,我就是在這個環節中了排行榜的毒,熬夜刷了4、5次榜。哭大家需謹慎!!!



發佈了40 篇原創文章 · 獲贊 117 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章