Task: Machine learning problem on Incremental learning: Modify…
Question Answered step-by-step Task: Machine learning problem on Incremental learning: Modify… Task: Machine learning problem on Incremental learning:Modify naive bayes program for incremental learning.Incremental learning is a machine learning paradigm where the learning process takes place whenever new example(s) emerge and adjusts what has been learned according to the new example(s). The most prominent difference of incremental learning from traditional machine learning is that it does not assume the availability of a sufficient training set before the learning process, but the training examples appear over time.Below is the training set for a email spam detection.Image transcription textemail is spam Click Win Prize yes 1 Click meeting setup meeting no 2 Prize free prize yes Click prize free yes… Show moreThe code below shows the implementation for email spam detection using naive bayes from scratch.My issue is that:I want to allow the model to learn continuously new data and give different probabilities each time because the model is implemented using incremental learning too.I need a tutor to modify the given program and use libraries either creme or river so as my naive bayes model supports incremental learning.You can modify the library source code to work as per your need or modify my program itself. You should make sure that my actual model does not use any libraries other than pandas. import pandas as pddata = {’email’: [‘Click Win Prize’, ‘Click meeting setup meeting’,’Prize free prize’,’Click prize free’],’is_spam’: [‘yes’, ‘no’,’yes’,’yes’]}data = pd.DataFrame(data) data[“email”] = data[“email”].apply(lambda str : str.lower()) sum_s = 0sum_ns = 0for i in data[“is_spam”]:if i == “yes”:sum_s += 1else:sum_ns += 1prob_s = sum_s/len(data)prob_ns = sum_ns/len(data) data[“bow”] = data[“email”].str.split().apply(Counter) sum_bow_s = {}sum_bow_ns = {}i=0for bow in data[“bow”]: for key in bow: if data[“is_spam”][i] == “yes”: if key in sum_bow_s: sum_bow_s[key] += bow[key] else: sum_bow_s[key] = bow[key] else: if key in sum_bow_ns: sum_bow_ns[key] +=bow[key] else: sum_bow_ns[key] = bow[key] i += 1term_occurences_s = 0term_occurences_ns = 0for key in sum_bow_s:term_occurences_s += sum_bow_s[key]for key in sum_bow_ns: term_occurences_ns += sum_bow_ns[key] sum_bow = {}term_occurences = 0for bow in data[“bow”]: for key in bow: if key in sum_bow: sum_bow[key] += bow[key] else: sum_bow[key] = bow[key] term_occurences += bow[key]prob_bow = {}for key in sum_bow: prob_bow[key] = sum_bow[key]/term_occurences def split(words): return words.split()def lower(words): return words.lower()def probSpam(email): email = split(lower(email)) likelihood = 1 evidence = 1 for term in email: if term in sum_bow_s: likelihood *= (sum_bow_s[term]+1)/(term_occurences_s+unique_term) else: likelihood *= 1/(term_occurences_s+unique_term) evidence *= prob_bow[term] likelihood_prior = likelihood*prob_s posterior = likelihood_prior/evidence return posteriordef probNotSpam(email): email = split(lower(email)) likelihood = 1 evidence = 1 for term in email: if term in sum_bow_ns: likelihood *= (sum_bow_ns[term]+1)/(term_occurences_ns+unique_term) else: likelihood *= 1/(term_occurences_ns+unique_term) evidence *= prob_bow[term] likelihood_prior = likelihood*prob_ns posterior = likelihood_prior/evidence return posterior def prediction(ham_value,spam_value): if (ham_value>spam_value): return “ham” if(spam_value>ham_value): return “spam” print(probSpam(“Free setup meeting free”))print(probNotSpam(“Free setup meeting free”))print(predict(probNotSpam(“Free setup meeting free”),probSpam(“Free setup meeting free”)) Computer Science Engineering & Technology Python Programming IT 3903 Share QuestionEmailCopy link Comments (0)


