<jupyter_start><jupyter_text>PCA transformation before running classifier<jupyter_code># Finding the best number of principle components

pca = PCA()
pca.fit(X_train) #

cum_exp_var = np.cumsum(pca.explained_variance_ratio_)*100
print('This step done')
num_components = [n for n in range(len(cum_exp_var))]
plt.figure(figsize=(10, 10))
plt.plot(num_compenents, cum_exp_var, color = 'red',label='cumulative explained variance')
plt.title('Cumulative Explained Variance as a Function of the Number of Components')
plt.ylabel('Cumulative Explained variance')
plt.xlabel('Principal components')
plt.axhline(y = 95, color='k', linestyle='--', label = '95% Explained Variance')
plt.legend(loc='best')
cumsum = np.cumsum(pca.explained_variance_ratio_)*100
d = [n for n in range(len(cumsum))]
plt.figure(figsize=(10, 10))
plt.plot(d,cumsum, color = 'red',label='cumulative explained variance')
plt.title('Cumulative Explained Variance as a Function of the Number of Components')
plt.ylabel('Cumulative Explained variance')
plt.xlabel('Principal components')
plt.axhline(y = 95, color='k', linestyle='--', label = '95% Explained Variance')
plt.legend(loc='best')
# fitting and transforming PCA transformation

pca = PCA(n_components = 10)
pca.fit(X_train)
pca_X_train = pca.transform(X_train)
pca_X_test = pca.transform(X_test)

# # Splitting to training and test dataset



print('Data transformed')

# Building a Linear Support Vector Classifier
pca_clf = LinearSVC(random_state=25, tol=1e-5)
pca_clf.fit(pca_X_train, y_train)

print('Data fit')

# Prediciting both outcomes for training set and test set for error calculation
y_train_pred = pca_clf.predict(pca_X_train)
y_test_pred = pca_clf.predict(pca_X_test)

print('Data Creating Report')

# Producing mean accuracy score and classification report for finding performance of model in 
target_names = ['male', 'female']
print('Accuracy score on training data, {0:.2f}'.format(pca_clf.score(pca_X_train, y_train)))
print('Classification Report on training data')
print(classification_report(y_train, y_train_pred, target_names=target_names))
print('Accuracy score on test data, {0:.2f}'.format(pca_clf.score(pca_X_test, y_test)))
print('Classification Report on test data')
print(classification_report(y_test, y_test_pred, target_names=target_names))

lda = LinearDiscriminantAnalysis()
lda.fit(X_train, y_train)

lda_X_train = lda.transform(X_train)
lda_X_test = lda.transform(X_test)

print('Data transformed')

lda_clf = LinearSVC(random_state=25, tol=1e-5)
lda_clf.fit(lda_X_train, y_train)

print('Data fit')

y_train_pred = lda_clf.predict(lda_X_train)
y_test_pred = lda_clf.predict(lda_X_test)

print('Data Creating Report')

# Producing mean accuracy score and classification report for finding performance of model in 
target_names = ['male', 'female']
print('Accuracy score on training data, {0:.2f}'.format(lda_clf.score(lda_X_train, y_train)))
print('Classification Report on training data')
print(classification_report(y_train, y_train_pred, target_names=target_names))
print('Accuracy score on test data, {0:.2f}'.format(lda_clf.score(lda_X_test, y_test)))
print('Classification Report on test data')
print(classification_report(y_test, y_test_pred, target_names=target_names))
# Producing mean accuracy score and classification report for finding performance of model in 
target_names = ['male', 'female']
print('Accuracy score on training data, {0:.2f}'.format(lda_clf.score(lda_X_train, y_train)))
print('Classification Report on training data')
print(classification_report(y_train, y_train_pred, target_names=target_names))
print('Accuracy score on test data, {0:.2f}'.format(lda_clf.score(lda_X_test, y_test)))
print('Classification Report on test data')
print(classification_report(y_test, y_test_pred, target_names=target_names))<jupyter_output>Accuracy score on training data, 1.00
Classification Report on training data
             precision    recall  f1-score   support

       male       1.00      1.00      1.00       876
     female       1.00      1.00      1.00       882

avg / total       1.00      1.00      1.00      1758

Accuracy score on test data, 0.48
Classification Report on test data
             precision    recall  f1-score   support

       male       0.47      0.47      0.47       366
     female       0.50      0.49      0.49       388

avg / total       0.48      0.48      0.48       754
{'fraction_non_alphanumeric': 0.10852713178294573, 'fraction_numerical': 0.028499772001823985, 'mean_word_length': 5.152875175315568, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '567885', 'license': 'MIT', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1473, 'n_words': 375, 'path': 'assignments/Assignment_3_ML_Fashionista/ML_Fashionista.ipynb', 'repo_name': 'nik1997/cs156', 'size': '4386'}