Nowa wersja
|
Poprzednia wersja
|
pl:miw:miw08_rbs_ml:program [2008/06/10 14:22] miw utworzono |
pl:miw:miw08_rbs_ml:program [2008/06/16 13:39] miw |
<code = "csharp"> | |
using System; | |
using System.Collections.Generic; | |
using System.Windows.Forms; | |
using System.IO; | |
using System.Text; | |
using System.Xml.Serialization; | |
| |
namespace Morcinek.Machine_Learning | |
{ | |
static class Program | |
{ | |
/// <summary> | |
/// The main entry point for the application. | |
/// </summary> | |
[STAThread] | |
static void Main() | |
{ | |
#region Old | |
| |
object[][] learning; | |
| |
learning = Common.BinaryDeSerialize("Thermostat_2016.bin"); | |
/*learning = new object[2016][]; | |
int index = -1; | |
int day; | |
int oper; | |
int month; | |
for (int i = 0; i < 7; i++) // Dni | |
{ | |
day = i; | |
for (int j = 0; j < 24; j++) // Godziny | |
{ | |
oper = j; | |
for (int k = 0; k < 12; k++) // Miesiące | |
{ | |
month = k; | |
learning[++index] = new object[] { (Days)day, oper, (Month)month, 0 }; | |
learning[index][3] = Common.CountTemperature(learning[index]); | |
} | |
} | |
}*/ | |
int testCollectionLenght = 252; | |
int randValue; | |
Random rand = new Random(); | |
List<int> learningIndexes = new List<int>(); | |
List<int> testingIndexes = new List<int>(100); | |
for (int i = 0; i < testCollectionLenght; i++) | |
{ | |
randValue = rand.Next(2015); | |
if (!learningIndexes.Contains(randValue)) | |
learningIndexes.Add(randValue); | |
else | |
--i; | |
} | |
learningIndexes.Sort(); | |
for (int i = 0; i < 2016; i++) | |
{ | |
if (!learningIndexes.Contains(i)) | |
testingIndexes.Add(i); | |
} | |
| |
List<object[]> testingList = new List<object[]>(); | |
foreach (int index in testingIndexes) | |
{ | |
testingList.Add(learning[index]); | |
} | |
object[][] testing = testingList.ToArray(); | |
| |
| |
List<object[]> learningList = new List<object[]>(); | |
foreach (int index in learningIndexes) | |
{ | |
learningList.Add(learning[index]); | |
} | |
object[][] learn = learningList.ToArray(); | |
//*/ | |
//Common.BinarySerialize("Thermostat_2016.bin", learning); | |
string[] attributeNames = new string[] { "DayOfWeek", "Hour", "Month" }; | |
//string[] attributeNames = new string[] { "Aura", "Temperatura", "Wilgotnosc", "Wiatr" }; | |
#endregion | |
| |
//DecisionTree decisionTree = new DecisionTree(); | |
//decisionTree.Initialize(learn, attributeNames); | |
int learningLength = 150; | |
| |
Thermostat_252_2016(learningLength); | |
} | |
| |
public static void Thermostat_252_2016(int learningLength) | |
{ | |
int randValue; | |
object[][] everything; | |
object[][] testing; | |
object[][] learning; | |
DecisionTree decisionTree; | |
int goodClassified = 0; | |
| |
everything = Common.BinaryDeSerialize("Thermostat_2016.bin"); | |
string[] attributeNames = new string[] { "Day", "Hour", "Month" }; | |
| |
Random rand = new Random(); | |
List<int> learningIndexes = new List<int>(); | |
List<int> testingIndexes = new List<int>(); | |
for (int i = 0; i < learningLength; i++) | |
{ | |
randValue = rand.Next(2015); | |
if (!learningIndexes.Contains(randValue)) | |
learningIndexes.Add(randValue); | |
else | |
--i; | |
} | |
learningIndexes.Sort(); | |
| |
for (int i = 0; i < 2016; i++) | |
{ | |
if (!learningIndexes.Contains(i)) | |
testingIndexes.Add(i); | |
} | |
| |
testing = IndexesToArray(everything, testingIndexes); | |
| |
learning = IndexesToArray(everything, learningIndexes); | |
| |
decisionTree = new DecisionTree(); | |
decisionTree.Initialize(learning, attributeNames); | |
| |
foreach (object[] row in testing) | |
{ | |
if (decisionTree.FindCategory(row) == (int)row[3]) | |
goodClassified++; | |
} | |
StringBuilder sb = new StringBuilder(); | |
sb.Append("Everything is "); | |
sb.Append(goodClassified.ToString()); | |
sb.Append("/"); | |
sb.Append(testing.Length.ToString()); | |
Console.Write(sb.ToString()); | |
string temp = sb.ToString(); | |
UpdateCorrect(goodClassified); // Funkcja zapisuje kolejne ilości dobrze sklasyfikowanych przykładów | |
} | |
| |
private static object[][] IndexesToArray(object[][] everything, List<int> indexesList) | |
{ | |
List<object[]> testingList = new List<object[]>(); | |
foreach (int index in indexesList) | |
{ | |
testingList.Add(everything[index]); | |
} | |
return testingList.ToArray(); | |
} | |
| |
private static void UpdateCorrect(int amount) | |
{ | |
List<int> list; | |
XmlSerializer serializer = new XmlSerializer(typeof(List<int>)); | |
if (File.Exists("Tests.xml")) | |
{ | |
TextReader reader = new StreamReader("Tests.xml"); | |
list = (List<int>)serializer.Deserialize(reader); | |
reader.Close(); | |
} | |
else | |
list = new List<int>(); | |
int sum = 0; | |
foreach (int var in list) | |
{ | |
sum += var; | |
} | |
double percent = (double) sum / (double)(2016 - 150) / (double)list.Count; | |
list.Sort(); | |
| |
//list.Add(amount); | |
| |
TextWriter writer = new StreamWriter("Tests.xml"); | |
serializer.Serialize(writer, list); | |
writer.Close(); | |
} | |
} | |
} | |
</code> | |