-
Notifications
You must be signed in to change notification settings - Fork 103
/
NaiveBayesClassifier.cs
195 lines (171 loc) · 8.83 KB
/
NaiveBayesClassifier.cs
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using Tensorflow;
using Tensorflow.Keras.Utils;
using Tensorflow.NumPy;
using static Tensorflow.Binding;
namespace TensorFlowNET.Examples
{
/// <summary>
/// https://github.com/nicolov/naive_bayes_tensorflow
/// </summary>
public class NaiveBayesClassifier : SciSharpExample, IExample
{
public NDArray X, y;
public Normal dist { get; set; }
public ExampleConfig InitConfig()
=> Config = new ExampleConfig
{
Name = "Naive Bayes Classifier",
Enabled = true,
IsImportingGraph = false
};
public bool Run()
{
tf.enable_eager_execution();
PrepareData();
fit(X, y);
// Create a regular grid and classify each point
float x_min = np.amin(X, 0)[0] - 0.5f;
float y_min = np.amin(X, 0)[1] - 0.5f;
float x_max = np.amax(X, 0)[1] + 0.5f;
float y_max = np.amax(X, 0)[1] + 0.5f;
var (xx, yy) = np.meshgrid(np.linspace(x_min, x_max, 30), np.linspace(y_min, y_max, 30));
var array = np.Load<double[,]>(Path.Join("nb", "nb_example.npy"));
var samples = np.array(array).astype(np.float32);
var Z = predict(samples);
return true;
}
public void fit(NDArray X, NDArray y)
{
var (unique_y, _) = np.unique(y);
var dic = new Dictionary<int, List<List<float>>>();
// Init uy in dic
foreach (int uy in unique_y.ToArray<int>())
{
dic.Add(uy, new List<List<float>>());
}
// Separate training points by class
// Shape : nb_classes * nb_samples * nb_features
int maxCount = 0;
for (int i = 0; i < (int)y.size; i++)
{
var curClass = y[i];
var l = dic[curClass];
var pair = new List<float>();
pair.Add(X[i, 0]);
pair.Add(X[i, 1]);
l.Add(pair);
if (l.Count > maxCount)
{
maxCount = l.Count;
}
dic[curClass] = l;
}
float[,,] points = new float[dic.Count, maxCount, X.shape[1]];
foreach (KeyValuePair<int, List<List<float>>> kv in dic)
{
int j = kv.Key;
for (int i = 0; i < maxCount; i++)
{
for (int k = 0; k < X.shape[1]; k++)
{
points[j, i, k] = kv.Value[i][k];
}
}
}
var points_by_class = np.array(points);
// estimate mean and variance for each class / feature
// shape : nb_classes * nb_features
var cons = tf.constant(points_by_class);
var (mean, variance) = tf.nn.moments(cons, new int[] { 1 });
// Create a 3x2 univariate normal distribution with the
// Known mean and variance
dist = tf.distributions.Normal(mean, tf.sqrt(variance));
}
public Tensor predict(NDArray X)
{
if (dist == null)
{
throw new ArgumentNullException("cant not find the model (normal distribution)!");
}
int nb_classes = (int)dist.scale().shape[0];
int nb_features = (int)dist.scale().shape[1];
// Conditional probabilities log P(x|c) with shape
// (nb_samples, nb_classes)
var t1 = ops.convert_to_tensor(X, TF_DataType.TF_FLOAT);
var t2 = ops.convert_to_tensor(new int[] { 1, nb_classes });
Tensor tile = tf.tile(t1, t2);
var t3 = ops.convert_to_tensor(new int[] { -1, nb_classes, nb_features });
Tensor r = tf.reshape(tile, t3);
var cond_probs = tf.reduce_sum(dist.log_prob(r), 2);
// uniform priors
float[] tem = new float[nb_classes];
for (int i = 0; i < tem.Length; i++)
{
tem[i] = 1.0f / nb_classes;
}
var priors = np.log(np.array(tem));
// posterior log probability, log P(c) + log P(x|c)
var joint_likelihood = ops.convert_to_tensor(priors, TF_DataType.TF_FLOAT) + cond_probs;
// normalize to get (log)-probabilities
var norm_factor = tf.reduce_logsumexp(joint_likelihood, new int[] { 1 }, keepdims: true);
var log_prob = joint_likelihood - norm_factor;
// exp to get the actual probabilities
return tf.exp(log_prob);
}
public override void PrepareData()
{
#region Training data
X = np.array(new float[,] {
{5.1f, 3.5f}, {4.9f, 3.0f}, {4.7f, 3.2f}, {4.6f, 3.1f}, {5.0f, 3.6f}, {5.4f, 3.9f},
{4.6f, 3.4f}, {5.0f, 3.4f}, {4.4f, 2.9f}, {4.9f, 3.1f}, {5.4f, 3.7f}, {4.8f, 3.4f},
{4.8f, 3.0f}, {4.3f, 3.0f}, {5.8f, 4.0f}, {5.7f, 4.4f}, {5.4f, 3.9f}, {5.1f, 3.5f},
{5.7f, 3.8f}, {5.1f, 3.8f}, {5.4f, 3.4f}, {5.1f, 3.7f}, {5.1f, 3.3f}, {4.8f, 3.4f},
{5.0f, 3.0f}, {5.0f, 3.4f}, {5.2f, 3.5f}, {5.2f, 3.4f}, {4.7f, 3.2f}, {4.8f, 3.1f},
{5.4f, 3.4f}, {5.2f, 4.1f}, {5.5f, 4.2f}, {4.9f, 3.1f}, {5.0f, 3.2f}, {5.5f, 3.5f},
{4.9f, 3.6f}, {4.4f, 3.0f}, {5.1f, 3.4f}, {5.0f, 3.5f}, {4.5f, 2.3f}, {4.4f, 3.2f},
{5.0f, 3.5f}, {5.1f, 3.8f}, {4.8f, 3.0f}, {5.1f, 3.8f}, {4.6f, 3.2f}, {5.3f, 3.7f},
{5.0f, 3.3f}, {7.0f, 3.2f}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0f}, {7.4f, 2.8f}, {7.9f, 3.8f}, {6.4f, 2.8f},
{6.3f, 2.8f}, {6.1f, 2.6f}, {7.7f, 3.0f}, {6.3f, 3.4f}, {6.4f, 3.1f}, {6.0f, 3.0f},
{6.9f, 3.1f}, {6.7f, 3.1f}, {6.9f, 3.1f}, {5.8f, 2.7f}, {6.8f, 3.2f}, {6.7f, 3.3f},
{6.7f, 3.0f}, {6.3f, 2.5f}, {6.5f, 3.0f}, {6.2f, 3.4f}, {5.9f, 3.0f}, {5.8f, 3.0f}});
y = np.array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2);
string url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/data/nb_example.npy";
Web.Download(url, "nb", "nb_example.npy");
#endregion
}
}
}