<jupyter_start><jupyter_text>TP 4 - High dimensionalityLet's build a face recognition system. We are going to compare several models:- Logistic regression- SVM classifier<jupyter_code>from sklearn.datasets import fetch_lfw_people
import matplotlib.pyplot as plt

faces = fetch_lfw_people(min_faces_per_person=70)

for i in range(6):
    plt.subplot(2, 3, i + 1)
    plt.xticks([])
    plt.yticks([])
    plt.imshow(faces.images[i])
    plt.xlabel(faces.target_names[faces.target[i]])
print(faces.target[1])  # The output is a number
from sklearn.model_selection import train_test_split

X = faces.data
y = faces.target
X_train, X_test, y_train, y_test = train_test_split(X, y)
print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)
from sklearn.linear_model import LogisticRegression

model = LogisticRegression(multi_class='multinomial', solver='sag')

# Please uncomment the following lines to verify that it's slow

"""model.fit(X_train, y_train)
model.score(X_test, y_test)"""<jupyter_output><empty_output><jupyter_text>Dimensionality reduction<jupyter_code>from sklearn.decomposition import PCA

pca = PCA(n_components=150, svd_solver='randomized')
X_train_small = pca.fit_transform(X_train)<jupyter_output><empty_output><jupyter_text>Verify the shape of `X_train_small`, fit your model on the new features and compute the score.<jupyter_code>%%timeit -n1 -r1  # This is a timer
# Your code here to train on the new features<jupyter_output><empty_output><jupyter_text>In TP 1 you learned another classifier, right? Fit it and compute its score.<jupyter_code>from sklearn.svm import SVC
svc = SVC()
%%timeit -n1 -r1
# Your code here to train SVM classifier on the new features
svc  # To display its parameters<jupyter_output><empty_output><jupyter_text>Try again with the `class_weight` parameter set to `balanced`.Use GridSearchCV to find a better `C` hyperparameter. Which model is better?<jupyter_code>%%timeit -n1 -r1
from sklearn.model_selection import GridSearchCV

# Your code here<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09502708025603152, 'fraction_numerical': 0.008862629246676515, 'mean_word_length': 6.335740072202166, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '121246', 'license': 'MIT', 'n_tokens_mistral': 712, 'n_tokens_neox': 690, 'n_words': 190, 'path': 'tp4/TP4.ipynb', 'repo_name': 'jilljenn/tp-ml', 'size': '2031'}