Пример #1
0
        public void TestLoad()
        {
            var res = IrisDataset.Load();

            Assert.AreEqual(res.Data.Shape(), Tuple.Create(150, 4));
            Assert.AreEqual(res.Target.Length, 150);
        }
Пример #2
0
        /// <summary>
        /// Make some classification predictions on a toy dataset using a SVC
        ///
        /// If binary is True restrict to a binary classification problem instead of a
        /// multiclass classification problem
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="binary"></param>
        private static Tuple <int[], int[], Matrix <double> > MakePrediction(
            Matrix <double> x = null,
            int[] y           = null,
            bool binary       = false)
        {
            if (x == null && y == null)
            {
                // import some data to play with
                var dataset = IrisDataset.Load();
                x = dataset.Data;
                y = dataset.Target;
            }

            if (binary)
            {
                // restrict to a binary classification task
                x = x.RowsAt(y.Indices(v => v < 2));
                y = y.Where(v => v < 2).ToArray();
            }

            int nSamples  = x.RowCount;
            int nFeatures = x.ColumnCount;
            var rng       = new Random(37);

            int[] p = Shuffle(rng, Enumerable.Range(0, nSamples).ToArray());
            x = x.RowsAt(p);
            y = y.ElementsAt(p);
            var half = nSamples / 2;

            // add noisy features to make the problem harder and avoid perfect results
            rng = new Random(0);
            x   = x.HStack(DenseMatrix.CreateRandom(nSamples, 200, new Normal {
                RandomSource = rng
            }));

            // run classifier, get class probabilities and label predictions
            var clf = new Svc <int>(kernel: Kernel.Linear, probability: true);

            clf.Fit(x.SubMatrix(0, half, 0, x.ColumnCount), y.Take(half).ToArray());
            Matrix <double> probasPred = clf.PredictProba(x.SubMatrix(half, x.RowCount - half, 0, x.ColumnCount));

            if (binary)
            {
                // only interested in probabilities of the positive case
                // XXX: do we really want a special API for the binary case?
                probasPred = probasPred.SubMatrix(0, probasPred.RowCount, 1, 1);
            }

            var yPred = clf.Predict(x.SubMatrix(half, x.RowCount - half, 0, x.ColumnCount));
            var yTrue = y.Skip(half).ToArray();

            return(Tuple.Create(yTrue, yPred, probasPred));
        }
Пример #3
0
        public void TestInitialize()
        {
            var diabetes = DiabetesDataset.Load();

            xDiabetes = diabetes.Data.SubMatrix(0, 200, 0, diabetes.Data.ColumnCount);
            yDiabetes = MatrixExtensions.ToColumnMatrix(diabetes.Target.SubVector(0, 200));
            //ind = np.arange(X_diabetes.shape[0])
            //Random rng = new Random(0);
            //rng.shuffle(ind)
            //ind = ind[:200]
            //X_diabetes, y_diabetes = X_diabetes[ind], y_diabetes[ind]
            var iris = IrisDataset.Load();

            xIris = SparseMatrix.OfMatrix(iris.Data);
            yIris = iris.Target;
        }