title: C# Perceptron Algorithm (Reading in a file)
body: <p>I'm currently creating a simple single layer perceptron algorithm. It should take in a .txt file and the algorithm runs over it. At the moment, I have the algorithm and just hard coded sample data values to test if it works (which it does), but I need it to feed off existing data values from a file. I have tried to make it work, but due to my inexperience in coding, I haven't succeeded. It would be awesome if someone could help me get this code working with external data as I've been pulling my hair out. The data is formatted like below (obviously without the bullet numbers).</p>
  
  <ol>
  <li>0.651769</li>
  <li>0.651604</li>
  <li>0.651609</li>
  <li>0.651679</li>
  <li>0.651667</li>
  <li>0.651699</li>
  </ol>
  
  <p>Current Code:</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Collections.Generic;
  namespace Network
  {
  public class Network
  
  {
      static void Main()
      {
          // Load sample input patterns.
          double[,] inputs = new double[,] {
  
              {0.99, 0.99}, {0.99, 0.99}, {0.99, 0.99}, {0.99, 095}};
  
          // Load sample output patterns.
          int[] outputs = new int[] {0, 1, 0, 0 };
          int patternCount = inputs.GetUpperBound(0) + 1;
  
          // Randomise weights.
          Random rdm = new Random();
  
          // Setting randomly generated weights between -0.5 and +0.5
          double[] weights = {rdm.NextDouble()-0.5, rdm.NextDouble()-0.5, rdm.NextDouble()};
  
          // Set learning rate.
          double learningRate = 1;
  
          // Start iteration at 0
          int iteration = 1;
          double ErrorRate;
  
          do
          {   
              // Global error set to 0
              ErrorRate = 0;
              for (int j = 0; j &lt; patternCount; j++)
              {
                  // Calculate output.
                  int output = Output(weights, inputs[j, 0], inputs[j, 1]);
                  // Calculate error.
                  double localError = outputs[j] - output;
  
                  //if the localError is not equal to zero
                  if (localError != 0)
                  {
                      // Update weights.
                      for (int i = 0; i &lt; 2; i++)
                      {
                          weights[i] += learningRate * localError * inputs[j, i] / 2;
                      }
                  }
                  // Convert error to absolute value.
                  ErrorRate += (localError);
              }
  
              Console.WriteLine("Iteration {0}\tError {1}", iteration, ErrorRate);
              iteration++;
  
           // If the Error is equal to zero then calculate
          } while (ErrorRate != 0);
  
          // Convergence
          Console.WriteLine();
          Console.WriteLine("[Input1] [Input2] [Output]");
          // Input1 values
          for (double input1 = 0; input1 &lt;= 1; input1 += 1)
          {
          // Input2 values
              for (double input2 = 0; input2 &lt;= 1; input2 += 1)
              {
                  // Calculate output with the inputs and the randomly generated weights
                  int output = Output(weights, input1, input2);
                  Console.ForegroundColor = ConsoleColor.Cyan;
                  Console.WriteLine("    {0}         {1}        {2}", input1, input2, (output == 1) ? "1" : "0");
              }
          }
          Console.Read();
      }
  
      private static int Output(double[] weights, double input1, double input2)
      {
          //  Output = input1 * weight1 + input2 * weight2 + bias * weight3
          double sum = input1 * weights[0] + input2 * weights[1] + 1 * weights[2];
  
          // If the first condition is true, then it becomes the result. If not, the second condition becomes the result
          return (sum &gt;= 0) ? 1 : 0;
      }
  }
  }
  </code></pre>