{"id":9428,"date":"2016-02-26T05:50:04","date_gmt":"2016-02-26T05:50:04","guid":{"rendered":"https:\/\/www.bridge-global.com\/blog\/blog\/\/?p=9428"},"modified":"2019-09-17T10:43:21","modified_gmt":"2019-09-17T10:43:21","slug":"bayes-theorem-implementation-in-python","status":"publish","type":"post","link":"https:\/\/www.bridge-global.com\/blog\/bayes-theorem-implementation-in-python\/","title":{"rendered":"<!--:en-->Bayes&#8217; theorem implementation in python<!--:-->"},"content":{"rendered":"<p><!--:en-->\n<\/p>\n<p>Machine learning is a method of data analysis that automates analytical model building of data set. Using the implemented algorithms that iteratively learn from data, machine learning allows computers to find hidden insights without being explicitly programmed where to look. Naive bayes algorithm is one of the most popular machine learning technique. In this article we will look how to implement Naive bayes algorithm using python.\u00a0<\/p>\n<p>Before someone can understand Bayes\u2019 theorem, they need to know a couple of related concepts first, namely, the idea of Conditional Probability, and Bayes\u2019 Rule.<\/p>\n<p>Conditional Probability is just What is the probability that something will happen, given that something else has already happened.<\/p>\n<p>Let say we have a collection of people. Some of them are singers. They are either male or female. If we select a random sample, what is the probability that this person is a male? what is the probability that this person is a male and singer? Conditional Probability is the best option here. We can calculate probability like,<\/p>\n<p>P(Singer &amp; Male) = P(Male) x P(Singer \/ Male)<\/p>\n<p><strong>What is Bayes rule ?<\/strong><\/p>\n<p>We can simply define Bayes rule like this. Let A1, A2, &#8230; , An be a set of mutually exclusive events that together form the sample space S. Let B be any event from the same sample space, such that P(B) > 0. Then, P( Ak | B ) = P( Ak \u2229 B ) \/ P( A1 \u2229 B ) + P( A2 \u2229 B ) + . . . + P( An \u2229 B )<\/p>\n<p><strong>What is Bayes classifier ?<\/strong><\/p>\n<p>Naive Bayes classifiers are a family of simple probabilistic classifiers based on applying Bayes&#8217; theorem with strong (naive) independence assumptions between the features in machine learning. Basically we can use above theories and equations for classification problem.<\/p>\n<p><strong>Bayes classifier implementation in python<\/strong><\/p>\n<p>Now we have to implement this great theorem in python. Fortunately we have amazing library called scikit-learn in python.In this example we are going to create some random points in three dimensional space. We classified these points onto RED and BLUE. Our task is classify new points in this three dimensional space into either BLUE or RED. Lets start with importing required modules.\u00a0<\/p>\n<p><span style=\"background-color: #c0c0c0;\"><strong><span style=\"color: #008000;\">import<\/span>\u00a0<span style=\"color: #0000ff;\">warnings<\/span><\/strong><\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">warnings.filterwarnings(<span style=\"color: #993300;\">&#8216;ignore&#8217;<\/span>)<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\"><strong><span style=\"color: #008000;\">import<\/span>\u00a0<span style=\"color: #0000ff;\">numpy<\/span>\u00a0<span style=\"color: #008000;\">as<\/span>\u00a0<span style=\"color: #0000ff;\">np<\/span><\/strong><\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\"><strong><span style=\"color: #008000;\">import<\/span>\u00a0<span style=\"color: #0000ff;\">matplotlib.pyplot<\/span>\u00a0<span style=\"color: #008000;\">as<\/span>\u00a0<span style=\"color: #0000ff;\">plt<\/span><\/strong><\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\"><strong><span style=\"color: #008000;\">from<\/span>\u00a0<span style=\"color: #0000ff;\">sklearn.naive_bayes\u00a0<span style=\"color: #008000;\">import<\/span><\/span><\/strong>\u00a0GaussianNB<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\"><strong><span style=\"color: #008000;\">from<\/span>\u00a0<span style=\"color: #0000ff;\">IPython.display\u00a0<span style=\"color: #008000;\">import<\/span><\/span><\/strong>\u00a0Image<\/span><\/p>\n<p>Now we are going to create sample three dimensional data for training<\/p>\n<p><span style=\"background-color: #c0c0c0;\">x_blue = np.array([1,2,1,5,1.5,2.4,4.9,4.5])<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">y_blue = np.array([5,6.3,6.1,4,3.5,2,4.1,3])<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">z_blue = np.array([5,1.3,1.1,1,3.5,2,4.1,3])<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"background-color: #c0c0c0;\">x_red = np.array([5,7,7,8,5.5,6,6.1,7.7])<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">y_red = np.array([5,7.7,7,9,5,4,8.5,5.5])<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">z_red = np.array([5,6.7,7,9,1,4,6.5,5.5])<\/span><\/p>\n<p>We have to format this data to train with sklearn<\/p>\n<p><span style=\"background-color: #c0c0c0;\">red_points = np.array(<span style=\"color: #008000;\">zip<\/span>(x_red,y_red,z_red))<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">blue_points = np.array(<span style=\"color: #008000;\">zip<\/span>(x_blue,y_blue,z_blue))<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">points = np.concatenate([red_points,blue_points])<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">output = np.concatenate([np.ones(x_red.size),np.zeros(x_blue.size)])<\/span><\/p>\n<p>Now we want to classify following points<\/p>\n<p><span style=\"background-color: #c0c0c0;\">predictor = np.array([5.3,4.2,3.3])<\/span><\/p>\n<p>We are going to apply Bays classification theorem<\/p>\n<p><span style=\"background-color: #c0c0c0;\">classifier = GaussianNB()<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">classifier.fit(points,output)<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\"><span style=\"color: #008000;\">print<\/span>\u00a0classifier.predict([predictor])<\/span><\/p>\n<p>Lets move into more real world example. Suppose we have a list of name. We want to classify this names into Male and Female categories . Our classification process is as show in below.<\/p>\n<p><span style=\"background-color: #c0c0c0;\">Image(filename=<span style=\"color: #993300;\">&#8216;classification.png&#8217;<\/span>)<\/span><\/p>\n<p><a href=\"https:\/\/www.bridge-global.com\/blog\/\/wp-content\/uploads\/2018\/01\/download.png\"><img loading=\"lazy\" decoding=\"async\" title=\"Python\" src=\"https:\/\/www.bridge-global.com\/blog\/\/wp-content\/uploads\/2018\/01\/download.png\" alt=\"\" width=\"913\" height=\"471\"><\/a><\/p>\n<p>So first step is feature extraction. I am going to observe (extract) following evidance from a name last letter, last two letter and last_is_vowel<\/p>\n<p>Next step is machine leraning algorithm. of cource we are going to use Naive Bayes classification.<\/p>\n<p>Lets start implimentation in python. As it is more a NLP problem, We could use NLTK module from python. We have two csv files traing file names.txt and predict.txt which contains name to be predicted.<\/p>\n<p>Lets import required module,<\/p>\n<p><span style=\"background-color: #c0c0c0;\"><strong><span style=\"color: #008000;\">import<\/span>\u00a0<span style=\"color: #0000ff;\">numpy<\/span>\u00a0<span style=\"color: #008000;\">as<\/span>\u00a0<span style=\"color: #0000ff;\">np<\/span><\/strong><\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\"><strong><span style=\"color: #008000;\">import<\/span>\u00a0<span style=\"color: #0000ff;\">pandas<\/span>\u00a0<span style=\"color: #008000;\">as<\/span>\u00a0<span style=\"color: #0000ff;\">pd<\/span><\/strong><\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\"><strong><span style=\"color: #008000;\">import<\/span>\u00a0<span style=\"color: #0000ff;\">nltk<\/span><\/strong><\/span><\/p>\n<p>Define a function that parse csv file and return feature sets. We are using panda for parsing csv file.<\/p>\n<p><span style=\"background-color: #c0c0c0;\"><span style=\"color: #008000;\"><strong>def<\/strong><\/span>\u00a0<span style=\"color: #0000ff;\">get_data<\/span>(name, result=<span style=\"color: #993300;\">&#8220;gender&#8221;<\/span>):\u00a0<\/span><\/p>\n<p>\u00a0 \u00a0<span style=\"background-color: #c0c0c0;\"> df = pd.read_csv(name)<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">\u00a0 \u00a0 df[<span style=\"color: #993300;\">&#8216;last_letter&#8217;<\/span>] = df.apply (<span style=\"color: #008000;\"><strong>lambda<\/strong><\/span>\u00a0row: row[0][-1],axis=1)<\/span><\/p>\n<p>\u00a0<span style=\"background-color: #c0c0c0;\"> \u00a0 df[<span style=\"color: #993300;\">&#8216;last_two_letter&#8217;<\/span>] = df.apply (<span style=\"color: #008000;\"><strong>lambda<\/strong><\/span>\u00a0row: row[0][-2:],axis=1)<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">\u00a0 \u00a0 df[<span style=\"color: #993300;\">&#8216;last_is_vowel&#8217;<\/span>] = df.apply (<span style=\"color: #008000;\"><strong>lambda<\/strong><\/span>\u00a0row:\u00a0<span style=\"color: #008000;\">int<span style=\"color: #000000;\">(<\/span><\/span>row[0][-1]\u00a0<span style=\"color: #800080;\"><strong>in<\/strong><\/span>\u00a0<span style=\"color: #993300;\">&#8220;aeiouy&#8221;<\/span>),axis=1)<\/span><\/p>\n<p>\u00a0 <span style=\"background-color: #c0c0c0;\">\u00a0 train = df.loc[:,[<span style=\"color: #993300;\">&#8216;last_letter&#8217;,&#8217;last_two_letter&#8217;,&#8217;last_is_vowel&#8217;<span style=\"color: #000000;\">]<\/span><\/span>]<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">\u00a0 \u00a0 train_dicts = train.T.to_dict().values()<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">\u00a0 \u00a0 genders = df.loc[:,[result]][result]<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\"><span style=\"color: #008000;\"><strong>\u00a0 \u00a0 return<\/strong><\/span>\u00a0[(train_dict, gender)\u00a0<span style=\"color: #008000;\"><strong>for<\/strong><\/span>\u00a0train_dict,gender\u00a0<span style=\"color: #800080;\"><strong>in<\/strong><\/span>\u00a0<span style=\"color: #008000;\">zip<\/span>(train_dicts,genders)]<\/span><\/p>\n<p>our names.txt is looks like,<\/p>\n<p><span style=\"background-color: #c0c0c0;\">df = pd.read_csv(<span style=\"color: #993300;\">&#8220;names.txt&#8221;<\/span>)<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\"><span style=\"color: #008000;\"><strong>print<\/strong><\/span>\u00a0df<\/span><\/p>\n<p>name gender<\/p>\n<p>0 ebin M<\/p>\n<p>1 leekas M<\/p>\n<p>2 jinesh M<\/p>\n<p>3 neethu F<\/p>\n<p>4 mary F<\/p>\n<p>5 neenu F<\/p>\n<p>6 sanitha F<\/p>\n<p>7 lekha F<\/p>\n<p><span style=\"background-color: #c0c0c0;\">df[<span style=\"color: #993300;\">&#8216;last_letter&#8217;<\/span>] = df.apply (<span style=\"color: #008000;\"><strong>lambda<\/strong><\/span>\u00a0row: row[0][-1],axis=1)<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">df[<span style=\"color: #993300;\">&#8216;last_two_letter&#8217;<\/span>] = df.apply (<span style=\"color: #008000;\"><strong>lambda<\/strong><\/span>\u00a0row: row[0][-2:],axis=1)<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">df[<span style=\"color: #993300;\">&#8216;last_is_vowel&#8217;<\/span>] = df.apply (<span style=\"color: #008000;\"><strong>lambda<\/strong><\/span>\u00a0row:\u00a0<span style=\"color: #008000;\">int<\/span>(row[0][-1]\u00a0<span style=\"color: #800080;\"><strong>in<\/strong><\/span>\u00a0<span style=\"color: #993300;\">&#8220;aeiouy&#8221;<\/span>),axis=1)<\/span><\/p>\n<p>The extracted features are like<\/p>\n<p><span style=\"background-color: #c0c0c0;\"><span style=\"color: #008000;\"><strong>print<\/strong><\/span>\u00a0df<\/span><\/p>\n<div style=\"overflow-x:auto;\">\u00a0<\/p>\n<table id=\"python_output\">\n<tr>\n<td>&nbsp;<\/td>\n<td><em>\u00a0name<\/em><\/td>\n<td><em>gender<\/em><\/td>\n<td><em>last_letter<\/em><\/td>\n<td><em>last_two_letter<\/em><\/td>\n<td><em>last_is_vowel<\/em><\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>ebin<\/td>\n<td>M<\/td>\n<td>n<\/td>\n<td>in<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>leekas<\/td>\n<td>M<\/td>\n<td>s<\/td>\n<td>as<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>jinesh<\/td>\n<td>M<\/td>\n<td>h<\/td>\n<td>sh<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>neethu<\/td>\n<td>F<\/td>\n<td>u<\/td>\n<td>hu<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>mary<\/td>\n<td>F<\/td>\n<td>y<\/td>\n<td>ry<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>neenu<\/td>\n<td>F<\/td>\n<td>u<\/td>\n<td>nu<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>sanitha<\/td>\n<td>F<\/td>\n<td>a<\/td>\n<td>ha<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>lekha<\/td>\n<td>F<\/td>\n<td>a<\/td>\n<td>ha<\/td>\n<td>1<\/td>\n<\/tr>\n<\/table>\n<\/div>\n<p>Now we want to train with data from names.txt<\/p>\n<p><span style=\"background-color: #c0c0c0;\">train_set = get_data(<span style=\"color: #993300;\">&#8220;names.txt&#8221;<\/span>)<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\">classifier = nltk.NaiveBayesClassifier.train(train_set)<\/span><\/p>\n<p>Finally we want to test our model. We can use names from predict.txt file to test the created model<\/p>\n<p><span style=\"background-color: #c0c0c0;\"><span style=\"color: #008000;\"><strong>for<\/strong><\/span>\u00a0name_and_feature\u00a0<span style=\"color: #800080;\"><strong>in<\/strong><\/span>\u00a0get_data(<span style=\"color: #993300;\">&#8220;predict.txt&#8221;,&#8221;name&#8221;<\/span>):<\/span><\/p>\n<p><span style=\"background-color: #c0c0c0;\"><span style=\"color: #008000;\"><strong>\u00a0 \u00a0 print<\/strong>\u00a0<\/span>name_and_feature[1],<span style=\"color: #993300;\">&#8220;==&#8221;<\/span>, classifier.classify(name_and_feature[0])<\/span><\/p>\n<p>sukesh == M<\/p>\n<p>jithil == M<\/p>\n<p>sijith == M<\/p>\n<p>maria == F<\/p>\n<p>soumya == F<\/p>\n<p>neethu == F<\/p>\n<p><!--:--><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Machine learning is a method of data analysis that automates analytical model building of data set. Using the implemented algorithms that iteratively learn from data, machine learning allows computers to find hidden insights without being explicitly programmed where to look. &hellip;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":88,"featured_media":9431,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-9428","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-offshoring"],"featured_image_src":"https:\/\/www.bridge-global.com\/blog\/wp-content\/uploads\/2016\/02\/df1.png","author_info":{"display_name":"Ebin","author_link":"https:\/\/www.bridge-global.com\/blog\/author\/ebin\/"},"_links":{"self":[{"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/posts\/9428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/users\/88"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/comments?post=9428"}],"version-history":[{"count":6,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/posts\/9428\/revisions"}],"predecessor-version":[{"id":28339,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/posts\/9428\/revisions\/28339"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/media\/9431"}],"wp:attachment":[{"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/media?parent=9428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/categories?post=9428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bridge-global.com\/blog\/wp-json\/wp\/v2\/tags?post=9428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}