csharpfftfsharpintegrationinterpolationlinear-algebramathdifferentiationmatrixnumericsrandomregressionstatisticsmathnet
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
244 lines
13 KiB
244 lines
13 KiB
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8"/>
|
|
<title>Delimited Text Files (CSV & TSV)
|
|
</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
|
<meta name="description" content="Math.NET Numerics, providing methods and algorithms for numerical computations in science, engineering and every day use. .Net 4, .Net 3.5, SL5, Win8, WP8, PCL 47 and 136, Mono, Xamarin Android/iOS."/>
|
|
<meta name="author" content="Christoph Ruegg, Marcus Cuda, Jurgen Van Gael"/>
|
|
|
|
<script src="https://code.jquery.com/jquery-1.8.0.js"></script>
|
|
<script src="https://code.jquery.com/ui/1.8.23/jquery-ui.js"></script>
|
|
<script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/js/bootstrap.min.js"></script>
|
|
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrap-combined.min.css" rel="stylesheet"/>
|
|
|
|
<link type="text/css" rel="stylesheet" href="https://numerics.mathdotnet.com/content/style.css" />
|
|
<style>
|
|
#main table:not(.pre) {
|
|
border: 1px solid #dddddd;
|
|
max-width: 100%;
|
|
border-style: solid;
|
|
border-width: 1px;
|
|
border-color: gray;
|
|
border-collapse: collapse;
|
|
border-right-width: 1px;
|
|
border-bottom-width: 1px;
|
|
margin-top: 15px;
|
|
margin-bottom: 25px;
|
|
}
|
|
#main table:not(.pre) th, #main table:not(.pre) td {
|
|
border: 1px solid #dddddd;
|
|
padding: 6px;
|
|
}
|
|
#main table:not(.pre) th p, #main table:not(.pre) td p {
|
|
margin-bottom: 5px;
|
|
}
|
|
</style>
|
|
<script type="text/javascript" src="https://numerics.mathdotnet.com/content/tips.js"></script>
|
|
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
|
|
<!--[if lt IE 9]>
|
|
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
|
|
<![endif]-->
|
|
|
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="masthead">
|
|
<ul class="nav nav-pills pull-right">
|
|
<li><a href="https://www.mathdotnet.com">Math.NET Project</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com">Math.NET Numerics</a></li>
|
|
<li><a href="https://github.com/mathnet/mathnet-numerics">GitHub</a></li>
|
|
</ul>
|
|
<h3 class="muted">Math.NET Numerics</h3>
|
|
</div>
|
|
<hr />
|
|
<div class="row">
|
|
<div class="span9" id="main">
|
|
|
|
<h1><a name="Delimited-Text-Files-CSV-amp-TSV" class="anchor" href="#Delimited-Text-Files-CSV-amp-TSV">Delimited Text Files (CSV & TSV)</a></h1>
|
|
<p>Likely the most common file format for tabular data, delimited files like CSV store data as text
|
|
with one line per row and values within rows separated by a comma.
|
|
Such text files are supported by virtually all software that deals with tabular data.</p>
|
|
<p>Example:</p>
|
|
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l">1: </span>
|
|
<span class="l">2: </span>
|
|
<span class="l">3: </span>
|
|
</pre></td>
|
|
<td class="snippet"><pre class="fssnip"><code lang="text">A,B,C
|
|
0.5,0.6,98.0
|
|
2.0,3.4,5.3
|
|
</code></pre></td></tr></table>
|
|
<p>Unfortunately there is no universal standard on what character is used as separator and how
|
|
individual values are formatted and escaped. CSV files traditionally use a comma as separator, but this
|
|
causes problems e.g. in Germany where the comma is used as decimal point in numbers. The tabulator
|
|
proves to be a useful alternative, usually denoted by using the TSV extension instead of CSV.
|
|
Other separators like semicolons or colons are common as well.</p>
|
|
<p>Math.NET Numerics provides basic support for delimited files with the <strong>MathNet.Numerics.Data.Text</strong> package,
|
|
which is available on NuGet as separate package and not included in the basic distribution.</p>
|
|
<h2><a name="Reading-a-matrix-from-a-delimited-file" class="anchor" href="#Reading-a-matrix-from-a-delimited-file">Reading a matrix from a delimited file</a></h2>
|
|
<p>The <code>DelimitedReader</code> class provides static functions to read a matrix from a file or string in delimited form.
|
|
It can read from:</p>
|
|
<ul>
|
|
<li>
|
|
<strong>TextReader</strong>: If you have your delimited data already in memory in a string,
|
|
you can use this method using a StringReader.
|
|
</li>
|
|
<li><strong>Stream</strong>: read directly from a stream, e.g. a MemoryStream, FileStream or NetworkStream.</li>
|
|
<li><strong>File Path (string)</strong>: read from a file, specified by the file system path.</li>
|
|
</ul>
|
|
<p>All these functions expect the data type of the matrix to be generated as generic type argument.
|
|
Only Double, Single, Complex and Complex32 are supported.</p>
|
|
<p>Example:</p>
|
|
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l">1: </span>
|
|
<span class="l">2: </span>
|
|
<span class="l">3: </span>
|
|
</pre></td>
|
|
<td class="snippet"><pre class="fssnip highlighted"><code lang="csharp"><span class="k">using</span> MathNet.Numerics.Data.Text;
|
|
|
|
Matrix<<span class="k">double</span>> matrix <span class="o">=</span> DelimitedReader.Read<<span class="k">double</span>>(<span class="s">"data.csv"</span>, <span class="k">false</span>, <span class="s">","</span>, <span class="k">true</span>);
|
|
</code></pre></td></tr></table>
|
|
<p>Unfortunately the lack of standard means that the parsing logic needs to be parametrized accordingly.
|
|
There are ways to automatically profile the provided file to find out the correct parameters automatically,
|
|
but for simplicity the Read functions expects those parameters explicitly as optional arguments:</p>
|
|
<ul>
|
|
<li>
|
|
<strong>sparse</strong>: Whether the the returned matrix should be constructed as sparse (true) or dense (false).<br />
|
|
Default: false.
|
|
</li>
|
|
<li>
|
|
<strong>delimiter</strong>: Number delimiter between numbers of the same line. Supports Regex groups.<br />
|
|
Default: <code>\s</code> (white space).
|
|
</li>
|
|
<li>
|
|
<strong>hasHeaders</strong>: Whether the first row contains column headers or not. If true, the first line will be skipped.<br />
|
|
Default: false.
|
|
</li>
|
|
<li>
|
|
<strong>formatProvider</strong>: The culture to use. It is often a good idea to use InvariantCulture,
|
|
to make the format independent from the local culture.
|
|
Default: null.
|
|
</li>
|
|
</ul>
|
|
<h2><a name="Writing-a-matrix-to-a-delimited-file" class="anchor" href="#Writing-a-matrix-to-a-delimited-file">Writing a matrix to a delimited file</a></h2>
|
|
<p>The dual to the reader above is the <code>DelimitedWriter</code> class that can serialize a matrix
|
|
to a delimited text file, stream or TextWriter.</p>
|
|
<p>The static Write functions accept the following optional arguments to control the output format:</p>
|
|
<ul>
|
|
<li>
|
|
<strong>delimiter</strong>: Number delimiter to write between numbers of the same line.<br />
|
|
Default: <code>\t</code> (tabulator).
|
|
</li>
|
|
<li>
|
|
<strong>columnHeaders</strong>: list of column header strings, or null if no headers should be written.<br />
|
|
Default: null.
|
|
</li>
|
|
<li>
|
|
<strong>format</strong>: The number format to use on each element, similar to what can be provided to Double.ToString().<br />
|
|
Default: null.
|
|
</li>
|
|
<li>
|
|
<strong>formatProvider</strong>: The culture to use. It is often a good idea to use InvariantCulture,
|
|
to make the format independent from the local culture.
|
|
Default: null.
|
|
</li>
|
|
</ul>
|
|
<p>Example:</p>
|
|
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l">1: </span>
|
|
</pre></td>
|
|
<td class="snippet"><pre class="fssnip highlighted"><code lang="csharp">DelimitedWriter.Write(<span class="s">"data.csv"</span>, matrix, <span class="s">","</span>);
|
|
</code></pre></td></tr></table>
|
|
<h2><a name="Alternatives" class="anchor" href="#Alternatives">Alternatives</a></h2>
|
|
<p>The data extension packages also offer other ways to serialize a matrix to a binary stream or file.
|
|
Among others:</p>
|
|
<ul>
|
|
<li><a href="MatrixMarket.html">NIST MatrixMarket text files</a></li>
|
|
<li><a href="MatlabFiles.html">MATLAB Level-5 Mat files</a></li>
|
|
</ul>
|
|
|
|
|
|
</div>
|
|
<div class="span3">
|
|
<ul class="nav nav-list" id="menu">
|
|
|
|
<li class="nav-header">Math.NET Numerics</li>
|
|
<li><a href="https://numerics.mathdotnet.com/Packages.html">NuGet & Binaries</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com/ReleaseNotes.html">Release Notes</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com/License.html">MIT/X11 License</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com/Compatibility.html">Platform Support</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com/api/">Class Reference</a></li>
|
|
<li><a href="https://github.com/mathnet/mathnet-numerics/issues">Issues & Bugs</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com/Users.html">Who is using Math.NET?</a></li>
|
|
|
|
<li class="nav-header">Contributing</li>
|
|
<li><a href="https://numerics.mathdotnet.com/Contributors.html">Contributors</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com/Contributing.html">Contributing</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com/Build.html">Build & Tools</a></li>
|
|
<li><a href="http://feedback.mathdotnet.com/forums/2060-math-net-numerics">Your Ideas</a></li>
|
|
|
|
<li class="nav-header">Getting Help</li>
|
|
<li><a href="https://discuss.mathdotnet.com/c/numerics">Discuss</a></li>
|
|
<li><a href="https://stackoverflow.com/questions/tagged/mathdotnet">Stack Overflow</a></li>
|
|
|
|
<li class="nav-header">Getting Started</li>
|
|
<li><a href="https://numerics.mathdotnet.com/">Getting started</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com/Constants.html">Constants</a></li>
|
|
<li>Floating-Point Numbers</li>
|
|
<li>Arbitrary Precision Numbers</li>
|
|
<li>Complex Numbers</li>
|
|
<li><a href="https://numerics.mathdotnet.com/Matrix.html">Matrices and Vectors</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com/Euclid.html">Euclid & Number Theory</a></li>
|
|
<li>Combinatorics</li>
|
|
|
|
<li class="nav-header">Evaluation</li>
|
|
<li><a href="https://numerics.mathdotnet.com/Functions.html">Special Functions</a></li>
|
|
<li>Differentiation</li>
|
|
<li><a href="https://numerics.mathdotnet.com/Integration.html">Integration</a></li>
|
|
|
|
<li class="nav-header">Statistics/Probability</li>
|
|
<li><a href="https://numerics.mathdotnet.com/DescriptiveStatistics.html">Descriptive Statistics</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com/Probability.html">Probability Distributions</a></li>
|
|
|
|
<li class="nav-header">Generation</li>
|
|
<li><a href="https://numerics.mathdotnet.com/Generate.html">Generating Data</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com/Random.html">Random Numbers</a></li>
|
|
|
|
<li class="nav-header">Transformation</li>
|
|
<li>Fourier Transform (FFT)</li>
|
|
<li>Filtering & DSP</li>
|
|
<li>Window Functions</li>
|
|
|
|
<li class="nav-header">Solving Equations</li>
|
|
<li><a href="https://numerics.mathdotnet.com/LinearEquations.html">Linear Equation Systems</a></li>
|
|
<li>Nonlinear Root Finding</li>
|
|
|
|
<li class="nav-header">Optimization</li>
|
|
<li>Linear Least Squares</li>
|
|
<li>Nonlinear Optimization</li>
|
|
<li><a href="https://numerics.mathdotnet.com/Distance.html">Distance Metrics</a></li>
|
|
|
|
<li class="nav-header">Curve Fitting</li>
|
|
<li><a href="https://numerics.mathdotnet.com/Regression.html">Regression</a></li>
|
|
<li>Interpolation</li>
|
|
<li>Fourier Approximation</li>
|
|
|
|
<li class="nav-header">Native Providers</li>
|
|
<li><a href="https://numerics.mathdotnet.com/MKL.html">Intel MKL</a></li>
|
|
|
|
<li class="nav-header">Working Together</li>
|
|
<li><a href="https://numerics.mathdotnet.com/CSV.html">Delimited Text Files (CSV)</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com/MatrixMarket.html">NIST MatrixMarket</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com/MatlabFiles.html">MATLAB</a></li>
|
|
<li><a href="https://numerics.mathdotnet.com/IFSharpNotebook.html">IF# Notebook</a></li>
|
|
<li>FsLab & Deedle</li>
|
|
<li>Microsoft Excel</li>
|
|
<li>numl.net machine learning</li>
|
|
<li>R-project</li>
|
|
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
|