Classifying a single instance (function)
Verfasst: Montag 9. November 2020, 21:08
Hallo liebes Forum,
Ich stehe ganz am Anfang mit meinen Versuchen in Python. Könnte jemand bei einer Aufgaben helfen bitte?
Ich komme leider nicht weiter
Folgende Aufgabe ist zu lösen:
The task include predicting health risk of subjects from personal data and habits.
The patient info is represented as a tuple.
Implement the function decision that takes as input a tuple containing values for attributes (smoker,age,diet), and computes the output of the decision tree. Should return either 'less' or 'more'. No other outputs are valid:
def decision(x: Tuple[str, int, str]) -> str:
"""
This function implements the decision tree represented in the above image. As input the function
receives a tuple with three values that represent some information about a patient.
Args:
x (Tuple[str, int, str]): Input tuple containing exactly three values. The first element represents
a patient is a smoker this value will be 'yes'. All other values represent that
the patient is not a smoker. The second element represents the age of a patient
in years as an integer. The last element represents the diet of a patient.
If a patient has a good diet this string will be 'good'. All other
values represent that the patient has a poor diet.
Returns:
str: A string that has either the value 'more' or 'less'.
No other return value is valid
# Test expected 'more'
x = ("yes", 31, "good")
output = decision(x)
print(f"decision({x}) --> {output}")
t.assertIsInstance(output, str)
t.assertEqual(output, "more")
# Test expected 'less'
x = ("yes", 29, "poor")
output = decision(x)
print(f"decision({x}) --> {output}")
t.assertIsInstance(output, str)
t.assertEqual(output, "less")
Vielen Dank!!
Mit freundlichen Grüßen,
Berta
Ich stehe ganz am Anfang mit meinen Versuchen in Python. Könnte jemand bei einer Aufgaben helfen bitte?
Ich komme leider nicht weiter

The task include predicting health risk of subjects from personal data and habits.
The patient info is represented as a tuple.
Implement the function decision that takes as input a tuple containing values for attributes (smoker,age,diet), and computes the output of the decision tree. Should return either 'less' or 'more'. No other outputs are valid:
def decision(x: Tuple[str, int, str]) -> str:
"""
This function implements the decision tree represented in the above image. As input the function
receives a tuple with three values that represent some information about a patient.
Args:
x (Tuple[str, int, str]): Input tuple containing exactly three values. The first element represents
a patient is a smoker this value will be 'yes'. All other values represent that
the patient is not a smoker. The second element represents the age of a patient
in years as an integer. The last element represents the diet of a patient.
If a patient has a good diet this string will be 'good'. All other
values represent that the patient has a poor diet.
Returns:
str: A string that has either the value 'more' or 'less'.
No other return value is valid
# Test expected 'more'
x = ("yes", 31, "good")
output = decision(x)
print(f"decision({x}) --> {output}")
t.assertIsInstance(output, str)
t.assertEqual(output, "more")
# Test expected 'less'
x = ("yes", 29, "poor")
output = decision(x)
print(f"decision({x}) --> {output}")
t.assertIsInstance(output, str)
t.assertEqual(output, "less")
Vielen Dank!!
Mit freundlichen Grüßen,
Berta