Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Globalization;
- using System.IO;
- namespace MultiClass
- {
- class Program
- {
- static Dictionary<double,string> ranges = new Dictionary<double,string>();
- static void Main(string[] args)
- {
- Console.WriteLine("starting conversion to multiclass");
- training = convert2MultiClass(
- ConfigurationManager.AppSettings["training_path"],
- ConfigurationManager.AppSettings["multiclass_training_path"]
- );
- Console.WriteLine("program quits");
- }
- class Event
- {
- internal string features;
- internal double weight;
- internal string label;
- }
- static Dictionary<int, Event> training;
- static Dictionary<int, Event> convert2MultiClass(string training_path, string multiclass_training_path)
- {
- Dictionary<int, Event> ret_val = new Dictionary<int, Event>();
- string first_line;
- using (StreamReader sr = new StreamReader(training_path))
- {
- first_line = sr.ReadLine();
- int line = 0;
- string curr_line;
- bool end_training=false;
- while (!end_training)
- {
- curr_line = sr.ReadLine();
- if (curr_line == null || curr_line.Trim().Length == 0)
- {
- end_training = true;
- }
- else
- {
- Event evt = new Event();
- string[] fields = curr_line.Split(',');
- evt.features = string.Join(",",fields,1,fields.Length-3);
- if (!double.TryParse(fields[fields.Length-2], out evt.weight))
- {
- Console.WriteLine("wrong weight for line " + line.ToString());
- }
- evt.label = fields[fields.Length - 1];
- if (evt.label.Equals("s"))
- {
- if (ranges.ContainsKey(evt.weight))
- {
- evt.label = ranges[evt.weight];
- }
- else
- {
- string new_label = (ranges.Count + 1).ToString();
- ranges.Add(evt.weight, new_label);
- evt.label = new_label;
- }
- }
- else
- {
- evt.label = "0";
- }
- ret_val[line++] = evt;
- }
- }
- }
- using (StreamWriter sw = new StreamWriter(multiclass_training_path))
- {
- sw.WriteLine(first_line);
- for (int i = 0; i < 250000; i++)
- {
- sw.WriteLine((100000 + i).ToString() + ","+ret_val[i].features
- + "," +ret_val[i].weight.ToString(CultureInfo.InvariantCulture)
- + "," + ret_val[i].label
- );
- }
- }
- return ret_val;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement