예제 #1
0
func AlgorithmRunOnDataSet(classifier algo.Classifier, train_dataset, test_dataset *core.DataSet, pred_path string, params map[string]string) (float64, []*eval.LabelPrediction) {

	if train_dataset != nil {
		classifier.Train(train_dataset)
	}

	predictions := []*eval.LabelPrediction{}
	var pred_file *os.File
	if pred_path != "" {
		pred_file, _ = os.Create(pred_path)
	}
	for _, sample := range test_dataset.Samples {
		prediction := classifier.Predict(sample)
		if pred_file != nil {
			pred_file.WriteString(strconv.FormatFloat(prediction, 'g', 5, 64) + "\n")
		}
		predictions = append(predictions, &(eval.LabelPrediction{Label: sample.Label, Prediction: prediction}))
	}
	if pred_path != "" {
		defer pred_file.Close()
	}

	auc := eval.AUC(predictions)
	return auc, predictions
}
예제 #2
0
func AlgorithmRun(classifier algo.Classifier, train_path string, test_path string, pred_path string, params map[string]string) (float64, []*eval.LabelPrediction, error) {
	global, _ := strconv.ParseInt(params["global"], 10, 64)
	train_dataset := core.NewDataSet()

	err := train_dataset.Load(train_path, global)

	if err != nil {
		return 0.5, nil, err
	}

	test_dataset := core.NewDataSet()
	err = test_dataset.Load(test_path, global)
	if err != nil {
		return 0.5, nil, err
	}
	classifier.Init(params)
	auc, predictions := AlgorithmRunOnDataSet(classifier, train_dataset, test_dataset, pred_path, params)

	return auc, predictions, nil
}
예제 #3
0
func AlgorithmTest(classifier algo.Classifier, test_path string, pred_path string, params map[string]string) (float64, []*eval.LabelPrediction, error) {
	global, _ := strconv.ParseInt(params["global"], 10, 64)

	model_path, _ := params["model"]
	classifier.Init(params)
	if model_path != "" {
		classifier.LoadModel(model_path)
	} else {
		return 0.0, nil, nil
	}

	test_dataset := core.NewDataSet()
	err := test_dataset.Load(test_path, global)
	if err != nil {
		return 0.0, nil, err
	}

	auc, predictions := AlgorithmRunOnDataSet(classifier, nil, test_dataset, pred_path, params)

	return auc, predictions, nil
}
예제 #4
0
func predict(res http.ResponseWriter, req *http.Request) {
	driverID, _ := strconv.Atoi(req.URL.Query().Get(":id"))
	var model algo.Classifier
	if _, ok := driversModels[driverID]; !ok {
		model = NewModel(driverID)
	} else {
		model = driversModels[driverID]
	}
	fs := make(map[string]float64)
	fs["hour"], _ = strconv.ParseFloat(req.URL.Query().Get("hour"), 64)
	fs["dayOfWeek"], _ = strconv.ParseFloat(req.URL.Query().Get("dayOfWeek"), 64)
	fs["distance_from_order_on_creation"], _ = strconv.ParseFloat(req.URL.Query().Get("distance_from_order_on_creation"), 64)
	fs["driver_location_key"], _ = strconv.ParseFloat(req.URL.Query().Get("driver_location_key"), 64)
	fs["driver_latitude"], _ = strconv.ParseFloat(req.URL.Query().Get("driver_latitude"), 64)
	fs["driver_longitude"], _ = strconv.ParseFloat(req.URL.Query().Get("driver_longitude"), 64)
	fs["origin_location_key"], _ = strconv.ParseFloat(req.URL.Query().Get("origin_location_key"), 64)
	fs["origin_latitude"], _ = strconv.ParseFloat(req.URL.Query().Get("origin_latitude"), 64)
	fs["origin_longitude"], _ = strconv.ParseFloat(req.URL.Query().Get("origin_longitude"), 64)
	sample := NewSample(fs)
	pr := model.Predict(sample)
	renderJSON(res, http.StatusOK, map[string]interface{}{"predict": pr})
}
예제 #5
0
func AlgorithmTrain(classifier algo.Classifier, train_path string, params map[string]string) error {
	global, _ := strconv.ParseInt(params["global"], 10, 64)
	train_dataset := core.NewDataSet()

	err := train_dataset.Load(train_path, global)

	if err != nil {
		return err
	}

	classifier.Init(params)
	classifier.Train(train_dataset)

	model_path, _ := params["model"]

	if model_path != "" {
		classifier.SaveModel(model_path)
	}

	return nil
}