Order of priors in sklearn LinearDiscriminantAnalysis

Viewed 151

I'm fitting a Linear Discriminant Analysis model using the stock market data (Smarket.csv) from here. I'm trying to predict Direction with columns Lag1 and Lag2. Direction has two values: Up or Down.

Here is my reproducible code and the result:

import pandas as pd
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

url="https://raw.githubusercontent.com/JWarmenhoven/ISLR-python/master/Notebooks/Data/Smarket.csv"
Smarket=pd.read_csv(url, usecols=range(1,10), index_col=0, parse_dates=True)

X_train = Smarket[:'2004'][['Lag1', 'Lag2']]
y_train =  Smarket[:'2004']['Direction']


LDA = LinearDiscriminantAnalysis()
model = LDA.fit(X_train, y_train)

print(model.priors_)
[0.49198397 0.50801603]

How do I know which prior value corresponds to which class (Up or Down)? I looked at the documentation but there seems to be nothing.

Can someone explain it to me or point me to a resource that explains this?

1 Answers

Although I cannot find an explicit reference in the documentation (I'm sure there is a general one, somewhere), in such cases the classes are ordered alphabetically, ie. in your case it is ['Down', 'Up'].

You can easily verify that this is consistent with your results here; since the priors_ attribute is just passed through the priors argument, which, according to the documentation, is just the class proportions as inferred from the training data (when priors=None, like here):

y_train.value_counts(normalize=True)

gives:

Up      0.508016
Down    0.491984
Name: Direction, dtype: float64

and

model.priors_[0] == (y_train.value_counts(normalize=True)['Down']
# True
model.priors_[1] == (y_train.value_counts(normalize=True)['Up']
# True
Related