Another approach is to implement a matrix using a program-defined class along the lines of: However, this technique is rarely used in ML scenarios. ![]() Note that unlike most languages, C# supports a true built-in matrix object. Public static double MatCreate(int rows, int cols)ĭouble m = Utils.MatCreate(4, 3) // 4x3Īll cells of a matrix created in this way will be initialized by default to 0.0 values. The demo program defines a helper method in class Utils to create a matrix: The anatomy of a 3x4 array-of-arrays style matrix named d is shown in the bottom part of Figure 2. Technically, the matrix is an array named m that has four cells, and each cell is a reference to an array with three cells. M = new double // 3 columns per rowĬonceptually, the code creates a matrix named m that has four rows and three columns. For example: double m = new double // 4 rows The most common way to create a C# matrix is to use an array of arrays. NET System.Array class has an Array.Copy() method that you can use to make an independent copy of a vector. For example:ĭevelopers who prefer a functional style of programming can avoid using the reference mechanism to modify a vector and could write code like this:Īlternatively, the. Utils.MatLoad(fn, 4, new int Ī useful consequence of vectors being references is that a function can modify its array parameter. Matrices Demo Program Structure using System Ĭonsole.WriteLine("Begin vectors and matrices demo") Ĭonsole.WriteLine("Creating a vector with 4 cells") Ĭonsole.WriteLine("Creating a 3x4 matrix") Ĭonsole.WriteLine("Loading 4x3 matrix from file") I also added a header line that begins with "//" characters, which specifies the file name. After clicking on the Add button, I entered values 1.0 through 12.0, three per line, separated by tab characters, as shown in Figure 1. Next, in the Solution Explorer window, I right-clicked on the bold-font Matrices project name and selected Add | New Item | Text File and entered "dummy_data.tsv" in the Name field. In the Solution Explorer window, I renamed file Program.cs to the more descriptive MatricesProgram.cs and then in the editor window I renamed class Program to class MatricesProgram to match the file name. Next I added a using statement that references the System.IO namespace so the program can read data from a text file. ![]() I entered "Matrices" as the Project Name, specified C:\VSM on my local machine as the Location (you can use any convenient directory), and checked the "Place solution and project in the same directory" entry.Īfter the template code loaded into the Visual Studio editor, at the top of the editor window I removed all using statements to unneeded namespaces, leaving just the reference to the top-level System namespace. NET Core so it makes sense to develop most C# ML code in that environment. Many of the newer Microsoft technologies, such as the ML.NET code library, specifically target. The code presented in this article will run as a. Next, I selected C# from the Language dropdown control and Console from the Project Type dropdown, and then picked the "Console App (.NET Core)" item. From the main Visual Studio start window I selected the "Create a new project" option. I used the Community (free) edition but any relatively recent version of Visual Studio will work fine. To create the demo program, I launched Visual Studio 2019. The code is also available in the file download that accompanies this article. The complete code for the demo program shown running in Figure 1 is presented in this article. ![]() This article assumes you have intermediate or better skill with C# but doesn't assume you know anything about vectors and matrices or about ML. The demo program concludes by reading 12 values from a text file and storing them into a 4x3 matrix. Next, the demo creates a 3x4 (3 rows, 4 columns) matrix. The demo program begins by creating and displaying a vector with four cells, each initialized to 3.5. A matrix is conceptually a two-dimensional data structure of numeric values. Informally, a vector is an array of numeric values. In this article I get you up to speed with the fundamental knowledge you need to create and modify ML code written using the C# language.Ī good way to see where this article is headed is to take a look at the screenshot of a demo program in Figure 1. And virtually every significant ML technique uses vectors and matrices. A working knowledge of machine learning (ML) is becoming an increasingly important part of many C# developers' skill sets.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |