Graph of Machine Learning

Asked

Viewed 424 times

8

I have a problem to generate a graph using Python - Machine Learning - Naive Bayes model - would plot an F1 (score) 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.

Classifier Naive Bayes

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 that I tried contains 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 photo. I already thank you for the help that arrive. inserir a descrição da imagem aqui

  • 3

    It seems that this question only has a tangential relationship with machine-learning. Its difficulty is actually how to generate a graph in Python (so much so that the answer you have does just that). I suggest editing to clarify and remove machine learning tags.

1 answer

5

[TL;DR]
Without the original data it is difficult to be sure that it is not with them, so I created random data to test with the code you present and the chart was successfully plotted, 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()

Upshot:

Plot dos dados

Download the demo on a notebook jupyter.

  • I thank Sidon for making a chart that actually worked, it took me a long time to answer because I didn’t think I would have any answer. I really appreciate it. Att.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.