"Stove-refining AI" machine learning 016-How to know the confidence level of the SVM model output category

Source: Internet
Author: User
Tags svm

Tag: is the upload function--data set strong LIB new 1.5

"Stove-refining AI" machine learning 016-How to know the confidence level of the SVM model output category

(Python libraries and version numbers used in this article: Python 3.5, Numpy 1.14, Scikit-learn 0.19, matplotlib 2.2)

In general, for unknown samples, we predict by the model belong to a certain category, often give the probability of this category.

For example, the AI model to identify a certain picture is the probability of "dog" is 95.8%, is the probability of "cat" is 4.2%, then can SVM get a similar category of probability value?


1. Prepare the data set

This part of the code and the previous article ("Furnace AI" machine learning 014-using SVM to build a nonlinear classification model) are almost the same, so don't repeat it.


2. Calculate the confidence level of the new sample

Here, we build our own nonlinear SVM classification model, and use this model to classify the new sample data in categories. The following code

# 计算某个新样本的置信度new_samples=np.array([[2,1.5],                      [8,9],                      [4.8,5.2],                      [4,4],                      [2.5,7],                      [7.6,2],                      [5.4,5.9]])classifier3=SVC(kernel=‘rbf‘,probability=True) # 比上面的分类器增加了 probability=True参数classifier3.fit(train_X,train_y)# 使用训练好的SVM分类器classifier3对新样本进行预测,并给出置信度for sample in new_samples:    print(‘sample: {}, probs: {}‘.format(sample,classifier3.predict_proba([sample])[0]))

-------------------------------------lose-----------------------------------------

Sample: [2.1.5], probs: [0.08066588 0.91933412]
Sample: [8.9.], probs: [0.08311977 0.91688023]
Sample: [4.8 5.2], probs: [0.14367183 0.85632817]
Sample: [4.4.], probs: [0.06178594 0.93821406]
Sample: [2.5 7.], probs: [0.21050117 0.78949883]
Sample: [7.6 2.], probs: [0.07548128 0.92451872]
Sample: [5.4 5.9], probs: [0.45817727 0.54182273]

--------------------------------------------finished-------------------------------------

Drawing this new sample data point to the 2D distribution map, you can get the following results.

####################### #小 ********** Knot ###############################

1. As you can see, if you want to output different probabilities for each category, you need to set the parameter probability=true, and you need to use the Classifier.predict_proba () function to get the class probability value.

2. The probability of the model output sample category is the confidence that the sample belongs to this category.

#################################################################


Note: This section of the code has been uploaded to ( my GitHub), Welcome to download.

Resources:

1, Python machine learning classic example, Prateek Joshi, Tao Junjie, Chen Xiaoli translation

"Stove-refining AI" machine learning 016-How to know the confidence level of the SVM model output category

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.