Views
No views yet
random_state=42, class_weight="balanced".| Metric | Value |
|---|---|
| Testing Accuracy (CV 5-fold) | 91.0% |
| Testing Weighted Average Precision | 100% |
| Testing Weighted Average Recall | 100% |
| Testing Weighted Average F1 | 100% |
1feature_options = {
2 'cap-shape': {'b': 'bell', 'c': 'conical', 'x': 'convex', 'f': 'flat', 'k': 'knobbed', 's': 'sunken'},
3 'cap-surface': {'f': 'fibrous', 'g': 'grooves', 'y': 'scaly', 's': 'smooth'},
4 'cap-color': {'n': 'brown', 'b': 'buff', 'c': 'cinnamon', 'g': 'gray', 'r': 'green', 'p': 'pink', 'u': 'purple', 'e': 'red', 'w': 'white', 'y': 'yellow'},
5 'bruises': {'t': 'bruises', 'f': 'no'},
6 'odor': {'a': 'almond', 'l': 'anise', 'c': 'creosote', 'y': 'fishy', 'f': 'foul', 'm': 'musty', 'n': 'none', 'p': 'pungent', 's': 'spicy'},
7 'gill-attachment': {'a': 'attached', 'd': 'descending', 'f': 'free', 'n': 'notched'},
8 'gill-spacing': {'c': 'close', 'w': 'crowded', 'd': 'distant'},
9 'gill-size': {'b': 'broad', 'n': 'narrow'},
10 'gill-color': {'k': 'black', 'n': 'brown', 'b': 'buff', 'h': 'chocolate', 'g': 'gray', 'r': 'green', 'o': 'orange', 'p': 'pink', 'u': 'purple', 'e': 'red', 'w': 'white', 'y': 'yellow'},
11 'stalk-shape': {'e': 'enlarging', 't': 'tapering'},
12 'stalk-root': {'b': 'bulbous', 'c': 'club', 'u': 'cup', 'e': 'equal', 'z': 'rhizomorphs', 'r': 'rooted', '?': 'missing'},
13 'stalk-surface-above-ring': {'f': 'fibrous', 'y': 'scaly', 'k': 'silky', 's': 'smooth'},
14 'stalk-surface-below-ring': {'f': 'fibrous', 'y': 'scaly', 'k': 'silky', 's': 'smooth'},
15 'stalk-color-above-ring': {'n': 'brown', 'b': 'buff', 'c': 'cinnamon', 'g': 'gray', 'o': 'orange', 'p': 'pink', 'e': 'red', 'w': 'white', 'y': 'yellow'},
16 'stalk-color-below-ring': {'n': 'brown', 'b': 'buff', 'c': 'cinnamon', 'g': 'gray', 'o': 'orange', 'p': 'pink', 'e': 'red', 'w': 'white', 'y': 'yellow'},
17 'veil-type': {'p': 'partial', 'u': 'universal'},
18 'veil-color': {'n': 'brown', 'o': 'orange', 'w': 'white', 'y': 'yellow'},
19 'ring-number': {'n': 'none', 'o': 'one', 't': 'two'},
20 'ring-type': {'c': 'cobwebby', 'e': 'evanescent', 'f': 'flaring', 'l': 'large', 'n': 'none', 'p': 'pendant', 's': 'sheathing', 'z': 'zone'},
21 'spore-print-color': {'k': 'black', 'n': 'brown', 'b': 'buff', 'h': 'chocolate', 'r': 'green', 'o': 'orange', 'u': 'purple', 'w': 'white', 'y': 'yellow'},
22 'population': {'a': 'abundant', 'c': 'clustered', 'n': 'numerous', 's': 'scattered', 'v': 'several', 'y': 'solitary'},
23 'habitat': {'g': 'grasses', 'l': 'leaves', 'm': 'meadows', 'p': 'paths', 'u': 'urban', 'w': 'waste', 'd': 'woods'}
24}
25
26def get_user_input():
27 """
28 Collects user input for each mushroom feature.
29
30 Returns:
31 dict: A dictionary containing the user's input for each feature.
32 """
33 user_input = {}
34 print("Please provide the following mushroom characteristics:")
35 for feature, options in feature_options.items():
36 print(f"\n{feature.replace('-', ' ').capitalize()}:")
37 for key, value in options.items():
38 print(f" {key}: {value}")
39 while True:
40 choice = input(f"Enter the corresponding letter for {feature}: ").strip().lower()
41 if choice in options:
42 user_input[feature] = choice
43 break
44 else:
45 print("Invalid input. Please enter one of the listed letters.")
46 return user_input
47
48user_input = get_user_input()
49
50def predict_mushroom(features):
51 """
52 Predict whether a mushroom is edible or poisonous based on its features.
53
54 Parameters:
55 features (dict): A dictionary of mushroom features with feature names as keys and corresponding categorical values.
56
57 Returns:
58 str: 'Edible' or 'Poisonous'
59 """
60 # Load the trained model and mappings
61 model = joblib.load('mushroom_classifier.pkl')
62 mappings = joblib.load('mappings.pkl')
63
64 # Initialize a dictionary to hold the numerical features
65 numerical_features = {}
66
67 # Map each feature to its numerical value
68 for feature, value in features.items():
69 if feature in mappings:
70 if value in mappings[feature]:
71 numerical_features[feature] = mappings[feature][value]
72 else:
73 raise ValueError(f"Invalid value '{value}' for feature '{feature}'.")
74 else:
75 raise ValueError(f"Feature '{feature}' is not recognized.")
76
77 # Convert the numerical features into a DataFrame
78 input_df = pd.DataFrame([numerical_features])
79
80 # Predict using the trained model
81 prediction = model.predict(input_df)
82
83 # Interpret the prediction
84 if prediction[0] == 0:
85 return 'Edible'
86 else:
87 return 'Poisonous'
88
89# Predict edibility
90try:
91 result = predict_mushroom(user_input)
92 print(f"\nThe mushroom is likely: {result}")
93except ValueError as e:
94 print(f"Error: {e}")