Draw Logistic Regression Plots in Python
Project description
lorepy: Logistic Regression Plots for Python
Logistic 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 at loreplotr.
Installation
Lorepy can be installed using pip using the command below.
pip install lorepy
Usage
Data 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 code loreplot(data=iris_df, x="sepal width (cm)", y="species")
.
from lorepy import loreplot
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import pandas as pd
iris_obj = load_iris()
iris_df = pd.DataFrame(iris_obj.data, columns=iris_obj.feature_names)
iris_df["species"] = [iris_obj.target_names[s] for s in iris_obj.target]
loreplot(data=iris_df, x="sepal width (cm)", y="species")
plt.show()
Options
While lorepy has very few customizations, it is possible to pass arguments through to Pandas' DataFrame.plot.area and Matplotlib's pyplot.scatter to change the aesthetics of the plots.
Disable sample dots
Dots indicating where samples are located can be en-/disabled using the add_dots
argument.
loreplot(data=iris_df, x="sepal width (cm)", y="species", add_dots=False)
plt.show()
Custom styles
Additional 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.
from matplotlib.colors import ListedColormap
colormap=ListedColormap(['red', 'green', 'blue'])
loreplot(data=iris_df, x="sepal width (cm)", y="species", colormap=colormap)
plt.show()
Using scatter_kws
arguments for pyplot.scatter
can 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 implements predict_proba
and fit
. Below you can see the code and output with a
Support Vector Classifier (SVC) and Random Forest Classifier (RF).
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
fig, 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()
Contributing
Any contributions you make are greatly appreciated.
- Found a bug or have some suggestions? Open an issue.
- Pull requests are welcome! Though open an issue first to discuss which features/changes you wish to implement.
Contact
lorepy was developed by Sebastian Proost at the RaesLab and was based on R code written by Sara Vieira-Silva. As of version 0.2.0 lorepy is available under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.
For commercial access inquiries, please contact Jeroen Raes.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file lorepy-0.2.0.tar.gz
.
File metadata
- Download URL: lorepy-0.2.0.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb6f5a29291fe2e09c48f831aad32119edbdeed499b3c2bd7b72a1e44ff3a108 |
|
MD5 | 99cb8448e89d927a0df356ae578c3d78 |
|
BLAKE2b-256 | 5b632d8376597cc54a284ce1002d5dc3e88569d55ba021e3204fae568f508683 |
File details
Details for the file lorepy-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: lorepy-0.2.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 442556e05e7b02405e998a9ed1f03a7638f055f9fdeda41dae492db2a6b79972 |
|
MD5 | 708c16821363fa0a75f578de15ad387a |
|
BLAKE2b-256 | ff6e6993208c90822dd610afdfe502b8dd716673d803ed4b542abf68495f8b95 |