You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

NaiveBayesClassifier.cs 3.5 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Tensorflow;
  5. using NumSharp.Core;
  6. using System.Linq;
  7. namespace TensorFlowNET.Examples
  8. {
  9. /// <summary>
  10. /// https://github.com/nicolov/naive_bayes_tensorflow
  11. /// </summary>
  12. public class NaiveBayesClassifier : Python, IExample
  13. {
  14. public void Run()
  15. {
  16. np.array<float>(1.0f, 1.0f);
  17. // 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),);
  18. // 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 }, });
  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 y = np.array<int>(0,0,1,1,2,2);
  21. fit(X, y);
  22. // Create a regular grid and classify each point
  23. }
  24. public void fit(NDArray X, NDArray y)
  25. {
  26. NDArray unique_y = y.unique<long>();
  27. Dictionary<long, List<List<float>>> dic = new Dictionary<long, List<List<float>>>();
  28. // Init uy in dic
  29. foreach (int uy in unique_y.Data<int>())
  30. {
  31. dic.Add(uy, new List<List<float>>());
  32. }
  33. // Separate training points by class
  34. // Shape : nb_classes * nb_samples * nb_features
  35. int maxCount = 0;
  36. for (int i = 0; i < y.size; i++)
  37. {
  38. long curClass = (long)y[i];
  39. List<List<float>> l = dic[curClass];
  40. List<float> pair = new List<float>();
  41. pair.Add((float)X[i,0]);
  42. pair.Add((float)X[i, 1]);
  43. l.Add(pair);
  44. if (l.Count > maxCount)
  45. {
  46. maxCount = l.Count;
  47. }
  48. dic[curClass] = l;
  49. }
  50. float[,,] points = new float[dic.Count, maxCount, X.shape[1]];
  51. foreach (KeyValuePair<long, List<List<float>>> kv in dic)
  52. {
  53. int j = (int) kv.Key;
  54. for (int i = 0; i < maxCount; i++)
  55. {
  56. for (int k = 0; k < X.shape[1]; k++)
  57. {
  58. points[j, i, k] = kv.Value[i][k];
  59. }
  60. }
  61. }
  62. NDArray points_by_class = np.array<float>(points);
  63. // estimate mean and variance for each class / feature
  64. // shape : nb_classes * nb_features
  65. var cons = tf.constant(points_by_class);
  66. var tup = tf.nn.moments(cons, new int[]{1});
  67. var mean = tup.Item1;
  68. var variance = tup.Item2;
  69. // Create a 3x2 univariate normal distribution with the
  70. // Known mean and variance
  71. var dist = tf.distributions.Normal(mean, tf.sqrt(variance));
  72. }
  73. public void predict (NDArray X)
  74. {
  75. // assert self.dist is not None
  76. // nb_classes, nb_features = map(int, self.dist.scale.shape)
  77. throw new NotFiniteNumberException();
  78. }
  79. }
  80. }

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。