Linear Discriminant Analysis using Python

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello all, till now, we have learned about pca technique. Now, we will learn about linear discriminant analysis using python.

Linear Discriminant Analysis using Python:

Suppose there are n independent variables in your dataset. Performing linear discriminant analysis using python creates n or less than n new independent variables by separating classes of dependent variables. As it uses dependent variables, it comes under supervised learning.

We will reduce dimensions of our dataset to 2 by linear discriminant analysis using python.


Please download LDA dataset from superdatascience website. You will find wines.csv file in the folder. It contains 14 columns. First 13 columns i.e. independent variables are contents of wine. Last column is customer segment. It has 3 values. This is a classification problem. Our objective is to reduce 13 independent variables to 2 so that we can visualize results on graph.

Data Preprocessing:

  • We imported dataset and created matrices of independent and dependent variables
  • We split dataset into training set and test set.
  • Performed feature scaling.

Linear Discriminant Analysis using Python:

Note that we want only two new new independent features to plot a 2 dimensional graph.

We have called lda.fit_transform(X_train, y_train) by passing y_train also because lda is supervised learning.

Perform Classification:

We simply perform a logistic regression classification with 2 independent variables and predict results for X_test. Execute above code and we can see confusion matrix.

confusion matrix for LDA

confusion matrix for LDA

Note that all diagonal results 6 + 7 + 5 = 18 are correct predictions and all non diagonal results are 0. It means we have received 100% correct predictions in linear discriminant analysis using python.

Visualizing Results:

customer segment classification with lda

customer segment classification with lda



Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *