001/* 002 * Copyright (c) 2015-2022, Oracle and/or its affiliates. All rights reserved. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.tribuo.common.nearest; 018 019import com.oracle.labs.mlrg.olcut.config.ArgumentException; 020import com.oracle.labs.mlrg.olcut.config.Option; 021import org.tribuo.classification.ClassificationOptions; 022import org.tribuo.classification.Label; 023import org.tribuo.classification.ensemble.FullyWeightedVotingCombiner; 024import org.tribuo.classification.ensemble.VotingCombiner; 025import org.tribuo.common.nearest.KNNModel.Backend; 026import org.tribuo.ensemble.EnsembleCombiner; 027import org.tribuo.math.distance.DistanceType; 028import org.tribuo.math.neighbour.NeighboursQueryFactoryType; 029 030/** 031 * CLI Options for training a k-nearest neighbour predictor. 032 */ 033public class KNNClassifierOptions implements ClassificationOptions<KNNTrainer<Label>> { 034 035 /** 036 * The type of combination function. 037 */ 038 public enum EnsembleCombinerType { 039 /** 040 * Use a {@link VotingCombiner}. 041 */ 042 VOTING, 043 /** 044 * Use a {@link FullyWeightedVotingCombiner}. 045 */ 046 FULLY_WEIGHTED_VOTING 047 } 048 049 /** 050 * K nearest neighbours to use. Defaults to 1. 051 */ 052 @Option(longName = "knn-k", usage = "K nearest neighbours to use.") 053 public int knnK = 1; 054 /** 055 * Number of threads to use. Defaults to 1. 056 */ 057 @Option(longName = "knn-num-threads", usage = "Number of threads to use.") 058 public int knnNumThreads = 1; 059 /** 060 * Distance metric to use. Defaults to L2 (EUCLIDEAN). 061 */ 062 @Option(longName = "knn-distance-type", usage = "Distance metric to use.") 063 public DistanceType distType = DistanceType.L2; 064 065 /** 066 * Parallel backend to use. 067 */ 068 @Option(longName = "knn-backend", usage = "Parallel backend to use.") 069 public Backend knnBackend = Backend.STREAMS; 070 /** 071 * Parallel backend to use. 072 */ 073 @Option(longName = "knn-voting", usage = "Parallel backend to use.") 074 public EnsembleCombinerType knnEnsembleCombiner = EnsembleCombinerType.VOTING; 075 /** 076 * The nearest neighbour implementation factory to use. Defaults to {@link NeighboursQueryFactoryType#BRUTE_FORCE}. 077 */ 078 @Option(longName = "knn-neighbour-query-factory-type", usage = "The nearest neighbour implementation factory to use.") 079 public NeighboursQueryFactoryType nqFactoryType = NeighboursQueryFactoryType.BRUTE_FORCE; 080 081 @Override 082 public String getOptionsDescription() { 083 return "Options for parameterising a K-NN classification trainer."; 084 } 085 086 private EnsembleCombiner<Label> getEnsembleCombiner() { 087 switch (knnEnsembleCombiner) { 088 case VOTING: 089 return new VotingCombiner(); 090 case FULLY_WEIGHTED_VOTING: 091 return new FullyWeightedVotingCombiner(); 092 default: 093 throw new ArgumentException("ensemble combiner", "Unknown ensemble combiner " + knnEnsembleCombiner); 094 } 095 } 096 097 @Override 098 public KNNTrainer<Label> getTrainer() { 099 return new KNNTrainer<>(knnK, distType.getDistance(), knnNumThreads, getEnsembleCombiner(), knnBackend, nqFactoryType); 100 } 101}