ClassifierBase<T>
Base Classes · AiDotNet.Classification
Provides a base implementation for classification algorithms that predict categorical outcomes.
Implements: IClassifier<T>, IModelShape, IParameterChunkSource<T>, IParameterManifestProvider
Known derived types: AdaptiveRandomForestClassifier<T>, BalancedBaggingClassifier<T>, BalancedRandomForestClassifier<T>, EasyEnsembleClassifier<T>, HistGradientBoostingClassifier<T>, HoeffdingTreeClassifier<T>, MiniRocketClassifier<T>, OnlineNaiveBayesClassifier<T>, OrdinalClassifierBase<T>, OrdinalRegression<T>, ProbabilisticClassifierBase<T>, RocketClassifier<T>, SemiSupervisedClassifierBase<T>, TimeSeriesClassifierBase<T>, TimeSeriesForestClassifier<T>
For Beginners
Classification is about predicting which category something belongs to. This base class provides the foundation for different classification techniques, handling common operations like making predictions and saving/loading models. Think of it as a template that specific classification algorithms can customize while reusing the shared functionality.
How It Works
This abstract class implements common functionality for classification models, including prediction, serialization/deserialization, and parameter management. Specific classification algorithms should inherit from this class and implement the Train and Predict methods.
The class supports various options like class weighting to handle imbalanced datasets and different classification task types (binary, multi-class, multi-label, ordinal).
Properties
| Property | Summary |
|---|---|
ClassificationTaskType TaskType { get; set; } |
Gets or sets the type of classification task. |
ILossFunction<T> DefaultLossFunction { get; } |
Gets the total number of parameters in the model. |
ParameterLayoutSnapshot ParameterLayout { get; } |
|
Vector<T> ClassLabels { get; set; } |
Gets or sets the class labels learned during training. |
bool SupportsParameterInitialization { get; } |
Whether this classifier supports parameter initialization. |
int NumClasses { get; set; } |
Gets or sets the number of classes in the classification problem. |
int NumFeatures { get; set; } |
Gets or sets the number of features expected by this classifier. |
long ParameterCount { get; } |
|
string[] FeatureNames { get; set; } |
Gets or sets the feature names. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, T> GetFeatureImportance() |
Gets the feature importance scores as a dictionary. |
DynamicShapeInfo GetDynamicShapeInfo() |
|
IEnumerable<ParameterChunk<T>> GetParameterStateChunks() |
Enumerates canonical persistent state without flattening tensor-backed components. |
IEnumerable<Tensor<T>> GetParameterChunks() |
Streams state in the same order as the parameter manifest. |
IEnumerable<int> GetActiveFeatureIndices() |
Gets all model parameters as a single vector. |
IFullModel<T, Matrix<T>, Vector<T>> Clone() |
Creates a clone of the classifier model. |
IFullModel<T, Matrix<T>, Vector<T>> DeepCopy() |
Creates a deep copy of the classifier model. |
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) |
Gets the number of parameters this classifier serializes. |
ModelMetadata<T> GetModelMetadata() |
Gets metadata about the model. |
ModelOptions GetOptions() |
|
Vector<T> GetParameters() |
Gets this classifier’s parameters as a flat vector. |
Vector<T> Predict(Matrix<T> input) |
Predicts class labels for the given input data. |
Vector<T> SanitizeParameters(Vector<T> parameters) |
Sanitizes parameters to ensure they satisfy model constraints. |
bool IsFeatureUsed(int featureIndex) |
Determines whether a specific feature is used in the model. |
byte[] Serialize() |
Gets the type of the model. |
int[] GetInputShape() |
|
int[] GetOutputShape() |
|
void Deserialize(byte[] modelData) |
Deserializes the model from a byte array. |
void Dispose() |
|
void LoadModel(string filePath) |
Loads a classifier model from a file. |
void LoadState(Stream stream) |
Loads the model’s state from a stream. |
void SaveModel(string filePath) |
|
void SaveState(Stream stream) |
Saves the model’s current state to a stream. |
void SetActiveFeatureIndices(IEnumerable<int> featureIndices) |
Sets the active feature indices for this model. |
void SetParameterChunks(IEnumerable<Tensor<T>> chunks) |
Restores tensor-backed chunks on their device and retains custom-setter semantics. |
void SetParameters(Vector<T> parameters) |
Restores this classifier’s parameters from a flat vector. |
void Train(Matrix<T> x, Vector<T> y) |
Trains the classifier on the provided data. |