ageron / handson-ml

⛔️ DEPRECATED – See https://github.com/ageron/handson-ml3 instead.
Apache License 2.0
25.17k stars 12.92k forks source link

cmap=plt.cm.gray, cmap=mpl.cm.binary and cmap=plt.get_cmap("gray") #575

Open Samrat666 opened 4 years ago

Samrat666 commented 4 years ago

i just wanted to ask that what is the difference between all these three and the other functions. Please help me out sir I searched a lot on the internet over this issue but could not be resolved. My issue is that :-- 1>Are these three almost same function or are different with different set of color maps? 2>Are the color maps same but perform differently when operated on these three? 3>If not then are these functions some data or dataset specific if not then what are the use of these three statements... You can understand my breadth of confusion sir please help...

AbdullahDawud commented 4 years ago

1>Are these three almost same function or are different with different set of color maps?

'gray' and 'binary' are different colormaps. If you refer to matplotlib documentation, the difference is shown graphically. Click the following to see the difference between the binary and gray colormaps: Colormaps

2>Are the color maps same but perform differently when operated on these three?

Now, there are not three different colormaps in the codes you quoted but only two. Two of these codes refer to the same colormap I'm. The first and the third refer to the same colormaps but are just different ways of accessing the same colormap object 'gray'.

3>If not then are these functions some data or dataset specific if not then what are the use of these three statements...

It's been a long time since I have seen these codes. But if I remember correctly, the cm module in the matplotlib package is where the colormaps are found. So, to use colormaps in your codes you have to import the matplotlib.cm module. Now, since the pyplot module in the matplotlib package also imports the matplotlib.cm module in it's code, therefore you can also access the cm module with a syntax like cmap=plt.cm.gray or cmap=plt.get_cmap("gray")

You can consult the source code of the pyplot module to see that it imports the matplotlib.cm module at line 53 and 54 here: pyplot.py

Do not get too much into this source codes at this stage, rather concentrate on the machine learning codes instead.

Samrat666 commented 4 years ago

Thank you so so so very much, sir. Your efforts are so very laudable.

Samrat666 commented 4 years ago

I DON'T UNDERSTAND THE EXPLANATION GIVEN BELOW THE CODE: IT IS IN THE HANDSON ML AGERON BOOK->CHAPTER 3->MNIST CLASSIFICATION->MULTILABEL CLASSIFICATION..

y_train_knn_pred = cross_val_predict(knn_clf, X_train, y_train, cv=3) >>> f1_score(y_train, y_train_knn_pred, average="macro") 0.96845540180280221 This assumes that all labels are equally important, which may not be the case. In particular, if you have many more pictures of Alice than of Bob or Charlie, you may want to give more weight to the classifier’s score on pictures of Alice. One simple option is to give each label a weight equal to its support (i.e., the number of instances with that target label). To do this, simply set average="weighted" in the preceding code

PLEASE SIR IF YOU COULD GIVE A SHORT EXPLANATION OVER THE WEIGHT ATTACHMENT TO EACH LABEL WILL BE HIGHLY INDEBTED.

tcarr032 commented 1 year ago

I don't know if you're still confused after a year but it's just saying that if you had pictures of Alice, Bob, and Charlie and the number of photos of each person looked like this: [1000, 10, 10] then you would want the model to favor choosing Alice more than the others. So we use average="weighted" to give Alice a high chance to be chosen

ageron commented 1 year ago

Here we're not talking about training the model, but rather evaluating it. And we evaluate a model to decide whether it's good enough to deploy to production, or to compare it to other models.

Let me take another example. Suppose you want to evaluate a model that was trained for self-driving cars. To simplify things, suppose it can detect just two things: cars, and fire engines. Say you evaluate the F1 score for cars, and it is very high: when the model says that there's a car, then it's almost certain that there really is a car (that's high precision), and conversely, when there really is a car, then the model almost always detects it (that's high recall). But suppose that the model is not that great with fire engines: if you evaluate the model's F1 score for fire engines, it is quite low.

If you have the time to manually compare different versions of this model, then that's great, you can look at all the available data and decide which model is best depending on many factors. But suppose you're trying to tune the hyperparameters of your model, for example using grid search: in this case, a simple approach is to have a single metric (rather than two F1 scores) and pick the hyperparameters that maximize this metric.

So how can we turn multiple F1 scores (one per class) into a single score? One way is to just compute the mean F1 score. But this will give equal importance to the detection of cars and the detection of fire engines. In real life, the model is much more likely to see cars than fire engines, so it would be a good idea to prefer models that are good at detecting cars, and give less importance to the fire-engine detection capability. We need to give more weight to the F1 score for cars when computing the average. How much more? Well, one approach is to just look at the training set and see how frequent cars are compared to fire engines. That's what average="weighted" does. If cars are 10 times more frequent than fire engines, then the weighted average F1 score will be: (10*CAR_F1_SCORE + 1*FIRE_ENGINE_F1_SCORE) / (10 + 1)

Hope this helps.