Which plants are better for bees: native or non-native?
📖 Background
You work for the local government environment agency and have taken on a project about creating pollinator bee-friendly spaces. You can use both native and non-native plants to create these spaces and therefore need to ensure that you use the correct plants to optimize the environment for these bees.
The team has collected data on native and non-native plants and their effects on pollinator bees. Your task will be to analyze this data and provide recommendations on which plants create an optimized environment for pollinator bees.
💾 The Data
You have assembled information on the plants and bees research in a file called plants_and_bees.csv
. Each row represents a sample that was taken from a patch of land where the plant species were being studied.
Column | Description |
---|---|
sample_id | The ID number of the sample taken. |
species_num | The number of different bee species in the sample. |
date | Date the sample was taken. |
season | Season during sample collection ("early.season" or "late.season"). |
site | Name of collection site. |
native_or_non | Whether the sample was from a native or non-native plant. |
sampling | The sampling method. |
plant_species | The name of the plant species the sample was taken from. None indicates the sample was taken from the air. |
time | The time the sample was taken. |
bee_species | The bee species in the sample. |
sex | The gender of the bee species. |
specialized_on | The plant genus the bee species preferred. |
parasitic | Whether or not the bee is parasitic (0:no, 1:yes). |
nesting | The bees nesting method. |
status | The status of the bee species. |
nonnative_bee | Whether the bee species is native or not (0:no, 1:yes). |
Source (data has been modified)
💪 Challenge
Provide your agency with a report that covers the following:
- Which plants are preferred by native vs non-native bee species?
- A visualization of the distribution of bee and plant species across one of the samples.
- Select the top three plant species you would recommend to the agency to support native bees.
🧑⚖️ Judging criteria
This is a community-based competition. The top 5 most upvoted entries will win.
The winners will receive DataCamp merchandise.
✅ Checklist before publishing
- Rename your workspace to make it descriptive of your work. N.B. you should leave the notebook name as notebook.ipynb.
- Remove redundant cells like the judging criteria, so the workbook is focused on your work.
- Check that all the cells run without error.
⌛️ Time is ticking. Good luck!
import pandas as pd
data = pd.read_csv("data/plants_and_bees.csv")
data
Importing modules
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.impute import SimpleImputer
from sklearn.impute import KNNImputer
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.dummy import DummyClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
import xgboost as bst
Looking into the data types
This will allow me to see if the data is in the correct format.
data.info()
Changing the Dtypes
I find this is useful before looking at any of the decriptive information do to the way numeric and alpha-type data are portrayed. This will also allow me to interrogate missing values.
sampleid
will become the indesspecies
num is fine as an intdate
to become date timeseason
only has 2 so will be one hot encodedsite
has 3 options, A, B, C so can be encodednative_or_non
is binarysampling
is binary againplant_species
has 24 options - Targettime
list of ints that can be retained#bee_species
93 different species - try one hot encodingsex
binaryspecialized_on
- mostly nan, likely dropparasitic
- binarynesting
- 6 types for encodingstatus
- only 15, likely dropnonnative_bee
= binary imbalanced
# instantiating imputers for missing values
s_imp_para = SimpleImputer() # parasite
k_imp_para = KNNImputer() # parasite
s_imp_nest = SimpleImputer() # nesting
k_imp_nest = KNNImputer() # nesting
s_imp_nnat = SimpleImputer() # nonnative
k_imp_nnat = KNNImputer() # nonnative
# simple imputations
data['parasitic'] = s_imp_para.fit_transform(data['parasitic'].values.reshape(-1,1))
data['nesting'] = data['nesting'].fillna('ground')
data['nonnative_bee'] = s_imp_nnat.fit_transform(data['nonnative_bee'].values.reshape(-1,1))
# converting column types
data_drop = data.drop(columns=['status', 'specialized_on'], axis=1).copy()
data_drop['date'] = pd.to_datetime(data_drop['date']) # date to date_time
data_drop['year'] = data_drop['date'].dt.year
data_drop['month'] = data_drop['date'].dt.month
data_drop['day'] = data_drop['date'].dt.day
data_drop['hour'] = data_drop['date'].dt.hour
data_drop['day_of_week'] = data_drop['date'].dt.dayofweek
data_drop['is_weekend'] = data_drop['date'].dt.weekday >= 5
data_drop = data_drop.drop(columns=['date'], axis=1)
data_drop['plant_species'] = data_drop['plant_species'].str.replace('None', 'air')
data_drop['plant_species'] = pd.factorize(data_drop['plant_species'])[0]
columns_to_encode = ['native_or_non', 'season', 'site', 'sampling', 'bee_species', 'sex', 'parasitic', 'nesting', 'nonnative_bee']
data_encoded = pd.get_dummies(data_drop, columns=columns_to_encode, drop_first=True)
df_shape = data_encoded.shape
data_encoded.set_index('sample_id')
# df_shape = data_drop.shape
print(f"There are {df_shape[0]} rows and {df_shape[1]} columns")