Cse 5120 March Programming Assignmentthe Printing Partskerstin Voig ✓ Solved
CSE 5120 March Programming Assignment “The printing parts†Kerstin Voigt, March 2021 In the Python interpreter shell: Type an variable + enter: the interpreter will display the value of this variable … this can look messy for values that are large data structures … Using Python 3 print to print out the value does not produce any better looking output messy! still messy… Plain Python print(…) is fine for simple values or simple, moderately sized data structures It you want a data structure to be printed more “neatly alignedâ€, e.g., in some pseudo tabular form, WE WRITE OUR OWN FUNCTIONS Note: the only reason that print_table has a second parameter ‘_’, is because my original print_table was a bit more elaborate and took two arguments; to blend with the existing code, the above print_table has been given a “dummy†second argument; by making it ‘_’, it is anonymous, just a placeholder.
To clean things up, remove ‘_’ and remove second argument from print_table calls in the rule learner code … if you want … def report_rules(rules): … def report_covers_neg(dtab,rule): … see 6.2 annotated slides … Plain Python 3 print of a simple string. Python 3 “formatted†print; printing of a string with parameters; simplest pattern: the ith {} appearing in the string will be replaced with the ith parameter in .format(…). Numerous formatting option exist, to be set within {} . See tutorials for details. Sample Output Behavior of the Rule Learner (1st slide of Module 6.1) Following instructions, your program output should look similar to this: CSE 5120 March Programming Assignment“The printing parts†Slide Number 2 Slide Number 3 Slide Number 4 Sample Output Behavior of the Rule Learner(1st slide of Module 6.1) Python tidbits for CSE 5120 March Programming Assignment Kerstin Voigt, March 2021 Work in Python Shell to test out code snippets Program loaded dtab a copy of list DATATABLE DATATABLE is a list of Python dictionaries Each line a dictionary that lists KEY : VALUE pairs Let’s all each line an INSTANCE K.
Voigt Highlight K. Voigt Highlight K. Voigt Highlight K. Voigt Highlight K. Voigt Highlight K.
Voigt Highlight The third instance of datatable dtab Iterating over all ATTRIBUTES of the instance, Here: just to print Iterating over all VALUES of the instance, Here: just to print Iterating over all ITEMS (key:val) of the instance … K. Voigt Highlight K. Voigt Highlight K. Voigt Highlight K. Voigt Highlight K.
Voigt Highlight K. Voigt Highlight K. Voigt Highlight A Python dictionary is equivalent to a C++ Map data structure; Use [ ] operator on key to obtain value associated with key A RULE is represented by plain LIST of attributes; all attribs are are to have value 1 in an instance in order for rule to “cover†the instance (= rule is applicable to instance) This function definition should go into your .py file …. But for quick prototyping, you CAN define a quick fct within the Pyton shell … benefit of interpreter! K.
Voigt Highlight K. Voigt Highlight K. Voigt Highlight K. Voigt Highlight K. Voigt Highlight Again: put fct def into .py file; in defined in shell just for demo DEC is global; here DEC = ‘ok’ Neither of these 4 rules covers any neg instance, i.e., inst wit ‘ok’ : 0 These 2 rules cover one or more neg instances … find them in listing here … Empty rule [ ] covers all instances be default; thus True K.
Voigt Highlight K. Voigt Highlight K. Voigt Highlight Left to define … def num_covers(dtab, rule): … your code … • Identifies and counts all instances in dtab that are covered by the rule • Special case: if rule is empty, return the length of dtab bec “all†instances are coverd by rule [ ] def num_covers_pos(dtab, rule): … your code … • Identifies and counts all POSITIVE instances (those with ‘ok’:1) that are covered by the rule • Special case: if rule is empty, return the number of all positive instances in dtab; they are all covered by rule [ ] K. Voigt Highlight K. Voigt Highlight K.
Voigt Highlight Python tidbits for CSE 5120 March Programming Assignment Slide Number 2 Slide Number 3 Slide Number 4 Slide Number 5 Left to define … CSE 5120 in March of 2021 wit Dr. Voigt Programming Assignment Total Points: 25 This is a programming assignment that is aligned with the lecture Modules 6.1 and 6.2 on Inductive Rule Learning. In particular, this assignment wants you to complete the Inductive Rule Learning program in the Module 6.2 video. The driving function ‘learn_rules()’ and several functions it calls are already provided. You will see calls to other functions without their implementations shown in the Module 6.2 video.
To find these, consult the annotation is the Module 6.2 slides that are given along with the video. A bit more code is given in the annotations with just few functions left for you to implement on your own. You will see which … STEP 1: The top-level function ‘learn_rules()’ assumes that a learning problem has been provided in the form of 3 global variables (every once in a while – here for the sake of convenience to us, the programmers – using globals is fine …). The globals variables that the program assumes are already provided with their initializations: DATATABLE (the set of training instances), ATTRIBS (the list of attributes that describe the concept to be learned), and DEC (the attribute that indicates the binary classification associated with each training instance.).
In order to save some typing, you may download file cse5120_decrule_learn.py with the globals for the lecture training set. Continue your program by adding on to this file. STEP 2: Start programming based on the code provided in the video and on the slides. Whenever you come across one of those functions that are to you to program on your own, step back, make sure that you understand the functions purpose, decide on how to accomplish it (none of it is very complicated), then code. Make the last lines of your program the following: STEP 3: Attempt to run your program, debug as needed.
Your program should reproduce the rule learning outcomes you have seen in the video. STEP 4: Rerun your program for another set of training instances. In order to have your program load another set of training instances, simply comment out the block of the original DATATABLE, ATTRIBS and DEC lines and in their place cut-and-paste the content of file cse5120_great_pizza.py You will find out, on the basis of 42 instances of rated pizzas (choices: red/non-red sauce, meat/no-meat, seafood/no-seafood/, extra-crust/no-extra-crust, ‘anchovies/no-anchovies’), which pizzas are the best ïŠ Python 2 to Python 3: It turns out that part of this assignment will be your converting the lecture code, which is written in Python 2, to Python 3 (which, to my knowledge, Dr.
Khan has you use). The conversion is not difficult and just a few statement will need your attention: 1. Convert Python 2 print statements into Python 3 statements; since you have no doubt already been using Python 3 print statements, just by looking at the Python 2 code, you may already know what to do. If not, there are many online sites on this that you can Google. If this becomes a “huge†problem, ask by email either me, our student assistant Benjamin Alexander for pointers.
2. Python 2 ‘raw_input’ becomes Python 3 ‘input’. 3. The Python 2 ‘map’ statement is no longer supported in the same manner by Python 3; but you can still use it by wrapping a list(…) around the entire ‘map(…)’ CREDIT FOR THIS ASSIGNMENT: Submit via Blackboard Submission Portal under the “CSE 5120 in March’ content area (1) your file decrule_learn.py; make sure that add your name and student id to the top of the file; (2) A screenshot of your running program and output from STEP 3; the screenshot need only show the bottom portion of the output which shows the learned rules, (3) a screenshot showing the rules that your program learned for the run in STEP 4. Submit partially programs (even those that may not work) for partial credit. The assignment is due on Tuesday, 3/16, at 11:59pm.
Paper for above instructions
CSE 5120 March Programming Assignment: Implementing Rule Learning in Python
The CSE 5120 programming assignment titled "The Printing Parts" focuses on implementing a rule learner algorithm in Python. This assignment is aligned with the inductive rule learning methodology where the goal is to learn rules from a dataset that can subsequently be used for classification. In this document, we will outline the steps required to complete the assignment, discuss the implementation details, and provide code examples.
Overview of the Assignment
The assignment requires writing functions that operate on a dataset composed of instances represented as dictionaries. Specifically, the functions to be implemented are:
1. `num_covers(dtab, rule)`: Counts all instances in `dtab` that are covered by the `rule`.
2. `num_covers_pos(dtab, rule)`: Counts all positive instances covered by the `rule`.
3. `print_table(data)`: Prints the data neatly in a tabular format.
These functions will leverage Python's capabilities to handle lists and dictionaries efficiently.
Step-by-Step Approach
Step 1: Understand the Global Variables
To facilitate the learning process, we need to work with three global variables:
- `DATATABLE`: A list of dictionaries where each dictionary represents an instance.
- `ATTRIBS`: A list containing the attributes that describe the instances.
- `DEC`: The attribute indicating the binary classification associated with each instance.
The provided sample `DATATABLE` may look like this:
```python
DATATABLE = [
{'attr1': 1, 'attr2': 0, 'ok': 1},
{'attr1': 0, 'attr2': 1, 'ok': 0},
{'attr1': 1, 'attr2': 1, 'ok': 1},
]
ATTRIBS = ['attr1', 'attr2']
DEC = 'ok'
```
Step 2: Implementing the `num_covers` and `num_covers_pos` Functions
The following functions need to be implemented to count the instances covered by the specified rules:
```python
def num_covers(dtab, rule):
if not rule: # If the rule is empty
return len(dtab) # It covers all instances
count = 0
for instance in dtab:
if all(instance[attr] == 1 for attr in rule):
count += 1
return count
def num_covers_pos(dtab, rule):
if not rule: # If the rule is empty
return sum(1 for instance in dtab if instance[DEC] == 1) # Count positive instances
count = 0
for instance in dtab:
if all(instance[attr] == 1 for attr in rule) and instance[DEC] == 1:
count += 1
return count
```
The implementation involves iterating through each instance in the dataset and checking if the rule conditions are satisfied. If an instance meets all conditions specified by the rule (i.e., all attributes in the rule are set to 1), we increment the count accordingly.
Step 3: Implementing the `print_table` Function
The `print_table` function is crucial for output formatting, which enhances readability, especially with larger datasets. Here is a sample implementation:
```python
def print_table(data):
headers = data[0].keys()
print("{:<15}" len(headers).format(headers)) # Print headers in format
print("-" 15 len(headers)) # Separate line
for entry in data:
print("{:<15}" len(entry).format(entry.values())) # Print each entry in format
```
Step 4: Integrating with the Learning Algorithm
Finally, the above implementations will be integrated into the overall rule learning function `learn_rules()`. Here’s a basic skeleton of how it could be structured:
```python
def learn_rules():
rules = [['attr1'], ['attr2']]
for rule in rules:
total_covers = num_covers(DATATABLE, rule)
positive_covers = num_covers_pos(DATATABLE, rule)
print(f"Rule: {rule} covers: {total_covers}, Positive covers: {positive_covers}")
```
To run the program effectively, ensure the `learn_rules()` function is called, and the dataset is accurately defined with instances before the function execution.
Testing the Implementation
After implementation, it is vital to test the functions with various datasets to ensure they work correctly and yield expected results. Utilize test cases to determine if the functions accurately count instances and print outputs as formatted.
Conclusion
The CSE 5120 programming assignment "The Printing Parts" challenges students to apply their understanding of inductive rule learning through the implementation of functions in Python. Following the steps outlined above, we implemented core functionalities for counting and displaying covered instances, enriched by clear and formatted output. Engaging with this assignment not only reinforces programming skills but also deepens comprehension of inductive learning principles.
References
1. Voigt, K. (2021). Assignment Overview: CSE 5120 March Programming Assignment.
2. Lutz, M. (2013). Programming Python (4th ed.). O'Reilly Media.
3. McKinney, W. (2018). Python for Data Analysis (2nd ed.). O'Reilly Media.
4. Beazley, D. (2013). Python Cookbook (3rd ed.). O'Reilly Media.
5. Sweigart, A. (2015). Automate the Boring Stuff with Python. No Starch Press.
6. W3Schools. (n.d.). Python Lists - W3Schools. W3Schools.
7. GeeksforGeeks. (2021). Python Dictionary. GeeksforGeeks.
8. Python Software Foundation. (2023). Python Documentation. Python.org.
9. Dummies. (2021). Python All-in-One For Dummies. John Wiley & Sons.
10. Zelle, J. (2010). Python Programming. Franklin, Beedle & Associates Inc.