Linear Discriminant Analysis is a classifier with a linear decision boundary, generated by fitting class conditional densities to the data and using Bayes' rule. It fits a Gaussian density to each class, assuming that all classes share the same covariance matrix.
So this recipe is a short example on how does Linear Discriminant Analysis work. Let's get started.
from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.datasets import load_iris from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
Let's pause and look at these imports. We have exported train_test_split which helps in randomly breaking the datset in two parts. Here sklearn.dataset is used to import one classification based model dataset. Also, we have exported LinearDiscriminantAnalysis to build our model.
X,y=load_iris(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
Here, we have used load_iris function to import our dataset in two list form (X and y) and therefore kept return_X_y to be True. Further with have broken down the dataset into 2 parts, train and test with ratio 3:4.
Now our dataset is ready.
model = LinearDiscriminantAnalysis()
We have simply built a classification model with LinearDiscriminantAnalysis with default values.
model.fit(X_train, y_train) y_pred= model.predict(X_test)
Here we have simply fit used fit function to fit our model on X_train and y_train. Now, we are predicting the values of X_test using our built model.
Here we have calculated accuracy score using score function for both our train and test set.
Once we run the above code snippet, we will see:
Clearly, the model built for the given datset is efficient on any unknown set.