400 likes | 402 Views
<br><br><br><br>ExcelR is a leading Data Scientist Certification in pune training institute.Data Scientist Certification in pune will be delivered by highly experienced and certified trainers who are considered as one the best trainers in the industry and so we are considered to be one of the best Data Scientist Certification in pune training institutes.<br> <br>
E N D
1-Nearest Neighbor Classifier Test Examples (What class to assign this?)
Ingredient apple Bacon banana carrot celery cheese Sweetness Crunchiness Food type 9 fruit 4 protein 1 fruit 10 vegetable 10 vegetable 1 protein 10 1 10 7 3 1
Measuring similarity with distance Locating the tomato's nearest neighbors requires a distance function, or a formula that measures the similarity between the two instances. There are many different ways to calculate distance. Traditionally, the k-NN algorithm uses Euclidean distance, which is the distance one would measure if it were possible to use a ruler to connect two points, illustrated in the previous figure by the dotted lines connecting the tomato to its neighbors.
Euclidean distance Euclidean distance is specified by the following formula, where p and q are the examples to be compared, each having n features. The term p1 refers to the value of the first feature of example p, while q1 refers to the value of the first feature of example q:
Application of KNN Which Class Tomoto belongs to given the feature values: Tomato (sweetness = 6, crunchiness = 4),
Understanding probability The probability of an event is estimated from the observed data by dividing the number of trials in which the event occurred by the total number of trials For instance, if it rained 3 out of 10 days with similar conditions as today, the probability of rain today can be estimated as 3 / 10 = 0.30 or 30 percent. Similarly, if 10 out of 50 prior email messages were spam, then the probability of any incoming message being spam can be estimated as 10 / 50 = 0.20 or 20 percent. For example, given the value P(spam) = 0.20, we can calculate P(ham) = 1 – 0.20 = 0.80 Note: The probability of all the possible outcomes of a trial must always sum to 1
Understanding probability cont.. For example, given the value P(spam) = 0.20, we can calculate P(ham) = 1 – 0.20 = 0.80 Because an event cannot simultaneously happen and not happen, an event is always mutually exclusive and exhaustive with its complement The complement of event A is typically denoted Ac or A'. Additionally, the shorthand notation P(¬A) can used to denote the probability of event A not occurring, as in P(¬spam) = 0.80. This notation is equivalent to P(Ac).
Understanding joint probability Often, we are interested in monitoring several nonmutually exclusive events for the same trial All emails Lotter y 5% Spam 20% Ham 80%
Understanding joint probability Lottery appearing in Spam Lottery appearin g in Ham Lottery without appearin g in Spam Estimate the probability that both P(spam) and P(Spam) occur, which can be written as P(spam ∩ Lottery). the notation A ∩ B refers to the event in which both A and B occur.
Calculating P(spam ∩ Lottery) depends on the joint probability of the two events or how the probability of one event is related to the probability of the other. If the two events are totally unrelated, they are called independent events If P(spam) and P(Lottery) were independent, we could easily calculate P(spam ∩ Lottery), the probability of both events happening at the same time. Because 20 percent of all the messages are spam, and 5 percent of all the e-mails contain the word Lottery, we could assume that 1 percent of all messages are spam with the term Lottery. More generally, for independent events A and B, the probability of both happening can be expressed as P(A ∩ B) = P(A) * P(B). 0.05 * 0.20 = 0.01
Bayes Rule Bayes Rule: The most important Equation in ML!! Class Prior Data Likelihood given Class ( ) ( )P Data Class P Data ( )=P Class P Class Data ( ) Data Prior (Marginal) Posterior Probability (Probability of class AFTER seeing the data)
Conditional Independence P Fever, BodyAcheViral ( )= P Fever Viral ( )P BodyAcheViral ( ) P Fever, BodyAche ( )¹ P Fever ( )P BodyAche ( ) Simple Independence between two variables: ( )= P X1 ( )P X2 ( ) P X1,X2 Class Conditional Independence assumption: ( P X1,X2C ( ) ¹ P X1 )= P X1C ( )P X2 ( ) P X1,X2 ( )P X2C ( )
Naïve Bayes Classifier Conditional Independence among variables given Classes! D P XdC ( ) Õ P C ( ) ( ) ( )P X1,X2,...,XDC P X1,X2,...,XDC' ¢ )=P C ( = d=1 P C X1,X2,...,XD ( ) Cå D P Xd ( ) Cå Õ ¢ C d=1 ¢ Simplifying assumption Baseline model especially when large number of features Taking log and ignoring denominator: ( ( )µ log P C D ( ) ) ( )+ ( ) å ( ) log P C X1,X2,...,XD log P XdC d=1
Naïve Bayes Classifier for Categorical Valued Variables
Let’s Naïve Bayes! ( )µ log P C D ( ) ( ) ( )+ ( ) å ( ) log P C X1,X2,...,XD log P XdC d=1 Class Prior Parameters: P Like = Y ( P Like = N ( #EXMPLS COLOR 20 10 10 10 5 5 10 10 20 SHAPE LIKE Square Circle Triangle N Square Circle )=??? )=??? Red Red Red Green Green Green Triangle N Blue Square Blue Circle Blue Triangle Y Y Class Conditional Likelihoods P Color = Red Like = Y ( P Color = Red Like = N ( ... P Shape = Triangle Like = N ( N Y )=???? )=???? N N Y )=????
Naïve Bayes Classifier for Text Classifier
Text Classification Example Doc1 = {buy two shirts get one shirt half off} Doc2 = {get a free watch. send your contact details now} Doc3 = {your flight to chennai is delayed by two hours} Doc4 = {you have three tweets from @sachin} Four Class Problem: P promo doc1 ( P spam doc2 ( P main doc3 ( P social doc4 ( )= 0.84 )= 0.94 )= 0.75 )= 0.91 Spam, Promotions, Social, Main
Bag-of-Words Representation Structured (e.g. Multivariate) data – fixed number of features Unstructured (e.g. Text) data arbitrary length documents, high dimensional feature space (many words in vocabulary), Sparse (small fraction of vocabulary words present in a doc.) Bag-of-Words Representation: Ignore Sequential order of words Represent as a Weighted-Set – Term Frequency of each term
Naïve Bayes Classifier with BoW Make an “independence assumption” about words | class P doc1 promo ( = P buy:1, two:1, shirt :2, get :1, one:1, half :1, off :1 promo ( ) ) = P buy:1 promo ( P get :1 promo ( )P two:1 promo )P one:1 promo ( )P shirt :2 promo )P free:1 promo ( ( ) ( ) = P buy promo ( ( ) 1P one promo ( 1P two promo ( ) ) 1P shirt promo ( 1P free promo ( ) 1 2 ) ) P get promo
Naïve Bayes Text Classifiers Log Likelihood of document given class. ( tf wm ( ( { }m=1 ) M doc = tf wm ) = Number of times word wm occurs in doc )= P w1class ( )P w2class ( ( )...P wMclass ( ( ) ( ) ) ) tf w1 tf w2 tf wM P doc class Parameters in Naïve Bayes Text classifiers: P wmc ( P shirt promo ( Number of parameters = ?? )= Probability that word wm occurs in documents of class c ),P free spam ( ),P buy spam ( ),P buy promo ( ),...
Naïve Bayes Parameters Likelihood of a word given class. For each word, each class. ( )= Probability that word wm occurs in documents of class c P wmc Estimating these parameters from data: ( )= Number of times word wm occurs in documents of class c N wm,c doc1 doc2 ... docm tf free doc ( ) å N free,spam ( )= docÎspam doc1 doc2 doc3 ... ... docn tf free doc ( ) å N free,promo ( )= docÎpromo
Bayesian Classifier Multi-variate real-valued data
Bayes Rule Class Prior Data Likelihood given Class ( ) ( )P Data Class P Data ( )=P Class P Class Data ( ) Data Prior (Marginal) Posterior Probability (Probability of class AFTER seeing the data) ( ) )=P c ( )P x c P x ( ) P c x ( xÎRD
Simple Bayesian Classifier ( ) )=P c ( )P x c P x ( ) P c x ( 1 D 2 Sc exp -1 èç æ ö ø÷ ( )= P x c ( )= N x mc,Sc ( ) -1x-mc ( ) TSc x-mc 1 2 2 ( ) 2p P x c ( )dx xÎRDò xÎRDò xÎRDò =1 Sum: xP x c ( )dx = mc Mean: TP x c ( )dx ( ) x-mc ( ) x-mc = Sc Co-Variance:
CONTACT US www.excelr.com surya@excelr.com +91 9880913504 ExcelR - Data Science, Data Analytics Course Training in Pune Address: 102, 1st Floor, Phase II, Prachi Residency Opposite to Kapil Malhar, Baner Rd, Baner, Pune,Maharashtra 411046 Hour: Mon- Sat 07AM – 11PM Established in Year: 2013 THANK YOU