Package: lorepy
lorepy: Logistic Regression Plots for PythonLogistic Regression plots are used to plot the distribution of a categorical dependent variable in function of a
continuous independent variable.If you prefer an R implementation of this package, have a look atloreplotr.InstallationLorepy can be installed using pip using the command below.pip install lorepyUsageData needs to be provided as a DataFrame and the columns for the x (independent continuous) and y (dependant categorical)
variables need to be defined. Here the iris dataset is loaded and converted to an appropriate DataFrame. Once the data
is in shape it can be plotted using a single line of codeloreplot(data=iris_df, x="sepal width (cm)", y="species").fromlorepyimportloreplotfromsklearn.datasetsimportload_irisimportmatplotlib.pyplotaspltimportpandasaspdiris_obj=load_iris()iris_df=pd.DataFrame(iris_obj.data,columns=iris_obj.feature_names)iris_df["species"]=[iris_obj.target_names[s]forsiniris_obj.target]loreplot(data=iris_df,x="sepal width (cm)",y="species")plt.show()OptionsWhile lorepy has very few customizations, it is possible to pass arguments through to Pandas'DataFrame.plot.areaand Matplotlib'spyplot.scatterto change
the aesthetics of the plots.Disable sample dotsDots indicating where samples are located can be en-/disabled using theadd_dotsargument.loreplot(data=iris_df,x="sepal width (cm)",y="species",add_dots=False)plt.show()Custom stylesAdditional keyword arguments are passed to Pandas'DataFrame.plot.area.
This can be used, among other things, to define a custom colormap. For more options to customize these plots consult
Pandas' documentation.frommatplotlib.colorsimportListedColormapcolormap=ListedColormap(['red','green','blue'])loreplot(data=iris_df,x="sepal width (cm)",y="species",colormap=colormap)plt.show()Usingscatter_kwsarguments forpyplot.scattercan be set to change the appearance of the sample markers.scatter_options={'s':20,# Marker size'alpha':1,# Fully opaque'color':'black',# Set color to black'marker':'x'# Set style to crosses}loreplot(data=iris_df,x="sepal width (cm)",y="species",scatter_kws=scatter_options)plt.show()You can use LoRePlots in subplots as you would expect.fig,ax=plt.subplots(1,2,sharex=False,sharey=True)loreplot(data=iris_df,x="sepal width (cm)",y="species",ax=ax[0])loreplot(data=iris_df,x="petal width (cm)",y="species",ax=ax[1])ax[0].get_legend().remove()ax[0].set_title("Sepal Width")ax[1].set_title("Petal Width")plt.savefig('./docs/img/loreplot_subplot.png',dpi=150)plt.show()By default lorepy uses a multi-class logistic regression model, however this can be replaced with any classifier
from scikit-learn that implementspredict_probaandfit. Below you can see the code and output with a
Support Vector Classifier (SVC) and Random Forest Classifier (RF).fromsklearn.svmimportSVCfromsklearn.ensembleimportRandomForestClassifierfig,ax=plt.subplots(1,2,sharex=False,sharey=True)svc=SVC(probability=True)rf=RandomForestClassifier(n_estimators=10,max_depth=2)loreplot(data=iris_df,x="sepal width (cm)",y="species",clf=svc,ax=ax[0])loreplot(data=iris_df,x="sepal width (cm)",y="species",clf=rf,ax=ax[1])ax[0].get_legend().remove()ax[0].set_title("SVC")ax[1].set_title("RF")plt.savefig("./docs/img/loreplot_other_clf.png",dpi=150)plt.show()ContributingAny contributions you make aregreatly appreciated.Found a bug or have some suggestions? Open anissue.Pull requests are welcome! Though open anissuefirst to discuss which features/changes you wish to implement.Contactlorepy was developed bySebastian Proostat theRaesLaband was based on R code written bySara Vieira-Silva. As of version 0.2.0 lorepy is available under theCreative Commons Attribution-NonCommercial-ShareAlike 4.0 Internationallicense.For commercial access inquiries, please contactJeroen Raes.