Ejemplo n.º 1
0
        public ApiModule()
        {
            // CORS
            After.AddItemToEndOfPipeline((ctx) => ctx.Response
                                         .WithHeader("Access-Control-Allow-Origin", "*")
                                         .WithHeader("Access-Control-Allow-Methods", "POST,GET")
                                         .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type"));

            Get("/classify", ctx =>
            {
                var req = this.Bind <ClassifyRequest>();

                if (req.Month == null || req.Day == null || req.DayOfWeek == null ||
                    req.Airline == null || req.Airport == null)
                {
                    return("{\"error\": \"must provide all parameters\"}");
                }

                // build example based on params
                var example = new Example("");

                example.AddAttribute(Attrs.Month, int.Parse(req.Month));
                example.AddAttribute(Attrs.Day, int.Parse(req.Day));
                example.AddAttribute(Attrs.DayOfWeek, int.Parse(req.DayOfWeek));
                example.AddAttribute(Attrs.Airline, req.Airline);
                example.AddAttribute(Attrs.Airport, req.Airport);

                var result = WebServer.Tree.Classify(example);

                var value = result.classification.ToLower();
                var ratio = result.randomnessRatio;

                return("{\"classification\": " + value + ", \"randomnessRatio\": " + ratio + "}");
            });
        }
Ejemplo n.º 2
0
        private static DecisionTree.DecisionTree CreateTree(string connStr, int limit)
        {
            MySqlConnection connection;

            try
            {
                connection = new MySqlConnection(connStr);
                connection.Open();

                MySqlCommand    cmd;
                MySqlDataReader reader;
                String          listStr = null;

                int maxId = -1;
                {
                    cmd                = new MySqlCommand();
                    cmd.Connection     = connection;
                    cmd.CommandText    = "SELECT MAX(id) FROM flights";
                    cmd.CommandTimeout = int.MaxValue;
                    cmd.Prepare();

                    reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        maxId = reader.GetInt32(0);
                    }

                    reader.Close();
                }


                if (limit > -1)
                {
                    Random rand           = new Random();
                    var    listStrBuilder = new StringBuilder("(");

                    Console.WriteLine("Creating list string...");
                    for (int i = 0; i < limit; i++)
                    {
                        int id = rand.Next(maxId) + 1;
                        listStrBuilder.Append(id);
                        if (i != limit - 1)
                        {
                            listStrBuilder.Append(",");
                        }
                    }

                    listStrBuilder.Append(")");
                    listStr = listStrBuilder.ToString();
                }

                Console.WriteLine("Querying flights...");

                cmd             = new MySqlCommand();
                cmd.Connection  = connection;
                cmd.CommandText = "SELECT * FROM flights";
                if (listStr != null)
                {
                    cmd.CommandText += " WHERE ID in " + listStr;
                }
                cmd.CommandTimeout = int.MaxValue;
                cmd.Prepare();


                Stopwatch watch = new Stopwatch();
                watch.Start();

                List <Example> trainingSet = new List <Example>();

                Console.WriteLine("Retrieving data set...");

                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    var delay = reader.GetInt32("DEPARTURE_DELAY");

                    // it is considered delayed if it leaves more than 5 mins
                    // after scheduled departure time
                    var isDelayed = delay > 5;

                    // construct the example with the classification as
                    // "true" or "false" depending on if it was delayed
                    var example = new Example(isDelayed + "");

                    // fetch attributes
                    var month     = reader.GetInt32("MONTH");
                    var day       = reader.GetInt32("DAY");
                    var dayOfWeek = reader.GetInt32("DAY_OF_WEEK");
                    var airline   = reader.GetString("AIRLINE");
                    var airport   = reader.GetString("ORIGIN_AIRPORT");

                    // add attributes
                    example.AddAttribute(Attrs.Month, month);
                    example.AddAttribute(Attrs.Day, day);
                    example.AddAttribute(Attrs.DayOfWeek, dayOfWeek);
                    example.AddAttribute(Attrs.Airline, airline);
                    example.AddAttribute(Attrs.Airport, airport);

                    trainingSet.Add(example);
                }

//
//                Console.WriteLine("Selecting " + limit + " random entries from data set...");
//                Random rand = new Random();
//
//                var filteredSet = new List<Example>();
//
//                while (filteredSet.Count < limit)
//                {
//                    int index = rand.Next(trainingSet.Count);
//                    filteredSet.Add(trainingSet[index]);
//                    trainingSet.RemoveAt(index);
//                }
//
//                // discard the data
//                trainingSet.Clear();


                Console.WriteLine("Building decision tree...");

                var tree = new DecisionTree.DecisionTree();
                tree.BuildTree(trainingSet);

                Console.WriteLine("Done.");

                Console.Write("Elapsed Time (ms): ");
                Console.WriteLine(watch.ElapsedMilliseconds);

                return(tree);
            }
            catch (MySqlException ex)
            {
                Console.WriteLine(ex.ToString());
                return(null);
            }
        }