-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
46 lines (45 loc) · 1.4 KB
/
Program.cs
File metadata and controls
46 lines (45 loc) · 1.4 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
using DecisionTree;
using Microsoft.Data.Analysis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unit4.CollectionsLib;
namespace DTModel
{
internal class Program
{
static void Main(string[] args)
{
DataFrame df = DataFrame.LoadCsv("Car.csv");
df = df.DropNulls();
var (trainDf, testDf) = TrainTestSplit(df, 0.2, seed: 42);
RandomForest fr = new RandomForest(trainDf, "owner", 100);
DataFrame pred = fr.Pred(testDf);
Console.WriteLine(pred.ToString());
DataFrame.SaveCsv(pred, "pred.csv", ',');
}
public static (DataFrame train, DataFrame test) TrainTestSplit(DataFrame df, double testSize = 0.2, int? seed = null)
{
Random rand = seed.HasValue ? new Random(seed.Value) : new Random();
int rowCount = (int)df.Rows.Count;
int testCount = (int)(rowCount * testSize);
int trainCount = rowCount - testCount;
List<int> indices = Enumerable.Range(0, rowCount).ToList();
for (int i = indices.Count - 1; i > 0; i--)
{
int j = rand.Next(i + 1);
int temp = indices[i];
indices[i] = indices[j];
indices[j] = temp;
}
var trainIndices = indices.Take(trainCount).Select(i => (long)i);
var testIndices = indices.Skip(trainCount).Select(i => (long)i);
DataFrame trainDf = df[trainIndices];
DataFrame testDf = df[testIndices];
return (trainDf, testDf);
}
}
}