Skip to content

Commit a2fdf69

Browse files
committed
rich the zip API by implemented IEnumerable<(T1, T2)> zip<T1, T2>(NDArray t1, NDArray t2)
1 parent 7f156f1 commit a2fdf69

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

‎src/TensorFlowNET.Core/Python.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ public static float time()
9898
yield return (t1[i], t2[i]);
9999
}
100100

101+
public static IEnumerable<(T1, T2)> zip<T1, T2>(NDArray t1, NDArray t2)
102+
{
103+
for (int i = 0; i < t1.size; i++)
104+
yield return (t1.Data<T1>(i), t2.Data<T2>(i));
105+
}
106+
101107
public static IEnumerable<(int, T)> enumerate<T>(IList<T> values)
102108
{
103109
for (int i = 0; i < values.Count; i++)

‎test/TensorFlowNET.Examples/NaiveBayesClassifier.cs‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@ public class NaiveBayesClassifier : Python, IExample
1414
{
1515
public void Run()
1616
{
17-
// t/f.nn.moments()
17+
np.array<float>(1.0f, 1.0f);
18+
// var X = np.array<float>(np.array<float>(1.0f, 1.0f), np.array<float>(2.0f, 2.0f), np.array<float>(1.0f, -1.0f), np.array<float>(2.0f, -2.0f), np.array<float>(-1.0f, -1.0f), np.array<float>(-1.0f, 1.0f),);
19+
// var X = np.array<float[]>(new float[][] { new float[] { 1.0f, 1.0f}, new float[] { 2.0f, 2.0f }, new float[] { -1.0f, -1.0f }, new float[] { -2.0f, -2.0f }, new float[] { 1.0f, -1.0f }, new float[] { 2.0f, -2.0f }, });
20+
var X = np.array<float>(new float[][] { new float[] { 1.0f, 1.0f }, new float[] { 2.0f, 2.0f }, new float[] { -1.0f, -1.0f }, new float[] { -2.0f, -2.0f }, new float[] { 1.0f, -1.0f }, new float[] { 2.0f, -2.0f }, });
21+
var y = np.array<int>(0,0,1,1,2,2);
22+
23+
fit(X, y);
24+
// Create a regular grid and classify each point
25+
1826
}
1927

2028
public void fit(NDArray X, NDArray y)
2129
{
2230
NDArray unique_y = y.unique<long>();
2331

24-
Dictionary<int, List<NDArray>> dic = new Dictionary<int, List<NDArray>>();
32+
Dictionary<long, List<NDArray>> dic = new Dictionary<long, List<NDArray>>();
2533
// Init uy in dic
2634
foreach (int uy in unique_y.Data<int>())
2735
{
@@ -30,19 +38,19 @@ public void fit(NDArray X, NDArray y)
3038
// Separate training points by class
3139
// Shape : nb_classes * nb_samples * nb_features
3240
int maxCount = 0;
33-
foreach (var (x, t) in zip(X.Data<float>(), y.Data<int>()))
41+
for (int i = 0; i < y.size; i++)
3442
{
35-
int curClass = (y[t, 0] as NDArray).Data<int>().First();
43+
long curClass = (long)y[i];
3644
List<NDArray> l = dic[curClass];
37-
l.Add(x);
45+
l.Add(X[i] as NDArray);
3846
if (l.Count > maxCount)
3947
{
4048
maxCount = l.Count;
4149
}
42-
dic.Add(curClass, l);
50+
dic[curClass] = l;
4351
}
44-
NDArray points_by_class = np.zeros(dic.Count,maxCount,X.shape[1]);
45-
foreach (KeyValuePair<int, List<NDArray>> kv in dic)
52+
NDArray points_by_class = np.zeros(new int[] { dic.Count, maxCount, X.shape[1] });
53+
foreach (KeyValuePair<long, List<NDArray>> kv in dic)
4654
{
4755
var cls = kv.Value.ToArray();
4856
for (int i = 0; i < dic.Count; i++)

0 commit comments

Comments
 (0)