Classification
1. MNIST
函数:fetch_mldata(), permutation(), SGDClassifier(),fit(),predict(),StratifiedKFold(),cross_val_score(),cross_val_predict()
2. Training a Binary Classifier
Stochastic Gradient Descent (SGD) classifier:
Batch Gradient Descent:
some_digit_image = some_digit.reshape(28, 28)修改shape里面参数,图像为何没有变化?
3. Performance Measures
(1) Measuring Accuracy Using Cross-Validation
This demonstrates why accuracy is generally not the preferred performance measure
for classifiers, especially when you are dealing with skewed datasets (i.e., when some
classes are much more frequent than others).
(2)Confusion Matrix:
A much better way to evaluate the performance of a classifier is to look at the confu‐
sion matrix.
函数:precision_score(), recall_score(),f1_score()
(3)Precision/Recall Tradeoff
函数:sgd_clf.decision_function(),cross_val_predict(), precision_recall_curve()
(4)The ROC Curve(receiver operating characteristic curve)受试者工作特征曲线 /感受性曲线(sensitivity curve)
函数:roc_curve(),roc_auc_score(),RandomForestClassifier(),cross_val_predict()
ROC curve plots the true positive rate (another name for recall) against the false positive rate
![](https://i-blog.csdnimg.cn/blog_migrate/4b6126062567fe505a34c48daaa45252.png)
and ROC AUC scores
(2)one-versus-one (OvO) strategy
Another strategy is to train a binary classifier for every pair of digits: one to distinguish 0s and 1s, another to distinguish 0s and 2s, another for 1s and 2s, and so on.For the MNIST problem, this means training 45 binary classifiers! When you want to classify an image, you have to run the image through all 45 classifiers and see which class wins the most duels.
The main advantage of OvO is that each classifier only needs to be trained on the part of the training set for the two classes that it must distinguish.
For most binary classification algorithms, however, OvA is preferred. Scikit-Learn detects when you try to use a binary classification algorithm for a multiclass classification task, and it automatically runs OvA (except for SVM classifiers for which it uses OvO).
函数:decision_function(),OneVsOneClassifier(),predict_proba()
5.Error Analysis
Analyzing the confusion matrix can often give you insights on ways to improve your classifier
First, you need to divide each value in the confusion matrix by the number of images in the corresponding class, so you can compare error rates instead of absolute number of errors (which would make abundant classes look unfairly bad):
Now let’s fill the diagonal with zeros to keep only the errors, and let’s plot the result:
that rows represent actual classes, while columns represent predicted classes. Now you can clearly see the kinds of errors the classifier makes.
6.Multilabel Classification
函数:KNeighborsClassifier(),
In some cases you may want your classifier to output multiple classes for each instance. For example, consider a face-recognition classifier: what should it do if it recognizes several people on the same picture? Of course it should attach one label per person it recognizes.Such a classification system that outputs multiple binary labels is called a multilabel classification system.
7. Multioutput Classification
函数:randint()
It is simply a generalization of multilabel classification where each label can be multiclass (i.e., it can have more than two possible values).Notice that the classifier's output is multilabel (one label per pixel) and each label can have multiple values (pixel intensity ranges from 0 to 255).