Classifying a single instance (function)

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Berta
User
Beiträge: 3
Registriert: Montag 9. November 2020, 20:46

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
__deets__
User
Beiträge: 14545
Registriert: Mittwoch 14. Oktober 2015, 14:29

Und was hast du schon versucht? Und ist dir vom Prinzip her klar, wie ein Entscheidungsbaum funktioniert?
Berta
User
Beiträge: 3
Registriert: Montag 9. November 2020, 20:46

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
Berta
User
Beiträge: 3
Registriert: Montag 9. November 2020, 20:46

Und ich arbeite mir Jupyter Notebook/ Lab
Benutzeravatar
ThomasL
User
Beiträge: 1367
Registriert: Montag 14. Mai 2018, 14:44
Wohnort: Kreis Unna NRW

Also ich würde das hiermit lösen: https://scikit-learn.org/stable/modules/tree.html
Ich bin Pazifist und greife niemanden an, auch nicht mit Worten.
Für alle meine Code Beispiele gilt: "There is always a better way."
https://projecteuler.net/profile/Brotherluii.png
__deets__
User
Beiträge: 14545
Registriert: Mittwoch 14. Oktober 2015, 14:29

@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?
Benutzeravatar
ThomasL
User
Beiträge: 1367
Registriert: Montag 14. Mai 2018, 14:44
Wohnort: Kreis Unna NRW

"This function implements the decision tree represented in the above image. "

Ooops, irgendwie habe ich das Ende des Satzes überlesen. :-)
Ich bin Pazifist und greife niemanden an, auch nicht mit Worten.
Für alle meine Code Beispiele gilt: "There is always a better way."
https://projecteuler.net/profile/Brotherluii.png
Antworten