# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

from algoneer_datasets.bike_sharing import load_dataset
ds = load_dataset()
df = ds.df

from algoneer_datasets.bike_sharing.algorithms import get_algorithm

# we initialize a random forest regressor
algo = get_algorithm('random-forest', n_estimators=100)

model = algo.fit(ds)

# we make sure this model is a regressor
model.algorithm.is_regressor

# ## Partial Dependence Plot
#
# To make a partial dependence plot for a given feature, we implement the following algorithm:
#
# * If the feature is categorical, we generate a list of all possible feature values.
# * If the features is numerical, we generate a list of values spanning the valid range of the feature
# * For each value from the list of values, we calculate the average prediction value (for regression) or the class frequencies (for classification) over all datapoints in the dataset.
# * We plot the resulting value as a function of the feature value.

# +
from algoneer.methods.blackbox.pdp import PDP

pdp = PDP()
# -

result = pdp.run(model, ds, max_values=20, max_datapoints=100, correlated=True)

# +
# %matplotlib inline
import matplotlib.pylab as pl

pl.figure(figsize = (6,4))

d = 'atemp'
c = 'hum'

print(ds.columns)
for i in range(21):
    l = i*21
    r = (i+1)*21
# we plot the influence of the temperature on the bike rentals for different levels of humidity
    pl.plot([x[1]*47-8 for x in result[c][d][l:r]], [x[2] for x in result[c][d][l:r]], marker='o', ls='--')
pl.xlabel("temperature [°C]")
pl.ylabel("rented bicycles")
# -



StarcoderdataJupyterScriptsDedupFiltered