Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| def classify_habitat(species_list, species_data): | |
| species_to_habitat_weights = species_data.set_index('Species Present').to_dict('index') | |
| # Initialize habitat votes | |
| habitat_votes = {col: 0 for col in species_data.columns if col != 'Species Present'} | |
| # Iterate over each detected species | |
| # Iterate over each detected species | |
| unique_species = set(species_list) | |
| for species in unique_species: | |
| if species in species_to_habitat_weights: | |
| # Increment votes based on species presence and its weights | |
| for habitat in habitat_votes.keys(): | |
| habitat_weight = species_to_habitat_weights[species][habitat] | |
| habitat_votes[habitat] += habitat_weight | |
| # Create a DataFrame for the votes | |
| habitat_votes_df = pd.DataFrame(list(habitat_votes.items()), columns=['Habitat', 'Votes']) | |
| # Determine the habitat with the highest votes | |
| best_habitat = max(habitat_votes, key=habitat_votes.get) | |
| total_votes = sum(habitat_votes.values()) | |
| confidence = habitat_votes[best_habitat] / total_votes if total_votes else 0 | |
| if confidence == 0: | |
| best_habitat = 'No Classification' | |
| # Create a DataFrame for the prediction and confidence | |
| result_df = pd.DataFrame({ | |
| 'Prediction': [best_habitat], | |
| 'Confidence': [confidence] | |
| }) | |
| return result_df, habitat_votes_df | |
| def get_inconclusive(): | |
| result_df = pd.DataFrame({ | |
| 'Prediction': ['Inconclusive'], | |
| 'Confidence': 'N/a'}) | |
| probabilities_df = pd.DataFrame({ | |
| 'Classification': ['Inconclusive'], | |
| 'Confidence': 'N/a'}) | |
| return result_df, probabilities_df |