public void ARange() { var np = new NDArray <int>(); np.ARange(3); Assert.IsTrue(np.ToString().Equals("array([0, 1, 2])")); np.ARange(7, 3); Assert.IsTrue(np.ToString().Equals("array([3, 4, 5, 6])")); np.ARange(7, 3, 2); Assert.IsTrue(np.ToString().Equals("array([3, 5])")); }
public void ReShape() { var np = new NDArray <int>(); var np2 = np.ARange(6).ReShape(3, 2); Assert.IsTrue(np2.ToString().Equals("array([[0, 1], [2, 3], [4, 5]])")); }
public void ReshapeNegative() { var np = new NDArray <int>(); np.ARange(12); np.ReShape(-1, 2); Assert.IsTrue(np.Shape[0] == 6); Assert.IsTrue(np.Shape[1] == 2); np.ARange(12); np.ReShape(2, -1); Assert.IsTrue(np.Shape[0] == 2); Assert.IsTrue(np.Shape[1] == 6); np.ARange(12); np.ReShape(1, 3, 4); np.ReShape(-1, 3); Assert.IsTrue(np.Shape[0] == 4); Assert.IsTrue(np.Shape[1] == 3); np.ARange(12); np.ReShape(1, 3, 4); np.ReShape(3, -1); Assert.IsTrue(np.Shape[0] == 3); Assert.IsTrue(np.Shape[1] == 4); np.ARange(100 * 100 * 3); np.ReShape(100, 100, 3); np.ReShape(-1, 3); Assert.IsTrue(np.Shape[0] == 10000); Assert.IsTrue(np.Shape[1] == 3); np.ARange(15801033); np.ReShape(2531, 2081, 3); np.ReShape(-1, 3); Assert.IsTrue(np.Shape[0] == 5267011); Assert.IsTrue(np.Shape[1] == 3); }
public void SeedsClassificationTest() { var filename = @"data/seeds_dataset.csv"; var np = new NDArray <int>(); var n_hidden_nodes = 5; // nodes in hidden layers var l_rate = 0.6; // learning rate var n_epochs = 1000; // number of training epochs var n_folds = 4; // number of folds for cross-validation /* ================================= * Read data (X,y) and normalize X * ================================= */ Console.WriteLine($"Reading '{filename}'..."); (var X, var y) = Utils.ReadCsv(filename); // read as matrix of floats and int // normalize X.Normalize(); // extract shape of X (var N, var d) = X.Shape; var nClasses = y.Unique().Length; Console.WriteLine($"X.shape = {X.Shape}, y.shape = {y.Shape}"); Console.WriteLine($"size = {X.Data.Count}, dimesion = {X.NDim}, number of classes = {nClasses}"); Console.WriteLine($"hidden nodes = {n_hidden_nodes}, learning rate = {l_rate}, epochs = {n_epochs}"); /* =================================== * Create cross-validation folds * These are a list of a list of indices for each fold * =================================== */ var idx_all = np.ARange(N, 0); var idx_folds = Utils.CrossValFolds(N, 4); /* =================================== * Train and evaluate the model on each fold * =================================== */ // List<acc_train, acc_test = list(), list() # training/test accuracy score Console.WriteLine("Training and cross-validating..."); List <double> acc_train = new List <double>(); List <double> acc_test = new List <double>(); for (int i = 0; i < n_folds; i++) { // Collect training and test data from folds var idx_test = idx_folds[i]; var idx_train = idx_all.Delete(idx_test); var X_train = X[idx_train]; var y_train = y[idx_train]; var X_test = X[idx_test]; var y_test = y[idx_test]; // Build neural network classifier model and train NetworkModel model = new NetworkModel(); model.Layers.Add(new NeuralLayer(d, "INPUT")); model.Layers.Add(new NeuralLayer(n_hidden_nodes, "HIDDEN")); model.Layers.Add(new NeuralLayer(nClasses, "OUTPUT")); model.Build(); model.Train(X_train, y_train, iterations: n_epochs, learningRate: l_rate); // Make predictions for training and test data var y_train_predict = model.Predict(X_train); var y_test_predict = model.Predict(X_test); acc_train.Add(100 * y_train.Sum(y_train_predict) / y_train.Length); acc_test.Add(100 * y_test.Sum(y_test_predict) / y_test.Length); Console.WriteLine($"Fold {i + 1}/{n_folds}: train acc = {acc_train.Last()}%, test acc = {acc_test.Last()}% (n_train = {X_train.Length}, n_test = {X_test.Length})"); } Console.WriteLine($"Avg train acc = {acc_train.Average()}%, avg test acc = {acc_test.Average()}%"); }