2A-python代写
时间:2023-08-25
Module 2A: Assignment
Weightage: 30 Marks
Assessment task:
You are recent hire that works at a start-up AI image company. Your new job requires several tasks to support the different teams of the company. Your first task is implemented an AI knowledge-based
system to help customers with some of the terminology of AI. Some of the terminology is simple, i.e. names and years of famous AI events, but other terminology needs to be more detailed. In addition,
the new business has been receiving malware, so your second task is write a filter that can detect the malware from genuine software. Lastly, many customers of the company provide images that
require further post processing, your final task is to write an algorithm that can find portion of pixels within a single colour space. For example, it may be useful to identify one colour of pixels versus
another, as in black and white photographs.
If successful, the company will put your applications in productions for customers. You will therefore need to provide detailed comments in the code explaining the steps you have taken to design your
solution. As deliverables, you will complete the following three (3) parts:
Parts
1. Programming Part 1: Write a programme that accesses a AI knowledge-base to search for facts based on the user's input.
2. Programming Part 2: Write a programme that specifies the level of confidence in malware detection.
3. Programming Part 3: Write a programme that implements the flood fill algorithm, identifys and changes the marked pixels.
Instructions:
1. Complete all three parts of the assignments by write the Python codes.
2. Run your codes to ensure that the required outputs are delivered.
3. Submit the assignment for grading and to get feedback.
Programming Part 1: Create an AI History/Definition knowledge-based system
Assessment task:
Write a programme that accesses a AI knowledge-base to search for facts based on the user's input. There are 4 steps involved, you will complete the code for step 1 and 4 only.
Marks:
This part is worth 10 Marks. This part is divided by three assessments (3 marks, 4 marks, 3 marks).
Instructions:
1. Write your Python code in place of "your code here" placeholder below.
2. Run your code by clicking on 'run' cell in the toolbar before you submit.
3. You will get the feedback once you submit the assignment.
Submission:
Click on the submit button on the top right after you run the code.
Step 1
Read through the AI text book for this course and gather facts for the AI knowledge-based system.
1. Write at least five about either the history or provide definition in regards to Artifical Intelligence.
2. At least one fact needs to integer number, at least one fact needs to string and at least one fact needs to a list.
3. One fact needs to be defind as 'turing-test-proposed-year' i.e. in what year was the turing test proposed (Check module 1).
Step 2
Applications runs only while the running variable is 'true' which is written below.
Step3
The below code delivers a friendly message to the user by printing 'Hi, I'm an AI History/Definition Knowledge based system'.
Hi, I'm an AI History/Definition Knowledge based system
Step 4
Write the code to a. Ask the user to enter his/her facts that may or may not be in the facts (the system should be able to handle inquiries that are not in the facts database). b. After getting the user-
input, write the code to match the facts with the knowledge base that was defined above for three step 1. c. Inform the user about the identifed fact and the report back the findings.
What would you like to know?
definition of ai
Artificial Intelligence is defined as the ability of a digit computer or robot to perform tasks that the intelligent beings conduct. It is used to automating manual and
repetitive tasks
---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
/var/folders/zl/qk8rctjx0x107swqndlw35_h0000gn/T/ipykernel_75389/582220439.py in
8 ###
9 while running:
---> 10 question_asked = str(input('What would you like to know? \n')).lower()
11
12 if 'definition' in question_asked \
~/opt/anaconda3/lib/python3.9/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
1175 "raw_input was called, but this frontend does not support input requests."
1176 )
-> 1177 return self._input_request(
1178 str(prompt),
1179 self._parent_ident["shell"],
~/opt/anaconda3/lib/python3.9/site-packages/ipykernel/kernelbase.py in _input_request(self, prompt, ident, parent, password)
1217 except KeyboardInterrupt:
1218 # re-raise KeyboardInterrupt, to truncate traceback
-> 1219 raise KeyboardInterrupt("Interrupted by user") from None
1220 except Exception:
1221 self.log.warning("Invalid Message:", exc_info=True)
KeyboardInterrupt: Interrupted by user
Programming Part 2: Malware Detection Bayesian Network
Write a programme that specifies the level of confidence in malware detection.
Assessment task:
In this Part 2, you'll write the code that specifies the level of confidence in malware detection. The programme consists of many steps that are partially written.
Marks:
This part is of 10 Marks. This part is divided by fives assessments (2 marks each).
Instructions:
1. Write your Python code in place of "your code here" placeholder below.
2. Run your code by clicking on 'run' cell in the toolbar before you submit.
3. You will get the feedback once you submit the assignment.
Submission:
Click on the submit button on the top right after you run the code.
Step 1
Import the pomegranate library.
Step 2
Define the probabilities for code.
Define the probabilities for resize.
Define the probabilities for signature
Define the probabilities for outcome
Step 3
Create the Bayesian Network.
Add the states to the network
Add the edges
Train the model
Step 4
Define probabilities with values that need to overwritten by the model
Calculate a probability that is less that 0.01 for a given observation (assign to low_probability)
0.005700000000000001
True
Calculate a probability that is less than 0.1 but greater than 0.01 for a given observation (assign to medium_probability)
0.020999999999999994
True
Calculate a probability that is greater than 0.1 for a given observation (assign to high_probability)
0.12600000000000003
True
Calculate predictions based on the evidence that is less than 0.5 and assigned it to low_probability_prediction.
0.19750000000000015
True
Calculate predictions based on the evidence that is greater than 0.8 and assigned it to high_probability_prediction.
0.8049999999999998
True
Part 3
Write a programme that implements the flood fill algorithm, identifys and changes the marked pixels. Furthermore, blank pixels are ignored.
Assessment task:
In this part, you'll write the code that implements the flood fill algorithm, identifys and changes the marked pixels. Furthermore, blank pixels are ignored. To do this, you'll use any breadth first search
algorithm. The code is partially written below.
Marks:
This part is of 10 Marks. This part is divided by two assessments (5 marks each).
Instructions:
1. Write your Python code in place of "your code here" placeholder below.
2. Run your code by clicking on 'run' cell in the toolbar before you submit.
3. You will get the feedback once you submit the assignment.
Submission:
Click on the submit button on the top right after you run the code.
Step 1: Create a Node
The datapoints within the graph
Do not change the structure of the Node class
Step 2: Create a Start function
Acts as a starting point for the flood fill algorithm
Step 3: Implement the flood fill algorithm
Write the flood fill algorithm using breadth first search
Step 4: Call the start function
By calling the start function, the function retuns the graph and its properties.
This is useful to debug your algorithm.
Important Note: The output of the image is not assessed, but rather the search algorithm is assessed. No marks will be awarded, if the algorithm is only changing the pixel values from 1 to 2 without
doing the flood fill correctly.
[[1, 0, 0, 0, 0], [1, 1, 0, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 1], [0, 1, 1, 0, 1]]
In [1]: # What you need to do
# 1. Write at least five about either the history or provide definition in regards to Artifical Intelligence
# 2. At least one fact needs to integer number, at least one fact needs to string and at least one fact needs to a list
# 3. One fact needs to be defind as 'turing-test-proposed-year' i.e. in what year was the turing test proposed
# (Check module 1)
facts = {
'definition': 'the ability of a digit computer or robot to perform tasks that the intelligent beings conduct. It is used to automating manual and repetitive tasks',
'turing-test-proposed-year': 1950,
'ai-areas': ['NLP', 'Knowledge Representation', 'Automated Reasoning', 'Machine Learning', 'Computer Vision',
'Speech Recognition', 'Robotic'],
'industries-affected': 'Transportation, Manufacturing, Healthcare, Education, Media, Customer Service, Science, Chemistry, Environment, etc. ',
'four-definitions': 'Thinking, Acting, Humanly, Rationality',
'thinking': 'thought processes and reasoning',
'acting': 'behavior',
'humanly': 'how well the AI performs comparing to a human in the same situation',
'rationality': 'how well the AI thinks or acts against an ideal measure'
###
### YOUR CODE HERE
###
}
In [2]: running = True
In [3]: print("Hi, I'm an AI History/Definition Knowledge based system\n")
In [4]: # What you need to do
#1. Match the key terms entered by the user with the knowledge base created above.
#2. Write your Python code below.
#3. Run your code to ensure it is working properly.
###
### YOUR CODE HERE
###
while running:
question_asked = str(input('What would you like to know? \n')).lower()

if 'definition' in question_asked \
and 'ai' in question_asked \
or 'artificial intelligence' in question_asked:
print("Artificial Intelligence is defined as " + facts['definition'])
elif 'turing' in question_asked \
and 'year' in question_asked:
print("The turing test was proposed in " + str(facts['turing-test-proposed-year']))
elif 'area' in question_asked:
print("The areas of AI include: ", end='\n\n')
for lst_of_area in facts['ai-areas']:
print(lst_of_area)
elif 'industry' in question_asked \
or 'industries' in question_asked \
or 'application' in question_asked:
print("AI is being applied in the fields of " + facts['industries-affected'])
elif 'four' in question_asked \
and 'definition' in question_asked:
print("Four definitions of AI include " + facts['four-definitions'] + ', in which:', end='\n')
for definition in ['thinking', 'acting', 'humanly', 'rationality']:
print(f"{definition} represents " + facts[definition], end=',\n')
else:
print("I have no knowledge or I'm not sure what you mean.")
In [ ]: ###
### AUTOGRADER TEST - DO NOT REMOVE
###
In [ ]: ###
### AUTOGRADER TEST - DO NOT REMOVE
###
In [ ]: ###
### AUTOGRADER TEST - DO NOT REMOVE
###
In [5]: from pomegranate import *
In [21]: code = Node(DiscreteDistribution({
"plain-text": 0.05,
"binary": 0.7,
"obfuscated": 0.25
}), name="code")
In [22]: resize = Node(ConditionalProbabilityTable([
["plain-text", "no", 0.95],
["plain-text", "yes", 0.05],
["binary", "no", 0.75],
["binary", "yes", 0.25],
["obfuscated", "no", 0.4],
["obfuscated", "yes", 0.6]
], [code.distribution]), name="resize")
In [23]: signature = Node(ConditionalProbabilityTable([
["plain-text", "no", "present", 0.8],
["plain-text", "no", "absent", 0.2],
["plain-text", "yes", "present", 0.9],
["plain-text", "yes", "absent", 0.1],
["binary", "no", "present", 0.6],
["binary", "no", "absent", 0.4],
["binary", "yes", "present", 0.8],
["binary", "yes", "absent", 0.2],
["obfuscated", "no", "present", 0.6],
["obfuscated", "no", "absent", 0.4],
["obfuscated", "yes", "present", 0.95],
["obfuscated", "yes", "absent", 0.05],
], [code.distribution, resize.distribution]), name="signature")
In [24]: outcome = Node(ConditionalProbabilityTable([
["present", "malware", 0.9],
["present", "software", 0.1],
["absent", "malware", 0.4],
["absent", "software", 0.6],
], [signature.distribution]), name="outcome")
In [25]: model = BayesianNetwork()
In [26]: ###
### YOUR CODE HERE
###
model.add_state(code)
model.add_state(resize)
model.add_state(signature)
model.add_state(outcome)
In [27]: ###
### YOUR CODE HERE
###
model.add_edge(code, resize)
model.add_edge(code, signature)
model.add_edge(resize, signature)
model.add_edge(signature, outcome)
In [28]: ###
### YOUR CODE HERE
###
model.bake()
In [29]: low_probability = 1
medium_probability = 1
high_probability = 0
low_probability_prediction = 1
high_probability_prediction = 0
In [30]: ###
### YOUR CODE HERE
###
low_probability = model.probability([["plain-text", "no", "absent", "software"]])
print(low_probability)
print(low_probability < 0.01)
In [31]: ###
### YOUR CODE HERE
###
medium_probability = model.probability([["binary", "yes", "absent", "software"]])
print(medium_probability)
print(medium_probability < 0.1 and medium_probability > 0.01)
In [32]: ###
### YOUR CODE HERE
###
high_probability = model.probability([["binary", "no", "absent", "software"]])
print(high_probability)
print(high_probability > 0.1)
In [36]: ###
### YOUR CODE HERE
###
predictions = model.predict_proba({
"code": "plain-text"
})
low_probability_prediction = predictions[3].parameters[0]['software']
print(low_probability_prediction)
print(low_probability_prediction < 0.5)
In [40]: ###
### YOUR CODE HERE
###
predictions = model.predict_proba({
"code": "obfuscated"
})
high_probability_prediction = predictions[3].parameters[0]['malware']
print(high_probability_prediction)
print(high_probability_prediction > 0.8)
In [ ]: ###
### AUTOGRADER TEST - DO NOT REMOVE
###
In [ ]: ###
### AUTOGRADER TEST - DO NOT REMOVE
###
In [ ]: ###
### AUTOGRADER TEST - DO NOT REMOVE
###
In [ ]: ###
### AUTOGRADER TEST - DO NOT REMOVE
###
In [ ]: ###
### AUTOGRADER TEST - DO NOT REMOVE
###
In [41]: class Node:
instance_count = 0
def __init__(self, x, y):
self.x = x
self.y = y
self.neighbours = list()
Node.instance_count += 1
In [42]: def start():
# Sample image represented as 2D array of numbers
# 0 are obstacles
# 1 are free or empty spaces
# A complete valid image is when the 1s have been converted to 2s once the graph.floodfill algorithm is complete.
image = [
[1,0,0,0,0],
[1,1,0,1,0],
[0,1,1,1,0],
[0,1,1,1,1],
[0,1,1,0,1]
]
graph = Graph()
print(graph.floodFill(image, 2, 2, 2))
# DO NOT REMOVE THIS RETURN STATEMENT!
return graph
In [43]: class Graph:
explored = set()
def floodFill(self, image, x, y, pixel):
start = Node(x, y)
queue = list()
queue.append(start)
# Remember to add unique x and y coordinates of newly discovered nodes to the explored set
# Be mindful of the ordering of the search
###
### YOUR CODE HERE
###
# Return the modified image represented as 2D array of numbers
return image
def neighbours(self, image, x, y, currentNode):
U = y - 1
D = y + 1
L = x - 1
R = x + 1
# Write the neighbours function to find the neighbours in four directions for a given pixel.
# An edge is valid if the pixel is newly discovered, i.e. an edge is created when the neighbour's pixel value is one.
# Append a valid new Node to the neighbours of the currentNode
# Remember to do boundary checking
###
### YOUR CODE HERE
###
# Return the current node's (the pixel in question) neighbours, not always a maximum of four.
return currentNode.neighbours
In [44]: graph = start()
In [ ]: ###
### AUTOGRADER TEST - DO NOT REMOVE
###
In [ ]: ###
### AUTOGRADER TEST - DO NOT REMOVE
###
In [ ]:

essay、essay代写