SKLearner Home | About | Contact | Examples

Configure LinearDiscriminantAnalysis "solver" Parameter

The solver parameter in scikit-learn’s LinearDiscriminantAnalysis determines the algorithm used to solve the LDA problem.

Linear Discriminant Analysis (LDA) is a method used for dimensionality reduction and classification. It projects features onto a lower-dimensional space while maximizing class separability.

The solver parameter affects how the LDA solution is computed, impacting both performance and computational efficiency. Different solvers are better suited for different dataset characteristics.

The default value for solver is ‘svd’. Other options include ’lsqr’ and ’eigen’.

In practice, ‘svd’ is often a good default choice, while ’lsqr’ can be faster for large datasets, and ’eigen’ is useful when the number of features is much larger than the number of samples.

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.metrics import accuracy_score

# Generate synthetic dataset
X, y = make_classification(n_samples=1000, n_features=10, n_classes=2,
                           n_informative=10, n_redundant=0, random_state=42)

# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train with different solver options
solvers = ['svd', 'lsqr', 'eigen']
accuracies = []

for solver in solvers:
    lda = LinearDiscriminantAnalysis(solver=solver)
    lda.fit(X_train, y_train)
    y_pred = lda.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    accuracies.append(accuracy)
    print(f"solver={solver}, Accuracy: {accuracy:.3f}")

Running the example gives an output like:

solver=svd, Accuracy: 0.825
solver=lsqr, Accuracy: 0.825
solver=eigen, Accuracy: 0.825

The key steps in this example are:

  1. Generate a synthetic multi-class classification dataset
  2. Split the data into train and test sets
  3. Train LinearDiscriminantAnalysis models with different solver options
  4. Evaluate the accuracy of each model on the test set

Some tips and heuristics for setting solver:

Issues to consider:



See Also