Seite 1 von 1

Classifying a single instance (function)

Verfasst: Montag 9. November 2020, 21:08
von Berta
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 :shock: 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

Re: Classifying a single instance (function)

Verfasst: Montag 9. November 2020, 21:22
von __deets__
Und was hast du schon versucht? Und ist dir vom Prinzip her klar, wie ein Entscheidungsbaum funktioniert?

Re: Classifying a single instance (function)

Verfasst: Montag 9. November 2020, 21:29
von Berta
Hallo, Ja, im Prinzip schon:
Erstmal die Bibliothek kreieren. Und dann folgende Funktionen anwenden:
def classify
If
Else
Wie gesagt aber, ich stehe nur am Anfang und dies sollte eine Probeaufgabe sein.
Danke!
Schöne Grüße

Re: Classifying a single instance (function)

Verfasst: Montag 9. November 2020, 21:29
von Berta
Und ich arbeite mir Jupyter Notebook/ Lab

Re: Classifying a single instance (function)

Verfasst: Montag 9. November 2020, 22:06
von ThomasL
Also ich würde das hiermit lösen: https://scikit-learn.org/stable/modules/tree.html

Re: Classifying a single instance (function)

Verfasst: Montag 9. November 2020, 22:11
von __deets__
@ThomasL: sie muss keinen Baum lernen. Der Baum ist gegeben, und muss einfach in decision hart verdrahtet werden.

@Berta: du hast drei Eigenschaften, also maximal drei Entscheidungen. Was ist die erste Eigenschaft. Welchen Wertebereich hat die, und was passiert bei welchem Wert? Das steht doch in dem Baum den du hast. Wie sieht das aus? Ist dir klar, wie du wenn ich dir einen Vektor gebe, und den Baum, du den von Hand auswerten würdest?

Re: Classifying a single instance (function)

Verfasst: Dienstag 10. November 2020, 07:12
von ThomasL
"This function implements the decision tree represented in the above image. "

Ooops, irgendwie habe ich das Ende des Satzes überlesen. :-)