VectorModel<T>

Models & Types · AiDotNet.Models

Represents a linear model that uses a vector of coefficients to make predictions.

Inheritance: ModelBase<T, Matrix<T>, Vector<T>> → VectorModel<T>

Implements: IInterpretableModel<T>

For Beginners

This is a simple linear model that makes predictions by multiplying each input by a weight and adding them up.

When using this model:

  • Each input feature has a corresponding coefficient (weight)
  • Predictions are made by multiplying each input by its coefficient and summing the results
  • The model can be trained using linear regression on example data
  • It supports genetic algorithm operations for optimization

For example, if predicting house prices, the model might learn that: price = 50,000 * bedrooms + 100 * square_feet + 20,000 * bathrooms

This is one of the simplest and most interpretable machine learning models, making it a good starting point for many problems.

How It Works

This class implements a simple linear model where predictions are made by computing the dot product of the input features and a vector of coefficients. It provides methods for training the model using linear regression, evaluating predictions, and genetic algorithm operations like mutation and crossover. This model is useful for linear regression problems and can serve as a building block for more complex models.

Example

// bedrooms, square feet, bathrooms
var features = new Matrix<double>(new double[,]
{
    { 3, 1500, 2 }, { 4, 2100, 3 }, { 2, 900, 1 }, { 5, 3000, 3 }
});
var prices = new Vector<double>(new double[] { 320000, 415000, 210000, 560000 });
            
// A linear model is one coefficient per feature; the builder fits them.
var coefficients = new Vector<double>(new double[] { 50000, 100, 20000 });
            
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new VectorModel<double>(coefficients))
    .Build(features, prices);
            
// Predict the price of a 3-bed, 1500 sqft, 2-bath house
var input = new Matrix<double>(1, 3);
input[0, 0] = 3; input[0, 1] = 1500; input[0, 2] = 2;
Vector<double> prediction = result.Predict(input);

Constructors

Constructor Summary
VectorModel(Vector<T> coefficients, ILossFunction<T> lossFunction) Initializes a new instance of the VectorModel class with the specified coefficients.

Properties

Property Summary
ILossFunction<T> DefaultLossFunction { get; } Gets the default loss function used by this model for gradient computation.
T Intercept { get; set; } Gets the fitted constant term of the linear regression model.
Vector<T> Coefficients { get; set; } Gets the vector of coefficients used by the model.
int Complexity { get; } Gets the complexity of the model.
int FeatureCount { get; } Gets the number of features used by the model.

Methods

