Machine Learning Chart

7

I'm having a problem generating a graph using Python - Machine Learning - Naive Bayes model - I would plot an F1 for the different values of K, below we have the classifier that gives me the following outputs:

  

Mean Accuracy: 0.896551724138, Mean Precision: 0.63, Mean Recall: 0.425, Mean f1: 0.486031746032.

Naive Bayes Classifier

pipe = Pipeline([
    ('scaler', StandardScaler()),
    ('selector', SelectKBest()),
    ('reducer', PCA(random_state=42)),
    ('classifier', GaussianNB())
])

param_grid = {
'scaler': SCALER,
'selector__k': SELECTOR__K,
'reducer__n_components': REDUCER__N_COMPONENTS
}

# sss = StratifiedShuffleSplit
gnb_grid = GridSearchCV(pipe, param_grid, scoring='f1', cv=sss)

evaluate_model(gnb_grid, X, y, sss)

test_classifier(gnb_grid.best_estimator_, my_dataset, features_list)

Regardless of the data I used I would like to generate a graph with y = f1 score (cross validation) and x = K Best Features, the code below I tried contains an error and only shows me the graph but the data does not appear.

gnb_grid = []
# Plot number of features VS. cross-validation scores
plt.figure()
plt.xlabel("K Best Features")
plt.ylabel("f1 score (cross validation)")
plt.plot(gnb_grid, k_features)
plt.show()

I need to generate one like this from the photo. Thank you for any help you can get.

    
asked by anonymous 01.09.2017 / 02:26

1 answer

4

[TL; DR]
Without the original data it is difficult to be sure that it is not with them, so I created random data to do tests with the code that you present and the graph was plotted successfully, see:

import numpy as np
from matplotlib import pyplot as plt

f1_score = np.random.normal(loc=0.325, scale=0.25, size=50)
f1_score = f1_score[(f1_score > 0) & (f1_score < 1)]
f1_score.sort()

k_features = np.arange(len(f1_score))

plt.figure()
plt.xlabel("K Best Features")
plt.ylabel("f1 score (cross validation)")
plt.plot(k_features, f1_score)
plt.show()

Result:

Download the demo on a jupyter notebook.

    
01.09.2017 / 13:58