(DataPoint data)
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public CategoricalResults classify(DataPoint data) |
| 237 | { |
| 238 | CategoricalResults cr = new CategoricalResults(predicting.getNumOfCategories()); |
| 239 | |
| 240 | int n = data.numNumericalValues(); |
| 241 | Matrix sigma = Matrix.eye(n); |
| 242 | Matrix B = new DenseMatrix(n, n); |
| 243 | Matrix W = new DenseMatrix(n, n); |
| 244 | Vec query = data.getNumericalValues(); |
| 245 | |
| 246 | Vec scratch0 = new DenseVector(n); |
| 247 | |
| 248 | |
| 249 | |
| 250 | //TODO threadlocal DoubleList / DenseVec might be better in practice for memory use |
| 251 | double[] weights = new double[kn]; |
| 252 | double[] priors = new double[predicting.getNumOfCategories()]; |
| 253 | int[] classCount = new int[priors.length]; |
| 254 | double sumOfWeights; |
| 255 | Vec mean = new DenseVector(sigma.rows()); |
| 256 | Vec[] classMeans = new Vec[predicting.getNumOfCategories()]; |
| 257 | for(int i = 0; i < classMeans.length; i++) |
| 258 | classMeans[i] = new DenseVector(mean.length()); |
| 259 | |
| 260 | for(int iter = 0; iter < maxIterations; iter++) |
| 261 | { |
| 262 | //Zero out prev iter |
| 263 | mean.zeroOut(); |
| 264 | Arrays.fill(priors, 0); |
| 265 | Arrays.fill(classCount, 0); |
| 266 | for(int i = 0; i < classMeans.length; i++) |
| 267 | classMeans[i].zeroOut(); |
| 268 | sumOfWeights = 0; |
| 269 | B.zeroOut(); |
| 270 | W.zeroOut(); |
| 271 | |
| 272 | |
| 273 | List<? extends VecPaired<VecPaired<Vec, Integer>, Double>> vecs = |
| 274 | (iter == 0) ? vc.search(query, kn) : brute(query, sigma, kn); |
| 275 | //Compute vector mean and weight sums, class weight sums, and the class means |
| 276 | double h = vecs.get(vecs.size()-1).getPair(); |
| 277 | for(int i = 0; i < vecs.size(); i++) |
| 278 | { |
| 279 | VecPaired<? extends VecPaired<Vec, Integer>, Double> vec = vecs.get(i); |
| 280 | //vecs contains the distances, we need the distance squared |
| 281 | weights[i] = pow(pow(1-pow(vec.getPair(), 2) /h, 3), 3); |
| 282 | sumOfWeights += weights[i]; |
| 283 | mean.mutableAdd(vec); |
| 284 | |
| 285 | int j = vec.getVector().getPair(); |
| 286 | priors[j] += weights[i]; |
| 287 | classMeans[j].mutableAdd(vec); |
| 288 | classCount[j]++; |
| 289 | } |
| 290 | |
| 291 | //Final divide for means and priors |
| 292 | mean.mutableDivide(kn); |
nothing calls this directly
no test coverage detected