A sample dataset has been provided to you in the '/data/dataset.csv' path. Here are the attributes for the dataset. Use this dataset to test your functions. • Age - ["<=30", "31-40", ">40'] • Income - ["low", "medium", "high"] • Student - ['no", "yes") • Credit Rating - ["fair", "excellent'] • Loan - ["no", "yes'] Note: A sample dataset to test your code has been provided in the location "data/dataset.csv". Please maintain this as it would be necessary • Do not change the variable names of the returned values. • After calculating each of those values, assign them to the corresponding value that is being returned. • The "Loan" attribute should be used as the target variable while making calculations for your naive bayes classifier.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

...............Python ..........

A sample dataset has been provided to you in the /data/dataset.csv' path. Here are the attributes for the dataset. Use this dataset to test your functions.
• Age - ["<=30", "31-40", ">40']
• Income - ["low", "medium", "high"]
• Student - ['no", "yes")
• Credit Rating - ["fair", "excellent")
• Loan - ["no", "yes']
Note:
A sample dataset to test your code has been provided in the location "data/dataset.csv". Please maintain this as it would be necessary
• Do not change the variable names of the returned values.
• After calculating each of those values, assign them to the corresponding value that is being returned.
• The "Loan" attribute should be used as the target variable while making calculations for your naive bayes classifier.
Transcribed Image Text:A sample dataset has been provided to you in the /data/dataset.csv' path. Here are the attributes for the dataset. Use this dataset to test your functions. • Age - ["<=30", "31-40", ">40'] • Income - ["low", "medium", "high"] • Student - ['no", "yes") • Credit Rating - ["fair", "excellent") • Loan - ["no", "yes'] Note: A sample dataset to test your code has been provided in the location "data/dataset.csv". Please maintain this as it would be necessary • Do not change the variable names of the returned values. • After calculating each of those values, assign them to the corresponding value that is being returned. • The "Loan" attribute should be used as the target variable while making calculations for your naive bayes classifier.
from collections import defaultdict
def naive_bayes (dataset_file, attributes, attribute_values):
#3
Input:
1. dataset_file - A string variable which references the path to the dataset file.
2. attributes - A python list which has all the attributes of the dataset
3. attribute_values - A python dictionary representing the values each attribute can hold.
Output: A proabbilities dictionary which contains the counts of when the Loan target variable is yes or no
depending on the input attribute.
Hint: Starter code has been provided to you to calculate the counts. Your code is very similar to the previous prok
probabilities = {
"Age": { "<=30": {"yes": 0, "no": 0}, "31-40": {"yes": 0, "no": 0}, ">40": {"yes": 0, "no": 0} },
"Income": { "low": {"yes": 0, "no": 0}, "medium": {"yes": 0, "no": 0}, "high": {"yes": 0, "no": 0}},
"Student": { "yes": {"yes": 0, "no": 0}, "no": {"yes": 0, "no": o} },
"Credit Rating": { "fair": {"yes": 0, "no": 0}, "excellent": {"yes": 0, "no": 0} },
"Loan": {"yes": 0, "no": 0}
df =
pd.read_csv(dataset_file)
d_range = len(df)
vcount = df[ "Loan"].value_counts ()
vcount_loan_yes = vcount["yes"]
vcount_loan_no = vcount["no"]
probabilities["Loan"][ "yes"] = vcount_loan_yes/d_range
probabilities [ "Loan" ][ "no" = vcount_loan_no/d_range
for attribute in attributes:
value_counts = dict()
vcount = df[attribute].value_counts ()
for att_value in attribute_values[attribute]:
# your code here
return probabilities
Transcribed Image Text:from collections import defaultdict def naive_bayes (dataset_file, attributes, attribute_values): #3 Input: 1. dataset_file - A string variable which references the path to the dataset file. 2. attributes - A python list which has all the attributes of the dataset 3. attribute_values - A python dictionary representing the values each attribute can hold. Output: A proabbilities dictionary which contains the counts of when the Loan target variable is yes or no depending on the input attribute. Hint: Starter code has been provided to you to calculate the counts. Your code is very similar to the previous prok probabilities = { "Age": { "<=30": {"yes": 0, "no": 0}, "31-40": {"yes": 0, "no": 0}, ">40": {"yes": 0, "no": 0} }, "Income": { "low": {"yes": 0, "no": 0}, "medium": {"yes": 0, "no": 0}, "high": {"yes": 0, "no": 0}}, "Student": { "yes": {"yes": 0, "no": 0}, "no": {"yes": 0, "no": o} }, "Credit Rating": { "fair": {"yes": 0, "no": 0}, "excellent": {"yes": 0, "no": 0} }, "Loan": {"yes": 0, "no": 0} df = pd.read_csv(dataset_file) d_range = len(df) vcount = df[ "Loan"].value_counts () vcount_loan_yes = vcount["yes"] vcount_loan_no = vcount["no"] probabilities["Loan"][ "yes"] = vcount_loan_yes/d_range probabilities [ "Loan" ][ "no" = vcount_loan_no/d_range for attribute in attributes: value_counts = dict() vcount = df[attribute].value_counts () for att_value in attribute_values[attribute]: # your code here return probabilities
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY