Python Implementation of Naive Bayes

Source: Internet
Author: User

Take the test tomorrow. You can bring your computer to your computer and write the program first. Save your effort to use a calculator ...... Directly use the Python source code. [Python] # Naive Bayes # Calculate the Prob. of class: clsdef P (data, cls_val, cls_name = "class"): cnt = 0.0 for e in data: if e [cls_name] = cls_val: cnt + = 1 return cnt/len (data) # Calculate the Prob (attr | cls) def PT (data, cls_val, attr_name, attr_val, cls_name = "class "): cnt1 = 0.0 cnt2 = 0.0 for e in data: if e [cls_name] = cls_val: cnt1 + = 1 if e [attr_name] = attr_val: cnt2 + = 1 return cnt2/cnt1 # Calculate the NBdef NB (data, test, cls_y, cls_n): PY = P (data, cls_y) PN = P (data, cls_n) for key, val in test. items (): print key, val PY * = PT (data, cls_y, key, val) PN * = PT (data, cls_n, key, val) return {cls_y: PY, cls_n: PN} if _ name _ = "_ main _": # data = [{"outlook": "sunny", "temp ": "hot", "humidity": "high", "wind": "weak", "class": "no" },{ "outlook": "sunny ", "temp": "hot", "humidity": "high", "wind": "strong", "class": "no" },{ "outlook ": "overcast", "temp": "hot", "humidity": "high", "wind": "weak", "class": "yes "}, {"outlook": "rain", "temp": "mild", "humidity": "high", "wind": "weak", "class ": "yes"}, {"outlook": "rain", "temp": "cool", "humidity": "normal", "wind": "weak ", "class": "yes" },{ "outlook": "rain", "temp": "cool", "humidity": "normal", "wind ": "strong", "class": "no" },{ "outlook": "overcast", "temp": "cool", "humidity": "normal ", "wind": "strong", "class": "yes" },{ "outlook": "sunny", "temp": "mild", "humidity ": "high", "wind": "weak", "class": "no" },{ "outlook": "sunny", "temp": "cool ", "humidity": "normal", "wind": "weak", "class": "yes" },{ "outlook": "rain", "temp ": "mild", "humidity": "normal", "wind": "weak", "class": "yes" },{ "outlook": "sunny ", "temp": "mild", "humidity": "normal", "wind": "strong", "class": "yes" },{ "outlook ": "overcast", "temp": "mild", "humidity": "high", "wind": "strong", "class": "yes "}, {"outlook": "overcast", "temp": "hot", "humidity": "normal", "wind": "weak", "class ": "yes" },{ "outlook": "rain", "temp": "mild", "humidity": "high", "wind": "strong ", "class": "no"},] # calculate print NB (data, {"outlook": "sunny", "temp": "cool", "humidity ": "high", "wind": "strong"}, "yes", "no") [/python]

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.