Ejemplo n.º 1
0
        /// <summary>
        ///     Prepare a Kaggle submission for Titanic.
        /// </summary>
        /// <param name="dataPath">The data path.</param>
        /// <param name="bestNetwork">The best network.</param>
        /// <param name="cross">The cross validated data.</param>
        public void Submit(string dataPath, RBFNetwork bestNetwork, CrossValidate cross)
        {
            String now            = new DateTime().ToString("yyyyMMddhhmm");
            string trainingPath   = Path.Combine(dataPath, TitanicConfig.TrainingFilename);
            string testPath       = Path.Combine(dataPath, TitanicConfig.TestFilename);
            var    score          = (int)(cross.Score * 10000);
            string submitPath     = Path.Combine(dataPath, "submit-" + now + "_" + score + ".csv");
            string submitInfoPath = Path.Combine(dataPath, "submit-" + now + ".txt");

            using (var file = new StreamWriter(submitInfoPath))
            {
                file.WriteLine("Crossvalidation stats:");
                for (int i = 0; i < cross.Count; i++)
                {
                    CrossValidateFold fold = cross.Folds[i];
                    file.WriteLine("Fold #" + (i + 1) + " : Score: " + fold.Score);
                }
                file.WriteLine("Average Score: " + cross.Score);
                file.WriteLine();
                file.WriteLine(String.Join(",", bestNetwork.LongTermMemory));
            }

            var stats = new TitanicStats();

            NormalizeTitanic.Analyze(stats, trainingPath);
            NormalizeTitanic.Analyze(stats, testPath);

            var ids = new List <String>();
            IList <BasicData> training = NormalizeTitanic.Normalize(stats, testPath, ids,
                                                                    TitanicConfig.InputNormalizeLow,
                                                                    TitanicConfig.InputNormalizeHigh,
                                                                    TitanicConfig.PredictSurvive,
                                                                    TitanicConfig.PredictPerish);

            int idx = 0;

            using (var streamWriter = new StreamWriter(submitPath))
                using (var writer = new CsvWriter(streamWriter))
                {
                    writer.WriteField("PassengerId");
                    writer.WriteField("Survived");
                    writer.NextRecord();

                    foreach (BasicData data in training)
                    {
                        double[] output   = bestNetwork.ComputeRegression(data.Input);
                        int      survived = output[0] > 0.5 ? 1 : 0;

                        writer.WriteField(ids[idx]);
                        writer.WriteField(survived);
                        writer.NextRecord();
                        idx++;
                    }
                }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Fit a RBF model to the titanic.
        /// </summary>
        /// <param name="dataPath">The path that contains the data file.</param>
        public void Process(string dataPath)
        {
            string trainingPath = Path.Combine(dataPath, TitanicConfig.TrainingFilename);
            string testPath     = Path.Combine(dataPath, TitanicConfig.TestFilename);

            IGenerateRandom rnd = new MersenneTwisterGenerateRandom();

            // Generate stats on the titanic.
            var stats = new TitanicStats();

            NormalizeTitanic.Analyze(stats, trainingPath);
            NormalizeTitanic.Analyze(stats, testPath);

            // Get the training data for the titanic.
            IList <BasicData> training = NormalizeTitanic.Normalize(stats, trainingPath, null,
                                                                    TitanicConfig.InputNormalizeLow,
                                                                    TitanicConfig.InputNormalizeHigh,
                                                                    TitanicConfig.PredictSurvive,
                                                                    TitanicConfig.PredictPerish);

            // Fold the data for cross validation.
            _cross = new CrossValidate(TitanicConfig.FoldCount, training, rnd);

            // Train each of the folds.
            for (int k = 0; k < _cross.Count; k++)
            {
                Console.WriteLine("Cross validation fold #" + (k + 1) + "/" + _cross.Count);
                TrainFold(k, _cross.Folds[k]);
            }

            // Show the cross validation summary.
            Console.WriteLine("Crossvalidation summary:");
            int kk = 1;

            foreach (CrossValidateFold fold in _cross.Folds)
            {
                Console.WriteLine("Fold #" + kk + ": " + fold.Score);
                kk++;
            }

            Console.WriteLine("Final, crossvalidated score:" + _cross.Score);
        }