image_recog
Typer | Posted on | |
from sklearn.datasets import fetch_lfw_people
from sklearn.decomposition import PCA
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
lfw = fetch_lfw_people(min_faces_per_person=150)
X_data = lfw.data
y_target = lfw.target
names = lfw.target_names
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X_data, y_target, test_size=0.3)
# Apply PCA for dimensionality reduction
pca = PCA(n_components=150, whiten=True)
pca.fit(X_train)
pca_train = pca.transform(X_train)
pca_test = pca.transform(X_test)
# Create and train the MLP classifier
mlpc = MLPClassifier()
mlpc.fit(pca_train, y_train)
y_pred = mlpc.predict(pca_test)
report = classification_report(y_test, y_pred, target_names=names)
print(report)