forked from SciSharp/Bigtree.Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
65 lines (52 loc) · 1.75 KB
/
Copy pathUtils.cs
File metadata and controls
65 lines (52 loc) · 1.75 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
using NumSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Bigtree.Algorithm
{
public class Utils
{
public static (NDArray, NDArray) ReadCsv(string path)
{
List<int> labels = new List<int>();
var x1 = new List<double>();
var y1 = new List<int>();
int length1d = 0;
int length2d = 0;
using (StreamReader reader = new StreamReader(path))
{
string line = String.Empty;
while (!String.IsNullOrEmpty(line = reader.ReadLine()))
{
var tokens = line.Split(',');
x1.AddRange(tokens.Select(x => double.Parse(x)).Take(tokens.Length - 1));
var _y = int.Parse(tokens[tokens.Length - 1]);
if (!labels.Contains(_y))
{
labels.Add(_y);
}
y1.Add(labels.FindIndex(l => l == _y));
length1d++;
length2d = tokens.Length - 1;
}
}
var X = np.array(x1.ToArray()).reshape(length1d, length2d);
var y = np.array(y1.ToArray());
return (X, y);
}
public static List<List<int>> CrossValFolds(int N, int n_folds)
{
var folds = new List<List<int>>();
/*var rands = np.random.permutation(N);
var N_fold = N / n_folds;
for(int i = 0; i < n_folds; i++)
{
var list = rands.Data<int>().Skip(N_fold * i).Take(N_fold).ToList();
folds.Add(list);
}*/
return folds;
}
}
}