-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnmf.py
37 lines (24 loc) · 1.24 KB
/
nmf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from main import fetch_dataset, fetch_data_details, split_data, dimensionality_reduction_NMF, train_text_transform_Model, classification_svc, prediction, print_report, plot_images, title
# Load data
dataset = fetch_dataset()
# get dataset details and target names
n_samples, height, width, X, n_features, y, target_names, n_classes = fetch_data_details(dataset)
# split into a training and testing set
X_train, X_test, y_train, y_test = split_data(X, y)
# compute NMF
n_components = 150
nmf, eigenfaces = dimensionality_reduction_NMF(n_components, X_train, height, width)
X_train_nmf, X_test_nmf = train_text_transform_Model(nmf, X_train, X_test)
# Training a SVM classification model
clf = classification_svc(X_train_nmf, y_train)
# Quantitative evaluation of the model quality on the test set
y_pred = prediction(clf, X_test_nmf)
# printing classification report
print_report(y_test, y_pred, target_names, n_classes)
# printing images
prediction_titles = [title(y_pred, y_test, target_names, i)
for i in range(y_pred.shape[0])]
plot_images(X_test, prediction_titles, height, width)
# plot eigenfaces
eigenface_titles = ["eigenface %d" % i for i in range(eigenfaces.shape[0])]
plot_images(eigenfaces, eigenface_titles, height, width)