@@ -19,7 +19,6 @@ public void Run()
1919 // 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 }, });
2020 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 } , } ) ;
2121 var y = np . array < int > ( 0 , 0 , 1 , 1 , 2 , 2 ) ;
22-
2322 fit ( X , y ) ;
2423 // Create a regular grid and classify each point
2524
@@ -28,41 +27,48 @@ public void Run()
2827 public void fit ( NDArray X , NDArray y )
2928 {
3029 NDArray unique_y = y . unique < long > ( ) ;
31-
32- Dictionary < long , List < NDArray > > dic = new Dictionary < long , List < NDArray > > ( ) ;
30+
31+ Dictionary < long , List < List < float > > > dic = new Dictionary < long , List < List < float > > > ( ) ;
3332 // Init uy in dic
3433 foreach ( int uy in unique_y . Data < int > ( ) )
3534 {
36- dic . Add ( uy , new List < NDArray > ( ) ) ;
35+ dic . Add ( uy , new List < List < float > > ( ) ) ;
3736 }
3837 // Separate training points by class
3938 // Shape : nb_classes * nb_samples * nb_features
4039 int maxCount = 0 ;
4140 for ( int i = 0 ; i < y . size ; i ++ )
4241 {
4342 long curClass = ( long ) y [ i ] ;
44- List < NDArray > l = dic [ curClass ] ;
45- l . Add ( X [ i ] as NDArray ) ;
43+ List < List < float > > l = dic [ curClass ] ;
44+ List < float > pair = new List < float > ( ) ;
45+ pair . Add ( ( float ) X [ i , 0 ] ) ;
46+ pair . Add ( ( float ) X [ i , 1 ] ) ;
47+ l . Add ( pair ) ;
4648 if ( l . Count > maxCount )
4749 {
4850 maxCount = l . Count ;
4951 }
5052 dic [ curClass ] = l ;
5153 }
52- NDArray points_by_class = np . zeros ( new int [ ] { dic . Count , maxCount , X . shape [ 1 ] } ) ;
53- foreach ( KeyValuePair < long , List < NDArray > > kv in dic )
54+ float [ , , ] points = new float [ dic . Count , maxCount , X . shape [ 1 ] ] ;
55+ foreach ( KeyValuePair < long , List < List < float > > > kv in dic )
5456 {
55- var cls = kv . Value . ToArray ( ) ;
56- for ( int i = 0 ; i < dic . Count ; i ++ )
57+ int j = ( int ) kv . Key ;
58+ for ( int i = 0 ; i < maxCount ; i ++ )
5759 {
58- points_by_class [ i ] = dic [ i ] ;
60+ for ( int k = 0 ; k < X . shape [ 1 ] ; k ++ )
61+ {
62+ points [ j , i , k ] = kv . Value [ i ] [ k ] ;
63+ }
5964 }
60- }
6165
66+ }
67+ NDArray points_by_class = np . array < float > ( points ) ;
6268 // estimate mean and variance for each class / feature
6369 // shape : nb_classes * nb_features
6470 var cons = tf . constant ( points_by_class ) ;
65- Tuple < Tensor , Tensor > tup = tf . nn . moments ( cons , new int [ ] { 1 } ) ;
71+ var tup = tf . nn . moments ( cons , new int [ ] { 1 } ) ;
6672 var mean = tup . Item1 ;
6773 var variance = tup . Item2 ;
6874 // Create a 3x2 univariate normal distribution with the
0 commit comments