0

I wanted to run LDA for my multiclass data which consist of 3 classes. I read that for multiclass the dimensions are reduced based on the number of classes (C), eg; C-1. But I have no idea how to alter my code.

I have this code which I use to run for my binary data. And I wanted to reduce dimensions of my multiclass data with LDA as well. How do i fix this code for my multiclass problem?

scaler_1 = StandardScaler()
X_scld = scaler_1.fit_transform(X)

lda1=LDA(store_covariance=True)

train_lda_fitting = lda1.fit(X_scld, y)

n_pcs= lda1.covariance_.shape[0]

# get the index of the most important feature on EACH component i.e. largest absolute value
# using LIST COMPREHENSION HERE
most_important = [np.abs(lda1.covariance_[i]).argmax() for i in range(n_pcs)]

initial_feature_names = X.columns

# get the names
most_important_names = [initial_feature_names[most_important[i]] for i in range(n_pcs)]

# using LIST COMPREHENSION HERE AGAIN
dic = {'PC{}'.format(i+1): most_important_names[i] for i in range(n_pcs)}

# build the dataframe
df_LDA = pd.DataFrame(sorted(dic.items()))
df_LDA.columns=['Components','Important Feature']

df_LDA.shape

df_LDA

selected_features_by_LDA = df_LDA['Important Feature'].values
nadia
  • 1

0 Answers0