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.3 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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<NDArray>> dic = new Dictionary<long, List<NDArray>>();
  28. // Init uy in dic
  29. foreach (int uy in unique_y.Data<int>())
  30. {
  31. dic.Add(uy, new List<NDArray>());
  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<NDArray> l = dic[curClass];
  40. l.Add(X[i] as NDArray);
  41. if (l.Count > maxCount)
  42. {
  43. maxCount = l.Count;
  44. }
  45. dic[curClass] = l;
  46. }
  47. NDArray points_by_class = np.zeros(new int[] { dic.Count, maxCount, X.shape[1] });
  48. foreach (KeyValuePair<long, List<NDArray>> kv in dic)
  49. {
  50. var cls = kv.Value.ToArray();
  51. for (int i = 0; i < dic.Count; i++)
  52. {
  53. points_by_class[i] = dic[i];
  54. }
  55. }
  56. // estimate mean and variance for each class / feature
  57. // shape : nb_classes * nb_features
  58. var cons = tf.constant(points_by_class);
  59. Tuple<Tensor, Tensor> tup = tf.nn.moments(cons, new int[]{1});
  60. var mean = tup.Item1;
  61. var variance = tup.Item2;
  62. // Create a 3x2 univariate normal distribution with the
  63. // Known mean and variance
  64. // var dist = tf.distributions.Normal(loc=mean, scale=tf.sqrt(variance));
  65. }
  66. public void predict (NDArray X)
  67. {
  68. // assert self.dist is not None
  69. // nb_classes, nb_features = map(int, self.dist.scale.shape)
  70. throw new NotFiniteNumberException();
  71. }
  72. }
  73. }

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