Method Summary
Dictionary<string, T> GetFeatureImportance() Gets the feature importance scores.
IEnumerable<int> GetActiveFeatureIndices() Gets the indices of all features used by this model.
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) Updates the model with new parameter values.
ModelMetadata<T> GetModelMetadata() Gets metadata about the model.
T Evaluate(Vector<T> input) Evaluates the model for a given input vector.
Task<AnchorExplanation<T>> GetAnchorExplanationAsync(Matrix<T> input, T threshold) Gets anchor explanation for a given input (IInterpretableModel implementation).
Task<AnchorExplanation<T>> GetAnchorExplanationAsync(Tensor<T> input, T threshold) Gets anchor explanation for a given input (IInterpretableModel implementation).
Task<CounterfactualExplanation<T>> GetCounterfactualAsync(Matrix<T> input, Vector<T> desiredOutput, int maxChanges) Gets counterfactual explanation for a given input and desired output (IInterpretableModel implementation).
Task<CounterfactualExplanation<T>> GetCounterfactualAsync(Tensor<T> input, Tensor<T> desiredOutput, int maxChanges) Gets counterfactual explanation for a given input and desired output (IInterpretableModel implementation).
Task<DeepLIFTExplanation<T>> GetDeepLIFTAsync(Tensor<T> input, Tensor<T> baseline, bool useRevealCancel) Gets DeepLIFT attributions for a prediction.
Task<Dictionary<int, T>> GetGlobalFeatureImportanceAsync() Gets the global feature importance across all predictions.
Task<Dictionary<int, T>> GetLocalFeatureImportanceAsync(Matrix<T> input) Gets the local feature importance for a specific input (IInterpretableModel implementation).
Task<Dictionary<int, T>> GetLocalFeatureImportanceAsync(Tensor<T> input) Gets the local feature importance for a specific input (IInterpretableModel implementation).
Task<Dictionary<string, object>> GetModelSpecificInterpretabilityAsync() Gets model-specific interpretability information.
Task<FairnessMetrics<T>> ValidateFairnessAsync(Matrix<T> inputs, int sensitiveFeatureIndex) Validates fairness metrics for the given inputs (IInterpretableModel implementation).
Task<FairnessMetrics<T>> ValidateFairnessAsync(Tensor<T> inputs, int sensitiveFeatureIndex) Validates fairness metrics for the given inputs (IInterpretableModel implementation).
Task<GradCAMExplanation<T>> GetGradCAMAsync(Tensor<T> input, int targetClass) Gets GradCAM visual explanation for a prediction.
Task<IntegratedGradientsExplanation<T>> GetIntegratedGradientsAsync(Tensor<T> input, Tensor<T> baseline, int numSteps) Gets Integrated Gradients attributions for a prediction.
Task<LimeExplanation<T>> GetLimeExplanationAsync(Matrix<T> input, int numFeatures) Gets LIME explanation for a specific input (IInterpretableModel implementation).
Task<LimeExplanation<T>> GetLimeExplanationAsync(Tensor<T> input, int numFeatures) Gets LIME explanation for a specific input (IInterpretableModel implementation).
Task<Matrix<T>> GetShapValuesAsync(Matrix<T> inputs) Gets SHAP values for the given inputs (IInterpretableModel implementation).
Task<Matrix<T>> GetShapValuesAsync(Tensor<T> inputs) Gets SHAP values for the given inputs (IInterpretableModel implementation).
Task<PartialDependenceData<T>> GetPartialDependenceAsync(Vector<int> featureIndices, int gridResolution) Gets partial dependence data for specified features.
Task<T> GetFeatureInteractionAsync(int feature1Index, int feature2Index) Gets feature interaction effects between two features.
Task<string> GenerateTextExplanationAsync(Matrix<T> input, Vector<T> prediction) Generates a text explanation for a prediction (IInterpretableModel implementation).
Task<string> GenerateTextExplanationAsync(Tensor<T> input, Tensor<T> prediction) Generates a text explanation for a prediction (IInterpretableModel implementation).
Vector<T> ComputeGradients(Matrix<T> input, Vector<T> target, ILossFunction<T> lossFunction) Computes gradients of the loss function with respect to model parameters WITHOUT updating parameters.
Vector<T> Predict(Matrix<T> input) Predicts outputs for the provided input.
bool IsFeatureUsed(int featureIndex) Determines whether a specific feature is used by the model.
void ApplyGradients(Vector<T> gradients, T learningRate) Applies pre-computed gradients to update the model parameters (coefficients).
void ConfigureFairness(Vector<int> sensitiveFeatures, FairnessMetric[] fairnessMetrics) Configures fairness evaluation settings.
void EnableMethod(InterpretationMethod[] methods) Enables specific interpretation methods.
void LoadModel(string filePath) Loads a model from a file into the current instance.
void LoadState(Stream stream) Loads the model’s state (parameters and configuration) from a stream.
void SaveModel(string filePath) Saves the model to a file.
void SaveState(Stream stream) Saves the model’s current state (parameters and configuration) to a stream.
void SetActiveFeatureIndices(IEnumerable<int> featureIndices)
void SetBaseModel(IFullModel<T, Matrix<T>, Vector<T>> model) Sets the base model for interpretability analysis.
void SetBaseModel(IFullModel<T, TInput, TOutput> model) Sets the base model for interpretability analysis.
void Train(Matrix<T> input, Vector<T> expectedOutput) Trains the model on the provided generic input and expected output.