首页 新闻 聚焦 科技 财经 创业 综合 图片 视频

IT界

旗下栏目: 行业 生态 IT界 创新

COMP9318程序设计代写、Java,CS,Python编程

来源:互联网 发布时间:2021-03-24
COMP9318程序设计代写、Java,CS,Python编程
COMP9318 Lab3
Instructions
This note book contains instructions for COMP9318-Lab3.
You are required to complete your implementation in a file submission.py provided along with this notebook.
 
You are not allowed to print out unnecessary stuff. We will not consider any output printed out on the screen. All results should be returned in appropriate data structures via corresponding functions.
 
You can submit your Implementation for Lab3 via following link: https://kg.cse.unsw.edu.au/submit/ .
 
For each question, we have provided you with detailed instructions along with question headings. In case of any problem, you can post your query @ Ed.
 
You are allowed to add other functions and/or import modules (you may have to in this lab), but you are not allowed to define global variables. Only functions are allowed in submission.py.
 
You should not import unnecessary modules/libraries, failing to import such modules at test time will lead to errors.
 
We will provide immediate feedback on your submission. You can access your scores using the online submission portal on the same day.
 
For Final Evaluation we will be using a different dataset, so your final scores may vary.
 
You are allowed to submit as Many times as you want before the deadline, but ONLY the latest version will be kept and marked.
 
Submission deadline for this assignment is 20:59:59 on 29th March, 2021 (SYDNEY TIME). We will not accept any late submissions.
 
Question-1: Text Classification using Multinomial Naive Bayes
You are required to implement a multinomial Naive bayes classifier to predict spam SMS.
 
The training data is a set of SMS categoried into spam and ham.
 
import pandas as pd
 
raw_data = pd.read_csv('./asset/data.txt', sep='\t')
raw_data.head()
category text
0 ham Go until jurong point, crazy.. Available only ...
1 ham Ok lar... Joking wif u oni...
2 spam Free entry in 2 a wkly comp to win FA Cup fina...
3 ham U dun say so early hor... U c already then say...
4 ham Nah I don't think he goes to usf, he lives aro...
In order to implement a unigram model, first we tokenize the text. We use the count corresponding to each token (word) in the SMS as its feature (i.e., bag of words). We store the features and catrgorical information for each SMS in a dictionary.
 
def tokenize(sms):
    return sms.split(' ')
 
def get_freq_of_tokens(sms):
    tokens = {}
    for token in Tokenize(sms):
        if token not in tokens:
            tokens[token] = 1
        else:
            tokens[token] += 1
    return tokens
 
training_data = []
for index in range(len(raw_data)):
    training_data.append((get_freq_of_tokens(raw_data.iloc[index].text), raw_data.iloc[index].category))
For this lab, you need to implement a multinomial naive bayes classifier (i.e., multinomial_nb() in the file: submission.py) with add-1 smoothing. The input arguments of multinomial_nb() are:
 
training_data: pre-processed data stored as a dictionary
sms: test-sms (i.e., a list of tokens) that you need to categorize as spam and/or ham
The return value of multinomial_nb() should be the ratio of the probability of sms is spam and the probability of sms is ham. A return value larger than 1 implies the sms is spam and vice versa.
 
For example, a sample Output is shown in the cell given below:
 
## How we test your implementation...
import submission_ans as submission
 
sms = 'I am not spam'
print(submission.multinomial_nb(training_data, tokenize(sms)))
0.2342767295597484
Test Environment
For testing, we have pre-installed the requisite modules and/or libraries in the testing environment. You are only allowed to use following libraries:
 
python: 3.6.5
pandas: 0.19.2
NOTE: You are required to implement the Classifier by yourself. You are not allowed to import sklearn and/or any other library in Lab3.
 
 
请加QQ:99515681 或邮箱:99515681@qq.com   WX:codehelp
  免责声明:珠穆朗玛网对于有关本网站的任何内容、信息或广告,不声明或保证其正确性或可靠性,用户自行承担使用网站信息发布内容的真假风险。
责任编辑:珠穆朗玛网