# ---
# 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
# ---

#import the libraries
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.ensemble import  RandomForestClassifier

# +
#read the data
df = pd.read_csv('winequality-red.csv')

#convert the target variable to a string
df['quality'] = df['quality'].astype(str)

#separate the target variable from the data
Y = df.pop('quality')
X = df

#separate the data into training and test sets
xtrain, xtest, ytrain, ytest = train_test_split(X, Y, test_size=0.2)

#create a model
rf = RandomForestClassifier()

#train the model
rf.fit(xtrain, ytrain)
# -

#import the partial dependence library
import partial_dependence as pdp_plot

#create a list of class labels
class_label = list(rf.classes_)

# bin a particular variable and get the plot for the rest of the variables. Number of bins must be specified
pdp_plot.PartialDependence.plot_binning(xtest, 'fixed acidity', rf, class_label, class_label[3], 4, 10)

#for a specified number of bins, the variables can be ranked by the total variance ot the plots
#this function prints the variables according to the rank and also returns a list of variables in the ranked order
pdp_plot.PartialDependence.get_rank_on_binning(xtest, rf, class_label, class_label[3], 4)

#trying binning on alcohol
pdp_plot.PartialDependence.plot_binning(xtest, 'alcohol', rf, class_label, class_label[3], 4, 10)

#trying binning on density
pdp_plot.PartialDependence.plot_binning(xtest, 'density', rf, class_label, class_label[3], 4, 10)



StarcoderdataJupyterScriptsDedupFiltered