-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
168 lines (157 loc) · 6.67 KB
/
Copy pathProgram.cs
File metadata and controls
168 lines (157 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace LinearRegression;
public static class Program
{
// Dataset.
private static List<double> toPredict = new List<double>() { 5, 8.9, 79.88, 98, 56 };
private static List<double> independant = new List<double>() { 80, 20, 89, 78, 150 };
private static List<double> dependant = new List<double>() { 176, 200, 350, 670, 455 };
private static List<double> targets = new List<double>() { 450, 558, 779, 780.97, 100 };
[DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ShowWindow([In] IntPtr hWnd, [In] int nCmdShow);
private static void Main()
{
// Temporary fix until dinamyic console window resizing is handled. Keeps console window minimized.
IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
ShowWindow(handle, 6);
CoreSetUp();
}
private static void CoreSetUp()
{
int epoch = 100000;
double learningRate = 0.0001;
double cost = 0;
(double slope, double intercept) = Parametars(independant, dependant);
Console.SetCursorPosition(Console.WindowWidth / 2, 4);
Console.WriteLine("Linear regression model");
for (int i = 1; i <= epoch; i++)
{
List<double> predicted = Predict(slope, intercept, independant);
cost = CostFunction(predicted, targets);
double weightGradient = wGradient(predicted, targets, independant);
double biasGradient = bGradient(predicted, targets);
slope = slope - learningRate * weightGradient;
intercept = intercept - learningRate * biasGradient;
Console.SetCursorPosition(0, 5);
Console.WriteLine($"Weight gradient: {weightGradient}");
Console.WriteLine($" Bias gradient: {biasGradient}");
Console.WriteLine($"Loss:{cost}");
Console.WriteLine($"Weight:{slope}");
Console.WriteLine($"Bias:{intercept}");
}
List <double> newPredictions = Predict(slope, intercept, toPredict);
foreach (double prediction in newPredictions)
{ int position = Convert.ToInt32(prediction);
position = Convert.ToInt32(position - Console.WindowWidth * Math.Floor(position / (double) Console.WindowWidth));
Console.SetCursorPosition(Convert.ToInt32(position), 0);
Console.WriteLine("p");
}
foreach (double target in targets)
{
int position = Convert.ToInt32(target);
position = Convert.ToInt32(position - Console.WindowHeight * Math.Floor(position / (double) Console.WindowHeight));
Console.SetCursorPosition(0, position);
Console.WriteLine("t");
}
drawLine(slope, intercept);
}
#region Parameters
private static (double slope, double intercept) Parametars(List<double> independent, List<double> dependent)
{
/*
independent = x.
dependent = y.
weight/s = slope.
bias = y-intercept.
*/
double numerator = 0;
double denominator = 0;
for (int i = 0; i < independent.Count; i++)
{
numerator += (independent[i] - independent.Average()) * (dependent[i] - dependent.Average());
denominator += Math.Pow(independent[i] - independent.Average(), 2);
}
double slope = numerator / denominator;
double intercept = dependent.Average() - slope * independant.Average();
return (slope, intercept);
}
#endregion Parameters
private static List<double> Predict(double slope, double intercept, List<double> toPredict)
{
List<double> predicted = new List<double>();
for (int i = 0; i < toPredict.Count; i++)
{
predicted.Add(slope * toPredict[i] + intercept);
}
return predicted;
}
private static double CostFunction(List<double> predicted, List<double> targets)
{
double loss = 0;
for (int i = 0; i < predicted.Count; i++)
{
loss += Math.Pow(targets[i] - predicted[i], 2);
}
return loss / (2 * predicted.Count);
}
#region Gradients
static double wGradient(List<double> predicted, List<double> targets, List<double> independent)
{
double gradient = 0;
for (int i = 0; i < independant.Count; i++)
{
gradient += (predicted[i] - targets[i]) * independent[i];
}
return gradient / independant.Count;
}
static double bGradient(List<double> predicted, List<double> targets)
{
double gradient = 0;
for (int i = 0; i < predicted.Count; i++)
{
gradient += (predicted[i] - targets[i]);
}
return gradient / predicted.Count;
}
#endregion Gradients
private static void drawLine(double slope, double intercept)
{
List<double> calculated = Predict(slope, intercept, new List<double> {independant.Min(), independant.Min()});
// Keep coordinates in bounds.
int x0 = Convert.ToInt32(independant.Min());
if (x0 > Console.WindowWidth)
{
x0 = Convert.ToInt32(x0 - Console.WindowHeight * Math.Floor(x0 / (double)Console.WindowHeight));
}
int x1 = Convert.ToInt32(independant.Max());
if (x1 > Console.WindowWidth)
{
x1 = Convert.ToInt32(x1 - Console.WindowHeight * Math.Floor(x1 / (double)Console.WindowHeight));
}
int y0 = Convert.ToInt32(calculated[0]);
if (y0 > Console.WindowHeight)
{
y0 = Convert.ToInt32(y0 - Console.WindowHeight * Math.Floor(y0 / (double)Console.WindowHeight));
}
int y1 = Convert.ToInt32(calculated[1]);
if (y1 > Console.WindowHeight)
{
y1 = Convert.ToInt32(y1 - Console.WindowHeight * Math.Floor(y1 / (double)Console.WindowHeight));
}
int dx = Math.Abs(Convert.ToInt32(x1 - x0));
int dy = Math.Abs(Convert.ToInt32(y1 - y0));
int direction = Math.Max(dx, dy);
int directionX = dx / direction;
int directionY = dy / direction;
for (int i = 1; i < direction + 1; i++)
{
Console.SetCursorPosition(Convert.ToInt32(x0 + i * directionX), Convert.ToInt32(y0 + i * directionY));
Console.WriteLine(".");
}
}
}